原文地址: 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. memcache 键名的命名规则以及和memcached的区别

    2014年3月27日 07:47:46 Keys---- Data stored by memcached is identified with the help of a key. A keyis ...

  2. COM和.NET的互操作

    组件对象模型的基本知识         基于构件的软件开发日益流行,这里我吧自己在学校时整理的关于COM的一些东西献给大家,供初学者参考.一.组件(COM),是微软公司为了计算机工业的软件生产更加符合 ...

  3. LeetCode(19):删除链表的倒数第N个节点

    Medium! 题目描述: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了 ...

  4. watch案例解析(element-ui el-select 无法选中问题剖析)

    fire 读在最前面: 1.此文章衔接Vue 虚拟Dom 及 部分生命周期初探,相关整体知识点请先阅读后再继续本文阅读 问:子组件中明明有watch value,为什么this.$emit('inpu ...

  5. 微信小程序开发之IOS/Android兼容坑(持续更新)

    一.时间转换问题: 这不只是小程序上面的问题是ios系统 都有这个问题就是new  Date("2017-06-16") 在IOS会出现NAN的情况所以对于时间转换需要另行封装,解 ...

  6. Video.js 简单的使用介绍

    vedio.js 是一款视频播放插件,它会自动检测浏览器对 HTML5 的支持情况,如果不支持 HTML5 则自动使用 Flash 播放器.下面来介绍下它的使用: 引用video-js.cs样式文件和 ...

  7. codewar 上做练习的一些感触

    废话 在[codewar][1]上做练习,每次都是尽量快速地做完,然后赶着去看排名里面clever分最高的solution,看完每次都要感叹一下人家怎么可以写得这么简洁,甚至有一次我用了一段大约七八行 ...

  8. fis3-postpackager-loader

    静态资源前端加载器,用来分析页面中使用的和依赖的资源(js或css), 并将这些资源做一定的优化后插入页面中.如把零散的文件合并. 注意 此插件做前端硬加载,适用于纯前端项目,不适用有后端 loade ...

  9. Solr7.4.0的API(Solrj)操作

    一.SolrJ的概念 solr单机版服务搭建:https://www.cnblogs.com/frankdeng/p/9615253.html solr集群版服务搭建:https://www.cnbl ...

  10. fatal error C1060:compiler is out of heap space

    今天svn update了下代码,rebuild工程的时候报错: fatal error C1060:compiler is out of heap space 意思是说编译器堆内存不足 百度结果:V ...