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 ...
随机推荐
- 破解验证码模拟登陆cnblogs
from selenium import webdriver from selenium.webdriver import ActionChains from PIL import Image imp ...
- Java基础88 数据库设计的三大范式
数据库的设计原则:建议设计的表尽量遵守三大范式 1.第一范式 要求表的每个字段必须是不可分割的独立单元 Student表: name -- 违反了第一范式 张 ...
- idea中搜狗输入法不跟随光标,看不到输入的字
好久没在windows上开发了,今天遇到一个比较坑的问题: 最新版idea,输入法都是最新的;但是idea里面输入字,看不到自己输入的是什么字,好坑... 在外面可以看到输入什么字说明与输入法无关, ...
- Java登陆拦截器
package com.beidou.warehouseerp.interceptor; import com.alibaba.fastjson.JSON; import com.beidou.war ...
- 【AtCoder】CODE FESTIVAL 2017 Final
A - AKIBA 模拟即可 代码 #include <bits/stdc++.h> #define fi first #define se second #define pii pair ...
- Java中实现多线程的两种方式之间的区别
Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...
- Python学习——set集合的补充
set 是一个无序且不重复的元素集合>>> num = {1,2,3,4,5} 1.add()添加一个元素 >>> num.add(6) >>> ...
- 用python做一个图片验证码
看一下做出来的验证码长啥样 验证码分析 1. 有很多点 2. 有很多线条 3. 有字母,有数字 需要用到的模块: 1. random 2. Pillow (python3中使用pillow) 安装p ...
- 【坐标离散化】AOJ0531- Paint Color
日文题……一开始被题目骗了以为真的要写文件? 题目大意&&解答戳:❀ #include<iostream> #include<cstdio> #include& ...
- 纠结好久的VM虚拟机MAC地址绑定问题
VM虚拟机(centos)采用桥接的方式访问网络,搭建一个Online Judger 的 web服务端.本想让虚拟机的ip能够固定下来,因此在路由上采用MAC和IP绑定的方式解决. 结果:每次重启虚拟 ...