Nginx?location和proxy_pass配置示例詳解
概述
Nginx 配置中 location
和 proxy_pass
指令的不同組合方式及其對請求轉(zhuǎn)發(fā)路徑的影響。
配置效果
1. location 和 proxy_pass 都帶斜杠 /
location /api/ { proxy_pass http://127.0.0.1:8080/; }
- 訪問地址:
www.hw.com/api/upload
- 轉(zhuǎn)發(fā)地址:
http://127.0.0.1:8080/upload
轉(zhuǎn)發(fā)地址不帶 location 匹配目錄 /api/
2. location 不帶斜杠,proxy_pass 帶斜杠 /
location /api { proxy_pass http://127.0.0.1:8080/; }
- 訪問地址:
www.hw.com/api/upload
- 轉(zhuǎn)發(fā)地址:
http://127.0.0.1:8080//upload
轉(zhuǎn)發(fā)地址會多帶 /
3. location 帶斜杠,proxy_pass 不帶斜杠
location /api/ { proxy_pass http://127.0.0.1:8080; }
- 訪問地址:
www.hw.com/api/upload
- 轉(zhuǎn)發(fā)地址:
http://127.0.0.1:8080/api/upload
轉(zhuǎn)發(fā)地址會帶 location 匹配目錄 /api/
4. location 和 proxy_pass 都不帶斜杠
location /api { proxy_pass http://127.0.0.1:8080; }
- 訪問地址:
www.hw.com/api/upload
- 轉(zhuǎn)發(fā)地址:
http://127.0.0.1:8080/api/upload
轉(zhuǎn)發(fā)地址會帶 location 匹配目錄 /api/
5. location 和 proxy_pass 都帶斜杠 /,但 proxy_pass 帶地址
location /api/ { proxy_pass http://127.0.0.1:8080/server/; }
- 訪問地址:
www.hw.com/api/upload
- 轉(zhuǎn)發(fā)地址:
http://127.0.0.1:8080/server/upload
轉(zhuǎn)發(fā)地址不帶 location 匹配目錄 /api/
6. location 不帶斜杠,proxy_pass 帶斜杠 /,但 proxy_pass 帶地址
location /api { proxy_pass http://127.0.0.1:8080/server/; }
- 訪問地址:
www.hw.com/api/upload
- 轉(zhuǎn)發(fā)地址:
http://127.0.0.1:8080/server//upload
轉(zhuǎn)發(fā)地址不帶 location 匹配目錄 /api/ ,會多帶 /
7. location 帶斜杠,proxy_pass 不帶斜杠,但 proxy_pass 帶地址
location /api/ { proxy_pass http://127.0.0.1:8080/server; }
- 訪問地址:
www.hw.com/api/upload
- 轉(zhuǎn)發(fā)地址:
http://127.0.0.1:8080/serverupload
轉(zhuǎn)發(fā)地址不帶 location 匹配目錄 /api/ 直接進(jìn)行了替換
8. location 和 proxy_pass 都不帶斜杠,但 proxy_pass 帶地址
location /api { proxy_pass http://127.0.0.1:8080/server; }
- 訪問地址:
www.hw.com/api/upload
- 轉(zhuǎn)發(fā)地址:
http://127.0.0.1:8080/server/upload
轉(zhuǎn)發(fā)地址不帶 location 匹配目錄 /api
總結(jié)
- 當(dāng)
proxy_pass
代理地址端口后有目錄(包括/
),轉(zhuǎn)發(fā)后地址為:代理地址 + 訪問 URL 目錄部分去除location
匹配目錄。 - 當(dāng)
proxy_pass
代理地址端口后無任何內(nèi)容,轉(zhuǎn)發(fā)后地址為:代理地址 + 訪問 URL 目錄部分(包括location
地址)。
場景示例
upstream backend_name_hw { server 10.10.10.10:32323 max_fails=2 fail_timeout=2; } server { listen 80; server_name hw.test.com; client_max_body_size 1024m; client_body_timeout 12; client_header_timeout 12; keepalive_timeout 15; send_timeout 10; location / { proxy_pass http://backend_name_hw; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /hello { proxy_pass http://backend_name_hw/hello; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
location /hw/ { proxy_pass http://hw-nginx/index.html; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
location /hwhw/ { proxy_pass http://hw-nginx/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
location /hw/hi/ { proxy_pass http://hw-nginx/hello/index.html; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
location /hello/index.html { proxy_pass http://hw-nginx; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
到此這篇關(guān)于Nginx location和proxy_pass配置的文章就介紹到這了,更多相關(guān)Nginx location和proxy_pass配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Nginx中l(wèi)ocation proxy_pass加與不加/的區(qū)別說明
- Nginx中proxy_pass的斜杠的兩種方式
- Nginx的location路徑與proxy_pass匹配規(guī)則說明
- Nginx捕獲并自定義proxy_pass返回的錯誤問題
- nginx中如何配置proxy_pass
- Nginx rewrite和proxy_pass的區(qū)別及說明
- Nginx proxy_pass如何到https后端
- nginx代理參數(shù)proxy_pass的實(shí)現(xiàn)
- nginx反向代理proxy_pass遇到的死循環(huán)問題
- 解決nginx配置proxy_pass之后,響應(yīng)變慢的問題
- Nginx使用if指令實(shí)現(xiàn)多個proxy_pass方式
- Nginx中proxy_pass使用小結(jié)
相關(guān)文章
使用Nginx反向代理與proxy_cache緩存搭建CDN服務(wù)器的配置方法
linux下通過Nginx反向代理和proxy_cache緩存搭建CDN服務(wù)器加快Web訪問速度的配置方法2013-06-06nginx封空user_agent實(shí)現(xiàn)封禁迅雷的方法
nginx封空user_agent實(shí)現(xiàn)封禁迅雷的方法,需要的朋友可以參考下。2010-11-11windows查看nginx是否啟動及常用命令小結(jié)
這篇文章主要給大家介紹了關(guān)于windows查看nginx是否啟動及常用命令的相關(guān)資料,在Windows系統(tǒng)中,可以使用以下命令來操作和管理Nginx,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06nginx實(shí)現(xiàn)負(fù)載均衡和動靜分離
這篇文章主要為大家詳細(xì)介紹了nginx實(shí)現(xiàn)負(fù)載均衡和動靜分離,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03ELK與Grafana聯(lián)合打造可視化監(jiān)控來分析nginx日志
這篇文章主要為大家介紹了ELK與Grafana的聯(lián)合打造可視化監(jiān)控來分析nginx日志,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03Nginx生產(chǎn)環(huán)境平滑升級的實(shí)現(xiàn)
本文主要介紹了Nginx生產(chǎn)環(huán)境平滑升級的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03