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 ...
随机推荐
- PMS 启动流程
1.在SystemServer中启动PackageManagerService.main 2.newPackageManagerService()并添加到ServiceManager中 3.newin ...
- nagios的安装
Nagios通常由一个主程序(Nagios).一个插件程序(Nagios-plugins)和四个可选的ADDON(NRPE.NSCA. NSClient++和NDOUtils)组成.Nagios的监控 ...
- 【转】centos安装vim7.4
centos安装vim7.4 系统版本centos6.4; root权限 su - root 卸载 $ rpm -qa | grep vim $ yum remove vim vim-en ...
- ng机器学习视频笔记(二) ——梯度下降算法解释以及求解θ
ng机器学习视频笔记(二) --梯度下降算法解释以及求解θ (转载请附上本文链接--linhxx) 一.解释梯度算法 梯度算法公式以及简化的代价函数图,如上图所示. 1)偏导数 由上图可知,在a点 ...
- MySQL --当AUTO_INCREMENT自增遇到0
熟悉SQL Server的DBA都知道,在SQL Server中如果想要显示制定自增值,除了在INSERT语句中显示写明自增列外,还需要使用SET IDENTITY_INSERT TB_NAME ON ...
- 【PHP】学习中遇到的php方法
[1]range()快速创建一个范围内数组 <?php range(0,20); 创建一个包含从 "0" 到 "20" 之间的元素范围的数组: range ...
- 123 A. Prime Permutation
链接 http://codeforces.com/contest/123/problem/A 题目 You are given a string s, consisting of small Lati ...
- cmd中控制某个命令执行多少次
for /l %i in (1,1,5) do calc五次启动计算器程序for /l %i in (start ,step,end) do commandfor /l 表示从数字start开始,以s ...
- 二维数组int[3][2]在内存中的分布方式
- Round trip 流程图
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢!