nginx 之 proxy_pass
nginx中有两个模块都有proxy_pass指令
ngx_http_proxy_module的proxy_pass
语法: proxy_pass URL;
场景: location, if in location, limit_except
说明: 设置后端代理服务器的协议(protocol)和地址(address),以及location中可以匹配的一个可选的URI。协议可以是"http"或"https"。地址可以是一个域名或ip地址和端口,或者一个 unix-domain socket 路径。
详见官方文档: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
ngx_stream_proxy_module的proxy_pass
语法: proxy_pass address;
场景: server
说明: 设置后端代理服务器的地址。这个地址(address)可以是一个域名或ip地址和端口,或者一个 unix-domain socket路径。
详见官方文档: http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_pass
两个proxy_pass的关系和区别
在两个模块中,两个proxy_pass都是用来做后端代理的指令。ngx_stream_proxy_module模块的proxy_pass指令只能在server段使用使用, 只需要提供域名或ip地址和端口。可以理解为端口转发,可以是tcp端口,也可以是udp端口。ngx_http_proxy_module模块的proxy_pass指令需要在location段,location中的if段,limit_except段中使用,处理需要提供域名或ip地址和端口外,还需要提供协议,如"http"或"https",还有一个可选的uri可以配置。
proxy_pass的具体用法
ngx_stream_proxy_module模块的proxy_pass指令
server {
listen 127.0.0.1:;
proxy_pass 127.0.0.1:;
}
server {
listen ;
proxy_connect_timeout 1s;
proxy_timeout 1m;
proxy_pass example.com:;
}
server {
listen udp;
proxy_responses ;
proxy_timeout 20s;
proxy_pass dns.example.com:;
}
server {
listen [::]:;
proxy_pass unix:/tmp/stream.socket; #设置源站地址
}
ngx_http_proxy_module模块的proxy_pass指令
server {
listen ;
server_name www.test.com;
# 正常代理,不修改后端url的
location /some/path/ {
proxy_pass http://127.0.0.1;
}
# 修改后端url地址的代理(本例后端地址中,最后带了一个斜线)
location /testb {
proxy_pass http://www.other.com:8801/;
}
# 使用 if in location
location /google {
if ( $geoip_country_code ~ (RU|CN) ) {
proxy_pass http://www.google.hk;
}
}
location /yongfu/ {
# 没有匹配 limit_except 的,代理到 unix:/tmp/backend.socket:/uri/
proxy_pass http://unix:/tmp/backend.socket:/uri/;
# 匹配到请求方法为: PUT or DELETE, 代理到9080
limit_except PUT DELETE {
proxy_pass http://127.0.0.1:9080;
}
}
}
proxy_pass后端服务器的url(request_uri)情况分析
server {
listen ;
server_name www.test.com;
# 情形A
# 访问 http://www.test.com/testa/aaaa
# 后端的request_uri为: /testa/aaaa
location ^~ /testa/ {
proxy_pass http://127.0.0.1:8801;
}
# 情形B
# 访问 http://www.test.com/testb/bbbb
# 后端的request_uri为: /bbbb
location ^~ /testb/ {
proxy_pass http://127.0.0.1:8801/;
}
# 情形C
# 下面这段location是正确的
location ~ /testc {
proxy_pass http://127.0.0.1:8801;
}
# 情形D
# 下面这段location是错误的
#
# nginx -t 时,会报如下错误:
#
# nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular
# expression, or inside named location, or inside "if" statement, or inside
# "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17
#
# 当location为正则表达式时,proxy_pass 不能包含URI部分。本例中包含了"/"
location ~ /testd {
proxy_pass http://127.0.0.1:8801/; # 记住,location为正则表达式时,不能这样写!!!
}
# 情形E
# 访问 http://www.test.com/ccc/bbbb
# 后端的request_uri为: /aaa/ccc/bbbb
location /ccc/ {
proxy_pass http://127.0.0.1:8801/aaa$request_uri;
}
# 情形F
# 访问 http://www.test.com/namea/ddd
# 后端的request_uri为: /yongfu?namea=ddd
location /namea/ {
rewrite /namea/([^/]+) /yongfu?namea=$ break;
proxy_pass http://127.0.0.1:8801;
}
# 情形G
# 访问 http://www.test.com/nameb/eee
# 后端的request_uri为: /yongfu?nameb=eee
location /nameb/ {
rewrite /nameb/([^/]+) /yongfu?nameb=$ break;
proxy_pass http://127.0.0.1:8801/;
}
access_log /data/logs/www/www.test.com.log;
}
server {
listen ;
server_name www.test.com;
root /data/www/test;
index index.php index.html;
rewrite ^(.*)$ /test.php?u=$ last;
location ~ \.php$ {
try_files $uri =;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log /data/logs/www/www.test.com..log;
}
小结
情形A和情形B进行对比,可以知道proxy_pass后带一个URI,可以是斜杠(/)也可以是其他uri,对后端request_uri变量的影响。
情形D说明,当location为正则表达式时,proxy_pass不能包含URI部分(proxy_pass在以下情况下,指令中不能有URI,正则表达式位置、命名的地点、if 块),
情形E通过变量($request_uri, 也可以是其他变量),对后端的request_uri进行改写。
情形F和情形G通过rewrite配合break标志,对url进行改写,并改写后端的request_uri。需要注意,proxy_pass地址的URI部分在情形G中无效,不管如何设置,都会被忽略。
nginx中文文档-ngx_stream_proxy_module
nginx 之 proxy_pass的更多相关文章
- Nginx配置proxy_pass
nginx配置proxy_pass,需要注意转发的路径配置 1.location /test/ { proxy_pass http://t6:8300; } 2.location /test/ { p ...
- Nginx配置proxy_pass转发的/路径问题
Nginx配置proxy_pass转发的/路径问题 在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则 ...
- Nginx的proxy_pass及upstream的小型负载均衡
proxy_pass Nginx的proxy_pass将请求代理到其他的后端服务器.例如 listen 9999; server_name wyc.com; location /test/aaa { ...
- nginx中proxy_pass小斜杠
nginx中proxy_pass小斜杠 1. 故事背景 相信做微信公众号开发的朋友都知道,要想在微信中预览效果,必须使用域名访问.很多朋友使用内网穿透工具.不仅不好用还不稳定.所以,发挥脸厚吃天下的态 ...
- 记录一次 Nginx 配置 proxy_pass 后 返回404问题
一. Nginx 配置 proxy_pass 后 返回404问题 故障解决和定位 1.1. 问题 在一次生产涉及多次转发的配置中, 需求是下面的图: 在配置好了 proxy_pass 之后,请求 ww ...
- 解决nginx使用proxy_pass反向代理时,cookie丢失的问题
1. 如果只是host.端口转换,则cookie不会丢失.例如: location /project { proxy_pass http://127.0.0.1:8080/pr ...
- 解决nginx中proxy_pass到tomcat的session丢失问题
之前在配置tomcat的时候都是一个项目对应一个tomcat,也就是一个端口.最近需要把两个项目整合到同一个tomcat中,通过配置nginx让两个域名指向同一tomcat的不同项目.整合完毕后发现其 ...
- nginx 代理 proxy_pass设置
#img.test.com/img1 实际访问的路径是 http://127.0.0.1:123/a1 #img.test.com/img2 实际访问的路径是 http://127.0.0.1:123 ...
- Nginx配置proxy_pass【转载】
在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走. 下面四种 ...
随机推荐
- java Web 请求servlet绘制验证码简单例子
主要用来了解java代码怎么绘制验证码图片,实际开发中不会这样用 protected void doGet(HttpServletRequest request, HttpServletRespons ...
- 一个由有符号下标引起的bug
先看段代码: if(s[d[i]]) { ... } 这里的d是一个char*的内存buffer,s是一个256长度的bool数组.上段代码逻辑是,s已进行过初始化,其作用是过滤字节,有些字节对应tr ...
- mfs教程(四)
mfs文件系统(四) MooseFS 维护 一.启动MooseFS集群 最安全的启动MooseFS集群(避免任何读或写的错误数据或类似的问题)的方式是按照以下命令步骤: 1.启动mfsmaster进 ...
- the install of mysql in Linux System
一.下载MySql 浏览器打开 https://www.mysql.com/downloads/mysql/#downloads 下载 我下载的版本是Red Hat 5 版本的 https://www ...
- 解决在Python中使用Win32api报错的问题,No module named win32api
一.系统环境 操作系统: Win7 64位 Python:3.7.0 二.在使用import win32api时,报错:No module named win32api 网上查到有下面解决办法: 方法 ...
- 【Leetcode009】Palindrome Number
问题链接:https://leetcode.com/problems/palindrome-number/#/description Question:Determine whether an int ...
- stuff for xml path
SumOrg=stuff((select '/'+User_Org from V_RubricInfoRefer t where t.RubricID=V_RubricInfoRefer.Rubric ...
- Arduino I2C + DS1307实时时钟
主要特性 DS1307是Maxim的串行.I2C实时时钟芯片.主要特性有: 工作电压:主电源电压4.5~5.5V,电池电压2.0~3.5V 功耗:电池供电.备份模式时<500nA 接口:I2C, ...
- Go语言最佳实践——面向对象
对于接口,应使用组合而非继承的方式扩展: 对于结构体,应定义独立的结构体,而非用嵌套模拟继承. 值接收者和指针接收者: 1.对于不可变类型创建只接受值接收者的方法,而为可变的类型创建接受指针接收者的方 ...
- select2的搜索框不能输入搜索内容
按照select2官网配置完后,搜索框弹出后无法输入内容,究竟怎么回事,于是在其他页面尝试了select2,发现可以啊,为什么在这个地方不可以,终于找到了造成这个问题的不同之处:select2在模态对 ...