請教如何通過反向代理請求路徑時加入查詢引數?呢?

NGINX daimaosix • at 2021-02-23 16:07:14 • 5 Views

業務需要通過 Nginx 反向代理 Get 請求後端介面,但發現反向代理伺服器訪問路徑中無法新增查詢引數?

原配置

  location /net/comm/ {
    proxy_pass http://localhost:9000;
    proxy_set_header Host localhost;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

這樣可以通過訪問 http://a.com/net/comm/127.0.0.1 獲取資料

但需要將請求路徑調整為 http://a.com/net/comm/?ip=127.0.0.1 獲取資料時,調整了反代規則:

  location /net/comm/?ip= {
    proxy_pass http://localhost:9000;
    proxy_set_header Host localhost;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

此時,反向代理伺服器會報 404 錯誤。接著又進行了調整:

  location ^~ /net/comm/ {
    proxy_pass http://localhost:9000;
    proxy_set_header Host localhost;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

此時,介面會返回 404 錯誤。

麻煩 V 友給看看,需要怎麼調整才可以實現帶查詢引數訪問呢?將請求路徑 http://a.com/net/comm/127.0.0.1 調整為 http://a.com/net/comm/?ip=127.0.0.1

Total: 16
Add a reply
For Commenting you need to Login. If you dont have a Account you need to Register.