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 实现 隐式URL 转发功能,good。

时间: 2024-11-25 17:44:52

Nginx rewrite URL examples with and without redirect address的相关文章

Nginx Rewrite和Redirect模块使用说明

Nginx Rewrite 1. Nginx Rewrite 基本标记(flags) last - 基本上都用这个Flag.※相当于http://www.aliyun.com/zixun/aggregation/14417.html">Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则 break - 中止Rewirte,不再继续匹配 redirect - 返回临时重定向的HTTP状态302 permanent - 返回永久重定向的HTTP状态301 ※原有的url支持

详解nginx rewrite和根据url参数location_nginx

最近项目中涉及到旧老项目迁移,需要在nginx上做些配置,所以简单学习了下,好记性不如烂笔头,先记下来. rewrite 首先查看下nginx是否支持rewrite: ./nginx -V 不支持说明安装nginx时候缺少pcre,需要重新安装nginx: #安装pcre wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz tar -zxvf pcre-8.34.tar.gz cd pcre-8.

nginx rewrite规则怎么配置?

 在url优化过程中,不可避免的涉及到nginx rewrite规则.那么nginx rewrite是如何配置的呢? rewrite可以出现的地方有4个:NGX_HTTP_SRV_CONF,NGX_HTTP_SIF_CONF,NGX_HTTP_LOC_CONF,NGX_HTTP_LIF_CONF.分别对应着: NGX_HTTP_SRV_CONF:配置文件中的server域中的任何地方: NGX_HTTP_SIF_CONF:配置文件中server域中的if配置中: NGX_HTTP_LOC_CON

Nginx Rewrite规则初探(转)

Nginx  rewrite(nginx url地址重写)Rewrite 主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Nginx的Rewrite功能,在编译Nginx之前,需要编译安装PCRE库. 通过Rewrite规则,可以实现规范的URL.根据变量来做URL转向及选择配置.    if        指令规则语法: [plain] view plain copy   if ($http_user_agent ~MS

nginx rewrite重写规则配置教程详细介绍

rewrite可以出现的地方有4个:NGX_HTTP_SRV_CONF,NGX_HTTP_SIF_CONF,NGX_HTTP_LOC_CONF,NGX_HTTP_LIF_CONF.分别对应着: NGX_HTTP_SRV_CONF:配置文件中的server域中的任何地方: NGX_HTTP_SIF_CONF:配置文件中server域中的if配置中: NGX_HTTP_LOC_CONF:配置文件中的location域中的任何地方: NGX_HTTP_LIF_CONF:配置文件中的location域中

WordPress缓存插件wp-super-cache的nginx rewrite规则

WordPress的缓冲插件wp-super-cache默认支持apahce的缓冲方式,在生成了静态页面数据后,通过.htaccess的规则直接让apache读取静态文件,完全不经过PHP,可以很大的提高博客的页面性能. 但是Nginx的改写规则就没这么容易让代码来配置了,虽然wp-super-cache的第二种缓存方式就是为这种使用环境设计,但实际上是用了PHP来提供静态数据了,在使用apache benchmark压力的时候,php-cgi依然占很高的CPU占有率. 通过编写nginx的re

nginx rewrite 经典实例判断IE浏览器并提示

搞过前端的估计都碰到最头疼的问题就是浏览器兼容性问题了,特别是针对IE浏览器.往往前端为了省事就搞一个页面提示用户升级浏览器或者显示简单的静态页面.那接下来就需要运维来配置nginx rewrite规则了. 在这里直接贴出配置实例 server {listen 80;server_name xxx.xxx.com;root   /www ;if ( $http_user_agent ~* "MSIE [6-9].[0-9]") {rewrite   /ie.html break;}}

nginx rewrite重写规则与防盗链配置方法教程详解_nginx

导读:nginx rewrite重写规则与防盗链配置方法,rewrite规则格式中flag标记的几种形式,盗链时返回403错误,允许的域名直接跟在第二行的域名后面. nginx rewrite重写规则与防盗链配置方法如下所示: nginx rewite 规则,官方文档:http://wiki.nginx.org/NginxHttpRewriteModule nginx rewrite规则格式:rewrite regex replacement flag flag标记有四种格式: last – 相

nginx rewrite怎么写

问题描述 nginx rewrite怎么写 原动态 :域名/index.php?a=index&m=cate&cid=1007 怎么在nginx rewrite规则里面写 让他在浏览器输出 cate_1007.html