Nginx location 和 proxy_pass路径配置问题小结
本文是基于 location 的匹配末尾是否配置 /
和 proxy_pass 末尾是否配置 /
,进行测试,完全还原了整个测试过程。帮助了解具体的情况。
一、nginx location 基本配置
1.1、nginx 配置文件
upstream test1{ server 127.0.0.1:8000; } upstream test2{ server 127.0.0.1:8000; } server{ server_name test.com; listen 80; access_log /usr/local/openresty/nginx/logs/test.com_access.log latest; error_log /usr/local/openresty/nginx/logs/test.com.log error; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_connect_timeout 3s; proxy_read_timeout 120s; proxy_send_timeout 120s; proxy_next_upstream error timeout invalid_header http_404 http_502 http_504 http_500; location /user/ { proxy_set_header connection ""; proxy_http_version 1.1; proxy_pass http://test1/; } location / { proxy_set_header connection ""; proxy_http_version 1.1; proxy_pass http://test2/; } }
1.2 、python 脚本
python2 可以运行
该脚本用于获取请求内容。 这个作为后端,也就是 proxy_pass 代理的后端。
#!/usr/bin/env python import simplehttpserver import socketserver port = 8000 class gethandler(simplehttpserver.simplehttprequesthandler): def do_get(self): print(self.headers) self.send_response(200, "") def do_post(self): print(self.headers) content_length = self.headers.getheaders('content-length') length = int(content_length[0]) if content_length else 0 print(self.rfile.read(length)) self.send_response(200, "") handler = gethandler httpd = socketserver.tcpserver(("", port), handler) httpd.serve_forever()
二、测试
2.1、测试 location
末尾存在 /
和 proxy_pass末尾存在 /
nginx配置如下
location /user/ { proxy_set_header connection ""; proxy_http_version 1.1; proxy_pass http://test1/; }
请求url
test.com/user/test.html
后端内容
打印的内容:
host: test1 content-length: 0 user-agent: postmanruntime/7.26.8 accept: */* postman-token: f2bfe770-4f44-4ee9-91c4-060f59dfb26c accept-encoding: gzip, deflate, br 127.0.0.1 - - [10/apr/2021 16:54:26] "post /test.html http/1.1" 200 -
小结论:proxy_pass 地址加了 /
的话, 请求 test.com/user/test.html
实际请求是 http://test1/test.html
。
2.2、测试 location
末尾存在 /
和 proxy_pass末尾不存在 /
nginx配置如下
location /user/ { proxy_set_header connection ""; proxy_http_version 1.1; proxy_pass http://test1; }
请求url
test.com/user/test.html
后端内容
打印的内容:
host: test1
content-length: 0
user-agent: postmanruntime/7.26.8
accept: */*
postman-token: e33d0a2c-1965-4152-b87c-94fca50f2899
accept-encoding: gzip, deflate, br127.0.0.1 - - [10/apr/2021 16:57:18] "post /user/test.html http/1.1" 200 -
小结论: proxy_pass 地址不加了 /
的话, 请求 test.com/user/test.html
实际请求是 http://test1/user/test.html
2.3、测试三 location
不加末尾 /
且 proxy_pass 不加 末尾 /
nginx配置如下
location /user { proxy_set_header connection ""; proxy_http_version 1.1; proxy_pass http://test1; }
请求url
test.com/user/test.html
后端内容
打印的内容:
host: test1
content-length: 0
user-agent: postmanruntime/7.26.8
accept: */*
postman-token: 31cd33c6-4c95-41b5-a095-28cdc7113dcd
accept-encoding: gzip, deflate, br127.0.0.1 - - [10/apr/2021 16:59:34] "post /user/test.html http/1.1" 200 -
请求 test.com/user/test.html
实际请求是 http://test1/user/test.html
2.4、location 不加
末尾 /
且 proxy_pass 加 末尾 /
nginx配置如下
location /user { proxy_set_header connection ""; proxy_http_version 1.1; proxy_pass http://test1/; }
请求url
test.com/user/test.html
后端内容
打印的内容:
host: test1
content-length: 0
user-agent: postmanruntime/7.26.8
accept: */*
postman-token: d0f4b83f-6482-41ba-8a01-c059eececc2d
accept-encoding: gzip, deflate, br127.0.0.1 - - [10/apr/2021 17:00:21] "post //test.html http/1.1" 200 -
请求 test.com/user/test.html
实际请求是 http://test1//test.html
2.5、location 末尾
有 /
proxy_pass 末尾其他有路径,且末尾加 /
nginx配置如下
location /user/ { proxy_set_header connection ""; proxy_http_version 1.1; proxy_pass http://test1/haha/; }
请求url
test.com/user/test.html
后端内容
打印的内容:
host: test1
content-length: 0
user-agent: postmanruntime/7.26.8
accept: */*
postman-token: 6447cf0b-5988-4f96-81a4-2b621fe32604
accept-encoding: gzip, deflate, br127.0.0.1 - - [10/apr/2021 17:03:27] "post /haha/test.html http/1.1" 200 -
请求 test.com/user/test.html
实际请求是 http://test1/haha/test.html
2.6、 location 末尾
有 /
proxy_pass 末尾其他有路径,且末尾不加 /
nginx配置如下
location /user/ { proxy_set_header connection ""; proxy_http_version 1.1; proxy_pass http://test1/haha; }
请求url
test.com/user/test.html
后端内容
打印的内容:
host: test1
content-length: 0
user-agent: postmanruntime/7.26.8
accept: */*
postman-token: 32fb2a50-1e7c-4131-9804-1828e21ca841
accept-encoding: gzip, deflate, br127.0.0.1 - - [10/apr/2021 17:05:03] "post /hahatest.html http/1.1" 200 -
请求 test.com/user/test.html
实际请求是 http://test1/hahatest.html
三、总结
序号 | 访问url | location配置 | proxy_pass配置 | 后端接收的请求 | 备注 |
---|---|---|---|---|---|
1 | test.com/user/test.html | /user/ | http://test1/ | /test.html | |
2 | test.com/user/test.html | /user/ | http://test1 | /user/test.html | |
3 | test.com/user/test.html | /user | http://test1 | /user/test.html | |
4 | test.com/user/test.html | /user | http://test1/ | //test.html | |
5 | test.com/user/test.html | /user/ | http://test1/haha/ | /haha/test.html | |
6 | test.com/user/test.html | /user/ | http://test1/haha | /hahatest.html |
注意上表格中的后端是指 python 脚本对应的web服务。
在日常的web网站部署中,经常会用到 nginx
的 proxy_pass
反向代理,有一个配置需要弄清楚:配置 proxy_pass
时,
- 当在后面的
upstram_name
后面出现了/
,相当于是绝对根路径,则nginx
不会把location
中匹配的路径部分代理走; - 如果没有
/
,则会把匹配的路径部分也给代理走。
到此这篇关于nginx location 和 proxy_pass路径配置详解的文章就介绍到这了,更多相关nginx location 和 proxy_pass路径配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!