Nginx location 和 proxy_pass路徑配置問題小結(jié)
本文是基于 location 的匹配末尾是否配置 /
和 proxy_pass 末尾是否配置 /
,進(jìn)行測試,完全還原了整個測試過程。幫助了解具體的情況。
一、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 可以運行
該腳本用于獲取請求內(nèi)容。 這個作為后端,也就是 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
后端內(nèi)容
打印的內(nèi)容:
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 -
小結(jié)論: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
后端內(nèi)容
打印的內(nèi)容:
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 -
小結(jié)論: 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
后端內(nèi)容
打印的內(nèi)容:
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
后端內(nèi)容
打印的內(nèi)容:
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
后端內(nèi)容
打印的內(nèi)容:
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
后端內(nèi)容
打印的內(nèi)容:
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
三、總結(jié)
序號 | 訪問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 腳本對應(yīng)的web服務(wù)。
在日常的web網(wǎng)站部署中,經(jīng)常會用到 nginx
的 proxy_pass
反向代理,有一個配置需要弄清楚:配置 proxy_pass
時,
- 當(dāng)在后面的
upstram_name
后面出現(xiàn)了/
,相當(dāng)于是絕對根路徑,則nginx
不會把location
中匹配的路徑部分代理走; - 如果沒有
/
,則會把匹配的路徑部分也給代理走。
到此這篇關(guān)于Nginx location 和 proxy_pass路徑配置詳解的文章就介紹到這了,更多相關(guān)Nginx location 和 proxy_pass路徑配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx實現(xiàn)http自動跳轉(zhuǎn)到https
本文主要介紹了Nginx實現(xiàn)http自動跳轉(zhuǎn)到https,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01使用nginx配置基于域名的虛擬主機(jī)實現(xiàn)
這篇文章主要介紹了nginx配置基于域名的虛擬主機(jī)實現(xiàn)​,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10nginx配置location總結(jié)location正則寫法及rewrite規(guī)則寫法
本文詳細(xì)講述了Nginx location正則寫法,Nginx 的Rewrite規(guī)則以及Nginx.conf中if指令與全局變量2018-10-10nginx找到默認(rèn)根目錄(root?html)的方法
這篇文章主要給大家介紹了nginx如何找到默認(rèn)根目錄(root?html),文中給出詳細(xì)的解決方法,通過代碼示例講解的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2023-11-11shell腳本定時統(tǒng)計Nginx下access.log的PV并發(fā)送給API保存到數(shù)據(jù)庫
這篇文章主要介紹了shell腳本定時統(tǒng)計Nginx下access.log的PV并發(fā)送給API保存到數(shù)據(jù)庫的實現(xiàn)方法 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09