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中变量和常量的理解
一.变量的定义:把程序运算的中间结果临时存到内存里,以备后面的代码继续调用,这几个名字的学名就叫做"变量". 二.变量的作用:变量用于存储要在计算机程序中引用和操作的信息.它提供了 ...
- java项目导出war包
@参考文档 假如项目名称为yanan,进入到yanan目录下打开cmd 执行:jar -cvf yanan.war ./* 命令详解: @参考文档1 @参考文档2 jar:jar工具是个java应用程 ...
- BSA Network Shell系列-scriptutil命令
scriptutil ## 1 功能概述 scriptutil复制脚本到远程机的某个目录,然后在该目录执行脚本. 它的优点就是脚本是non-NSH的脚本.不支持NSH命令,执行起来的效果和runscr ...
- Composer - windows下安装方法
在windows下安装的方法 方法一:使用安装程序 这是将 Composer 安装在你机器上的最简单的方法. 下载并且运行 Composer-Setup.exe,它将安装最新版本的 Composer ...
- @interface注解类、 @Target:注解的作用目标 @Retention
public @interface xxx 定义注解 @interface 不是interface,是注解类 是jdk1.5之后加入的,java没有给它新的关键字,所以就用@interface 这么个 ...
- awk匹配以aaa开头,以bbb结尾的内容,同时aaa和bbb之间还包含ccc
如果是匹配以A开头,以B结尾的内容,同时A和B之间还包含C的这种怎么做?比如 [root@localhost ~]#cat file aaa grge ddd bbb aaa gege ccc bbb ...
- javascript中的字符串对象和数组对象
1.javascript的对象的概念 在javascript中,除了null和undefined以处,其他的数据类型都被定义成了对象 也可以用创建对象的方法定义变量,string,math,array ...
- 制作 alipay-sdk-java包到本地仓库
一.首先 搭建好maven 基础环境,本地可以运行maven 命令 从支付宝官网上下载sdk https://doc.open.alipay.com/doc2/detail?treeId=54& ...
- Annotation基础知识
1.Annotation介绍 Annontation是Java5开始引入的新特征.中文名称一般叫注解. Annontation像一种修饰符一样,应用于包.类型.构造方法.方法.成员变量.参数及本地变量 ...
- hdu 5909 Tree Cutting [树形DP fwt]
hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...