原文地址: 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的更多相关文章

  1. nginx的url重写[rewrite规则和参考]

    本日志内容来自互联网和平日使用经验,整理一下方便日后参考. Nginx Rewrite 相关指令有 if.rewrite.set.return 等. if 的语法 应用于 server 和 locat ...

  2. nginx rewrite 实现URL跳转

    最近工作中常常要改nginx配置,学习了nginx中rewrite的用法 URL跳转这里说的URL跳转就是用户在访问一个URL时将其跳转到另一个URL上.常见的应用场景是让多个域名跳转到同一个URL上 ...

  3. Nginx – rewrite 配置 URL重写及301跳转原理图

    Nginx – rewrite 配置 URL重写 官网:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html 语法:rewrite re ...

  4. (Nginx) URL REWRITE

    URL重写的基础介绍 把URI地址用作参数传递:URL REWRITE 最简单的是基于各种WEB服务器中的URL重写转向(Rewrite)模块的URL转换: 这样几乎可以不修改程序的实现将 news. ...

  5. 再谈Nginx Rewrite, 中文URL和其它

    上次谈到过Nginx和中文URL的问题,这几天又加深了认识. 多分享几个关于Nginx Rewrite的经验. Nginx匹配指定中文URL的方法:rewrite "(*UTF8)^x{66 ...

  6. thinkphp nginx php-fpm url rewrite 导致 404 错误

    ## thinkphp nginx php-fpm url rewrite 导致 404 错误 之前thinkphp的系统部署在apache上,考虑到在并发性能nginx比apache强悍得多,所以在 ...

  7. nginx——rewrite模块

    1.什么是Nginx的Rewrite规则? Rewrite主要的功能就是实现URL的重写,Nginx的Rewrite规则采用PCRE(Perl Compatible Regular Expressio ...

  8. Nginx Rewrite规则初探(转)

    Nginx  rewrite(nginx url地址重写)Rewrite 主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Ng ...

  9. Nginx Rewrite规则记录

    Rewrite 是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术.它还可以实现限制特定IP访问网站的功能.很多情况下,某个 IP 的访问很容易造成 CPU ...

随机推荐

  1. dataframe 差集

    >>>data_a={'state':[1,1,2],'pop':['a','b','c']}>>>data_b={'state':[1,2,3],'pop':[' ...

  2. CentOS7上安装与配置Tomcat8与MySQL5.7

    一.安装tomcat Tomcat 的安装依赖 JDK,在安装 Tomcat 之前需要先安装 Java JDK.输入命令 java -version,如果显示 JDK 版本,证明已经安装了 JDK.

  3. 【前端vue开发】vue开发总结

  4. Python_oldboy_自动化运维之路_线程,进程,协程(十一)

    本节内容: 线程 进程 协程 IO多路复用 自定义异步非阻塞的框架 线程和进程的介绍: 举个例子,拿甄嬛传举列线程和进程的关系: 总结:1.工作最小单元是线程,进程说白了就是提供资源的 2.一个应用程 ...

  5. Long-Polling, Websockets, SSE(Server-Sent Event) 之间有什么区别?

    链接:http://www.mamicode.com/info-detail-1327667.html https://www.jianshu.com/p/d3f66b1eb748?from=time ...

  6. tomcat错误信息解决方案【严重:StandardServer.await: create[8005]】

    错误信息:   严重: StandardServer.await: create[8005]:  java.net.BindException: Address already in use: JVM ...

  7. php常用的安全过滤函数

    目录结构 ①常用的安全函数有哪些: ②这些函数的作用: ③函数的用法: ④举例说明: ⑤参考资料: 由于越来越多的项目开始使用框架,所以,很多的程序员也不在关心安全的问题!因为框架已经帮我们几乎完美的 ...

  8. 打造一款便携版的Sublime Text

    https://segmentfault.com/a/1190000000707661 https://www.cnblogs.com/52cik/p/sublime-diy.html 直接安装Sub ...

  9. 《高性能MySQL》学习笔记

    第1章 MySQL架构与历史 1.2 并发控制 MySQL在两个层面实现并发控制:服务器层与存储引擎层. 读锁和写锁: 在处理并发读或写时,可以通过实现一个由两种锁组成的系统来解决问题. 这两种锁通常 ...

  10. JavaScript中变量的相互引用

    http://www.jb51.net/article/23387.htm