Nginx rewrite URL examples with and without redirect address
原文地址: http://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.VY9nfJeqqko
Nginx can handle the rewrite parameter differently, depending on the destination syntax.
Here are some examples how to define redirects and URL rewrites in nginx.
server {
server_name www.example.com;
root /var/www/www.example.com;
location / {
rewrite ^/$ http://websrv1.example.com/mypage redirect;
}
}
This will result in forwarding the browser to http://websrv1.example.com/mypage. The redirect address will be shown in the address bar.
Let's try this without a redirect or permanent option but with break or last:
server {
server_name www.example.com;
root /var/www/www.example.com;
location / {
rewrite ^/$ http://websrv1.example.com/mypage last;
}
}
Although the rewrite option is now set to last, the browser will still follow the URL and changes the URL in the address bar.
The reason for this is the http:// which is interpreted as external redirect.
So if you want to keep your domain and simply want to rewrite the URL (like in Apache with mod_rewrite), you must use a relative path:
server {
server_name www.example.com;
root /var/www/www.example.com;
location / {
rewrite ^/$ /mypage last;
}
}
This will load the website for www.example.com from the subfolder /mypage within the document root (/var/www/www.example.com).
But what if the destination website is loaded from somewhere else, for example from a Tomcat server in the background?
The following configuration covers this:
upstream tomcat {
server 127.0.0.1:8080;
}
server {
server_name www.example.com;
root /var/www/www.example.com;
location / {
include proxy-settings.conf;
proxy_pass http://tomcat;
rewrite ^/$ /mypage last;
}
}
First everything (location /) is passed to tomcat (the defined upstream server). Then the redirect for the root path (/) is happening and is relative to the path.
This results in keeping the browser's address URL at www.example.com but loads the website from 127.0.0.1:8080/mypage.
Nginx rewrite URL examples with and without redirect address的更多相关文章
- nginx的url重写[rewrite规则和参考]
本日志内容来自互联网和平日使用经验,整理一下方便日后参考. Nginx Rewrite 相关指令有 if.rewrite.set.return 等. if 的语法 应用于 server 和 locat ...
- nginx rewrite 实现URL跳转
最近工作中常常要改nginx配置,学习了nginx中rewrite的用法 URL跳转这里说的URL跳转就是用户在访问一个URL时将其跳转到另一个URL上.常见的应用场景是让多个域名跳转到同一个URL上 ...
- Nginx – rewrite 配置 URL重写及301跳转原理图
Nginx – rewrite 配置 URL重写 官网:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html 语法:rewrite re ...
- (Nginx) URL REWRITE
URL重写的基础介绍 把URI地址用作参数传递:URL REWRITE 最简单的是基于各种WEB服务器中的URL重写转向(Rewrite)模块的URL转换: 这样几乎可以不修改程序的实现将 news. ...
- 再谈Nginx Rewrite, 中文URL和其它
上次谈到过Nginx和中文URL的问题,这几天又加深了认识. 多分享几个关于Nginx Rewrite的经验. Nginx匹配指定中文URL的方法:rewrite "(*UTF8)^x{66 ...
- thinkphp nginx php-fpm url rewrite 导致 404 错误
## thinkphp nginx php-fpm url rewrite 导致 404 错误 之前thinkphp的系统部署在apache上,考虑到在并发性能nginx比apache强悍得多,所以在 ...
- nginx——rewrite模块
1.什么是Nginx的Rewrite规则? Rewrite主要的功能就是实现URL的重写,Nginx的Rewrite规则采用PCRE(Perl Compatible Regular Expressio ...
- Nginx Rewrite规则初探(转)
Nginx rewrite(nginx url地址重写)Rewrite 主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Ng ...
- Nginx Rewrite规则记录
Rewrite 是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术.它还可以实现限制特定IP访问网站的功能.很多情况下,某个 IP 的访问很容易造成 CPU ...
随机推荐
- React-Native 之 ListView使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- java 异常说明
异常说明使用了附加的关键字 throws ,后面接一个所有潜在异常类型的列表,方便客户端程序员查看. public static void main(String[] args) throws Nul ...
- 【TensorFlow】获取object detection API训练模型的输出坐标
如下图,谷歌开源的object detection API提供了五种网络结构的fine-tuning训练权重,方便我们针对目标检测的需求进行模型训练,本文详细介绍下导出训练模型后,如何获得目标检测框的 ...
- hdu 1700 (圆的内接三角形 要周长最大)
以原点为圆心,给出圆上的一点,要求圆上的另外两点,使得这三个点的距离和最大,很容易想到这是一个等边三角形然后有这两个公式 点a为已知点a*b=|a|*|b|*cos(120); x*x+y*y=r*r ...
- #JS 获取屏幕分辨率、网页可见区域等
window.screen document.body <script type="text/javascript"> function getInfo(){ var ...
- Codeforces 405E Graph Cutting
Graph Cutting 不会写.. dfs的过程中把回边丢到它的祖先中去, 回溯的时候两两配对.感觉好神奇啊. #include<bits/stdc++.h> #define LL l ...
- 对C转换说明符的误解以及关于数组的一些知识
事实上,scanf()函数中%c并非是用来输入单个字符的,而是用来输入一组字符的. 例如: ]; scanf("%3c",a); 其中“3”规定了输入数据的宽度,当然宽度为“1”的 ...
- 配置SSH服务使用证书登录Ubuntu服务器
根据项目要求,需要将项目迁移到Linux系统上,作为测试,选用的是阿里云服务器,1核CPU,1G内存(没错就是这么穷),操作系统Ubuntu 16.04 64位.当然其实如果使用阿里云服务器其实是不需 ...
- Angular 个人深究(一)【Angular中的Typescript 装饰器】
Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...
- 002.LVM创建
一 LVM创建步骤 创建分区 创建PV 创建VG 创建LV 格式化及挂载 二 创建分区 使用分区工具(如fdisk等)创建LVM分区,却将分区标识为LVM的分区类型8e. [root@kauai ~] ...