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 ...
随机推荐
- java虚拟机和java内存区域概述
什么是虚拟机,什么是Java虚拟机 虚拟机 定义:模拟某种计算机体系结构,执行特定指令集的软件 系统虚拟机(Virtual Box.VMware),进程虚拟机 进程虚拟机 jvm.Adobe Flas ...
- android adapter 中添加OnClickListener事件
public class SearchAutoAdapter extends BaseAdapter { private OnClickListener mOnClickListener; publi ...
- Ubantu搭建FTP
1.安装并启动 FTP 服务 安装 VSFTPD 使用 apt-get 安装 vsftpd kylin@kylin:~$ sudo apt-get install vsftpd -y [sudo] p ...
- Akamai CDN
Akamai CDN中的几个重要组件 mapping system 调度系统(映射client到edge cluster,进而到edge server) edge server platform 边缘 ...
- 关键字voltale
***volatile在多线程用的最多.*** #include<stdio.h> #include<stdlib.h> int main() { ; i < ; i++ ...
- 网易云免费OSS服务用做Markdown图床或博客图片外链
我使用据说是Windows下最好用的Markdown编辑器“MarkdownPad2”(个人感觉还是Visual Code+Markdown插件666)写Markdown,在贴图方面遇到一个问题,于是 ...
- Java垃圾回收机制[转]
原文地址:http://blog.csdn.net/zsuguangh/article/details/6429592 综合了若干人的blog- 1. 垃圾回收的意义 在C++中,对象所占的内存在程序 ...
- UVA 12633 Super Rooks on Chessboard [fft 生成函数]
Super Rooks on Chessboard UVA - 12633 题意: 超级车可以攻击行.列.主对角线3 个方向. R * C 的棋盘上有N 个超级车,问不被攻击的格子总数. 行列好好做啊 ...
- 【深度学习】L1正则化和L2正则化
在机器学习中,我们非常关心模型的预测能力,即模型在新数据上的表现,而不希望过拟合现象的的发生,我们通常使用正则化(regularization)技术来防止过拟合情况.正则化是机器学习中通过显式的控制模 ...
- 用Spring Tools Suite(STS)开始一个RESTful Web Service
spring.io官方提供的例子Building a RESTful Web Service提供了用Maven.Gradle.STS构建一个RESTFul Web Service,实际上采用STS构建 ...