亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Nginx?location和proxy_pass配置示例詳解

 更新時(shí)間:2024年11月09日 11:18:33   作者:_Johnny_  
這篇文章主要介紹了Nginx?location和proxy_pass配置的相關(guān)資料,本文詳細(xì)探討了Nginx配置中`location`和`proxy_pass`指令的不同組合方式及其對(duì)請求轉(zhuǎn)發(fā)路徑的影響,通過列舉多種組合,展示了`location`匹配目錄與`proxy_pass`地址路徑如何相互作用,需要的朋友可以參考下

概述

Nginx 配置中 location 和 proxy_pass 指令的不同組合方式及其對(duì)請求轉(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ā)地址會(huì)多帶 /

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ā)地址會(huì)帶 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ā)地址會(huì)帶 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/ ,會(huì)多帶 /

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論