nginx proxy_pass 与 rewrite 简记
syntax: rewrite regex replacement [flag]
Default: —
Context: server, location, if
- 如果正则表达式(regex)匹配到了请求的URI(request URI),这个URI会被后面的replacement替换
- rewrite的定向会根据他们在配置文件中出现的顺序依次执行
- 通过使用flag可以终止定向后进一步的处理
- 如果replacement以“http://”, “https://”, or “$scheme”开头,处理将会终止,请求结果会以重定向的形式返回给客户端(client)
- 如果replacement字符串里有新的request参数,那么之前的参数会附加到其后面,如果要避免这种情况,那就在replacement字符串后面加上“?”,eg:
rewrite ^/users/(.*)$ /show?user=$1? last;=
- 如果正则表达式(regex)里包含“}” or “;”字符,需要用单引号或者双引号把正则表达式引起来
- last
- 结束当前的请求处理,用替换后的URI重新匹配location;
- 可理解为重写(rewrite)后,发起了一个新请求,进入server模块,匹配location;
- 如果重新匹配循环的次数超过10次,nginx会返回500错误;
- 返回302 http状态码 ;
- 浏览器地址栏显示重地向后的url
- break
- 结束当前的请求处理,使用当前资源,不在执行location里余下的语句;
- 返回302 http状态码 ;
- 浏览器地址栏显示重地向后的url
- redirect
- 临时跳转,返回302 http状态码;
- 浏览器地址栏显示重地向后的url
- permanent
- 永久跳转,返回301 http状态码;
- 浏览器地址栏显示重定向后的url
Nginx配置proxy_pass转发的/路径问题
在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com/;
}
如上面的配置,如果请求的url是http://servername/static_js/test.html
会被代理成http://js.test.com/test.html
而如果这么配置
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com;
}
则会被代理到http://js.test.com/static_js/test.htm
当然,我们可以用如下的rewrite来实现/的功能
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
rewrite /static_js/(.+)$ /$1 break;
proxy_pass http://js.test.com;
}
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
- 不影响浏览器地址栏的url
- 设置被代理server的协议和地址,URI可选(可以有,也可以没有)
- 协议可以为http或https
- 地址可以为域名或者IP,端口可选;eg:
proxy_pass http://localhost:8000/uri/;
- 如果一个域名可以解析到多个地址,那么这些地址会被轮流使用,此外,还可以把一个地址指定为 server group(如:nginx的upstream), eg:

upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3; server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
} server {
location / {
proxy_pass http://backend;
}
}
- server name, port, URI支持变量的形式,eg:
proxy_pass http://$host$uri;
- 如果proxy_pass的URL定向里包括URI,那么请求中匹配到location中URI的部分会被proxy_pass后面URL中的URI替换,eg:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}
请求http://127.0.0.1/name/test.html 会被代理到http://example.com/remote/test.html - 如果proxy_pass的URL定向里不包括URI,那么请求中的URI会保持原样传送给后端server,eg:
location /name/ {
proxy_pass http://127.0.0.1;
} 请求http://127.0.0.1/name/test.html 会被代理到http://127.0.0.1/name/test.html - 一些情况下,不能确定替换的URI
- location里是正则表达式,这种情况下,proxy_pass里最好不要有URI
- 在proxy_pass前面用了rewrite,如下,这种情况下,proxy_pass是无效的,eg:
location /name/ {
rewrite /name/([^/]+) /users?name=$1 break;
proxy_pass http://127.0.0.1;
}
nginx proxy_pass 与 rewrite 简记的更多相关文章
- Nginx 之四: Nginx服务器的rewrite、全局变量、重定向和防盗链相关功能
一:Nginx 后端服务器组的配置: 1.upstream: 用于设置后端服务器组的主要指令,upstream类似于之前的server块或http块,用法如下: upstreame Myserver{ ...
- Nginx服务器的rewrite、全局变量、重定向和防盗链相关功能
一:Nginx 后端服务器组的配置: 1.upstream: 用于设置后端服务器组的主要指令,upstream类似于之前的server块或http块,用法如下: upstreame Myserver{ ...
- Nginx中的Rewrite的重定向配置与实践
阅读目录 一:理解地址重写 与 地址转发的含义. 二:理解 Rewrite指令 使用 三:理解if指令 四:理解防盗链及nginx配置 简介:Rewrite是Nginx服务器提供的一个重要的功能, ...
- 17.Nginx 重写(location rewrite)
Nginx 重写(location / rewrite) 目录 Nginx 重写(location / rewrite) 常见的nginx正则表达式 location lication的分类 loca ...
- thinkphp nginx php-fpm url rewrite 导致 404 错误
## thinkphp nginx php-fpm url rewrite 导致 404 错误 之前thinkphp的系统部署在apache上,考虑到在并发性能nginx比apache强悍得多,所以在 ...
- nginx重写规则报nginx: [emerg] directive "rewrite" is not terminated by ";"
对于下面的重写规则 rewrite ^/gongying/([\d]{8})_([\d]+).html$ /index.php?app=support&act=view&pts=$1& ...
- nginx里面的rewrite配置
哎,我需要静静,刚刚在去怎么优化dom层级,发现更新完代码,层级又蹭蹭蹭的往上涨,顿时没脾气了,还是把昨天的nginx配置总结下,增加点动力,昨天前天两天都在搞这个问题,也是搞的没脾气,网上查了很多资 ...
- [Linux] 解决nginx: [emerg] directive "rewrite" is not terminated by ";"
解决nginx: [emerg] directive "rewrite" is not terminated by ";"nginx的rewite规则有时候没注 ...
- nginx proxy_pass 指令
nginx proxy_pass 指令 文档 Nginx 官方文档 https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pa ...
随机推荐
- python初识-day2
1.模块初识 python中的库包括标准库和第三方库,需要使用时可直接导入,导入格式为"import 库模块",例如: import sys #sys为python的标准库 pr ...
- 函数式编程--为什么会出现lambda表达式?
java一直处在发张和演化的过程中,其中有2个版本从根本上改变了代码的编写方式.第一个就是JDK5之后增加的泛型,还有一个就是现在介绍的函数式编程,lambda表达式. lambda表达式是java8 ...
- 一个滑动选中RecyclerView中Item的布局SlidingCheckLayout,手指滑过Item时多项选中。
SlidingCheckLayout是一个滑动选中RecyclerView中Item的布局,手指滑过Item时多项选中. 作者:竹尘居士 github:https://github.com/homgw ...
- C# 多线程复习笔记
编码的日子其实也有一段时间了,但是,作为一个客户端程序,因为自己是做游戏开发的,一直没有对线程这个概念比较模糊吧. 记录下线程的整理学习路线.原文:http://www.cnblogs.com/min ...
- HTML5图形绘制
要在HTML5中绘制图形,首先要放置一个canvas元素 <canvas id="canvas" width="400" height="300 ...
- tomcat无法打开8080页面
tomcat已启动 app已经正常执行 但不能打开8080管理页面 可能是在webapps目录下没有ROOT目录
- 【转】VMware 克隆 Linux 系统后找不到 eth0 网卡问题
[问题描述] 使用 VMware 虚拟机的克隆功能,快速复制已安装好的 Linux 系统. 克隆完成之后,发现没有 eth0 网卡. [解决方法] 1. 编辑 /etc/udev/rules.d/70 ...
- mvn 手动安装jar 到本地库
安装: mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.1.0.7.0 -Dpackagi ...
- Effective Java 之-----谨慎的覆盖clone方法
1.概述 如果clone方法返回一个由构造器创建的对象,它就得到有错误的类.因此,如果覆盖了非final类中的clone方法,则应该返回一个通过调用super.clone得到的对象.如果类的所有超类都 ...
- BZOJ 1192: [HNOI2006]鬼谷子的钱袋 [娱乐]
题意: n个数分组,使得小于n的每个数都能表示出来,最少几组 就是“最优集合”的超级弱化版.....每次+=now+1 然后一个貌似科学的方法是n二进制拆分 #include <iostream ...