NGINX中的proxy_pass和rewrite
文章作者:luxianghao
文章来源:http://www.cnblogs.com/luxianghao/p/6807081.html 转载请注明,谢谢合作。
免责声明:文章内容仅代表个人观点,如有不当,欢迎指正。
---
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=$? 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
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=;
server backend2.example.com:;
server unix:/tmp/backend3; server backup1.example.com: backup;
server backup2.example.com: backup;
} server {
location / {
proxy_pass http://backend;
}
} - server name, port, URI支持变量的形式,eg:
proxy_pass http://$host$uri;
server {
listen ;
server_name example.com;
proxy_read_timeout ;
proxy_send_timeout ;
include /etc/nginx/fastcgi_params;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
location / {
...
}
}
- 如果proxy_pass的URL定向里包括URI,那么请求中匹配到location中URI的部分会被proxy_pass后面URL中的URI替换,eg:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}
请求http://example.com/name/test.html 会被代理到http://127.0.0.1/remote/test.html - 如果proxy_pass的URL定向里不包括URI,那么请求中的URI会保持原样传送给后端server,eg:
location /name/ {
proxy_pass http://127.0.0.1;
} 请求http://example/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=$ break;
proxy_pass http://127.0.0.1;
}
NGINX中的proxy_pass和rewrite的更多相关文章
- 转:NGINX中的proxy_pass和rewrite
章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6807081.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点,如有 ...
- Nginx中的Location和Rewrite
Nginx中的Location和Rewrite 目录 Nginx中的Location和Rewrite 一.location 1. location的分类 2. location常用的匹配规则 3. l ...
- nginx中配置proxy_pass
在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走. 下面四种 ...
- Nginx中location匹配及rewrite重写
目录 一.常用的Nginx正则表达式 二.location 2.1.location三类匹配类型 2.2.常用的匹配规则 2.3.location优先级 2.3.1.举例说明 2.4.实际网站使用中, ...
- nginx proxy_pass 与 rewrite 简记
rewrite syntax: rewrite regex replacement [flag] Default: - Context: server, location, if 如果正则表达式(re ...
- 【nginx】之proxy_pass
在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径:如果没有/,表示相对路径,把匹配的路径部分也给代理走. 假设下面四种情况分别用 h ...
- Nginx中的 location 匹配和 rewrite 重写跳转
Nginx中的location匹配和rewrite重写跳转 1.常用的Nginx正则表达式 2.location 3.rewrite 4.rewrite实例 1.常用的Nginx正则表达式: ^ :匹 ...
- nginx反向代理(proxy_pass)tomcat的过程中,session失效的问题解决
Nginx反向代理tomcat,很是方便,但是也有些细节的问题需要注意:今天遇到了这样一个问题,tomcat中路径“host/web1”,nginx中直接“host/”代理,这时候session就无法 ...
- nginx配置url中带问号的rewrite跳转
今天收到一个需求,要将一个带查询参数的url跳转到另外一个静态url,安装常规的rewrite规则,如: rewrite ^/a.html?id=67$ http://zt.epython.cn/20 ...
随机推荐
- Linux(CentOS6.7) 安装MySql5.7数据库
linux(CentOS6.7) 环境Mysql 5.7.17安装教程分享给大家,供大家参考,具体内容如下: 1系统约定安装文件下载目录:/data/softwareMysql目录安装位置:/usr/ ...
- 运行错误:应用程序无法启动因为并行配置不正确。the application has failed to start because its side-by-side configuration is incorrect 解决方法
问题描述: 当电脑同时安装VS2008和VS2008 SP1时,编译出来的Visual C++程序的manifest 文件会默认引用VS2008的MFC版本和CRT版本.如下: <depende ...
- (转)centos6.5安装VNC
在Linux下用VNC远程桌面是个很不错的玩意.但在CentOS中默认没有安装VNC的.可以用下面语句查询,如果出现下面情况说明没有安装vnc #rpm -q tigervnc tigervnc-se ...
- 利用shell实现批量添加用户
批量添加用户并设置随机密码,把添加的用户的名字和密码保存到文件中. [root@lamp scripts]# cat user.sh #!/bin/sh ` do pass=$(-) //取随机数的方 ...
- 利用echo命令实现倒计时的功能
echo -e:启用反斜线控制字符的转换 -E:关闭反斜线控制字符的转换(预设如此) -n:取消行末之换行符号(与 -e 选项下的 \c 字符同意 -e参数下的控制参数 \ ...
- angular替代Jquery,常用方法支持
1.angular.bind(self,fn.args); 切换作用域执行 2.angular.copy(source,[destination]); 拷贝和深度拷贝 3.angular.eq ...
- DPDK QoS之分层调度器
原创翻译,转载请注明出处. 分层调度器的时机主要体现在TX侧,正好在传递报文之前.它的主要目的是在每个网络节点按照服务级别协议来对不同的流量分类和对不同的用户的报文区分优先级并排序. 一.概述分层调度 ...
- Terminating app due to uncaught exception 'NSUnknownKeyException' this class is not key value coding-compliant for the key
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ViewController > se ...
- Python第一天接触心得
最近想学Python,就开始看教程下载,官网是https://www.python.org/downloads/,最新版本是3.6.1, 注意:x86-64表示适用于windows 64位系统:x86 ...
- mysql笔记一——安装和设置root密码
1. mysql 5.6安装包下载. MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提 ...