日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nginx http proxy 反向代理

發布時間:2024/9/19 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx http proxy 反向代理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 前言
  • proxy配置
    • 全部追加到url中
    • 部分追加到url中
  • 多次代理配置
  • 以某個路徑開頭
  • weblogic:https請求轉成http請求
  • tomcat:https請求轉成http請求
  • nginx多次代理:(tomcat中)scheme或https丟失問題
  • nginx多次代理:客戶端真實IP丟失 問題

前言

  • nginx 1.14.2

proxy配置

location / {proxy_pass http://127.0.0.1:8080/;proxy_set_header Host $host:$server_port;proxy_set_header Remote_Addr $remote_addr;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Nginx-Proxy true;# index頁面設置index index;}
  • proxy_set_header Host $host:$server_port;:告知后端,客戶端請求的真實host和port。
  • proxy_set_header X-Real-IP $remote_addr;:真實IP,即客戶端的IP。需要用ngx_http_realip_module 一起使用。
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;:代理走過的ip路徑。X-Forwarded-For的值可能為:218.107.55.254, 192.168.0.3, 192.168.0.99, ...,對應Client IP, Proxy1 IP, Proxy2 IP, ...

全部追加到url中

location [location表達式] {proxy_pass [proxy_pass表達式];....}

當proxy_pass 表達式中端口號后面不包含/字符時,location匹配的部分全部追加到proxy_pass表達式后面。

location / {proxy_pass http://127.0.0.1:8080;....} request urllocation 表達式匹配的部分proxy_pass 表達式proxy 后的 url是否符合預期
http://127.0.0.1/logo.png//logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/logo.png符合預期
http://127.0.0.1/images/logo.png//images/logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/images/logo.png符合預期
http://127.0.0.1/user/add//user/addhttp://127.0.0.1:8080http://127.0.0.1:8080/user/add符合預期
http://127.0.0.1/oa/logo.png/oa//oa/logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/oa/logo.png符合預期
http://127.0.0.1/oa/images/logo.png/oa//oa/images/logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/oa/images/logo.png符合預期
http://127.0.0.1/oa/user/add/oa//oa/user/addhttp://127.0.0.1:8080http://127.0.0.1:8080/oa/user/add符合預期
http://127.0.0.1/oa/logo.png/oa/oa/logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/oa/logo.png符合預期
http://127.0.0.1/oa/images/logo.png/oa/oa/images/logo.pnghttp://127.0.0.1:8080http://127.0.0.1:8080/oa/images/logo.png符合預期
http://127.0.0.1/oa/user/add/oa/oa/user/addhttp://127.0.0.1:8080http://127.0.0.1:8080/oa/user/add符合預期
http://127.0.0.1/oauser/add/oa/oauser/addhttp://127.0.0.1:8080http://127.0.0.1:8080/oauser/add符合預期

注:按照使用代理方式將url中的端口號去掉進行評判,比如將http://127.0.0.1/logo.png代理成ttp://127.0.0.1:8080/logo.png。

proxy_pass 表達式中端口號后面不包含/字符時:

  • location 表達式是否以/結尾均可。

部分追加到url中

當proxy_pass 表達式中端口號后面包含/字符時,對location匹配的部分進行截取操作,將截取所得的部分追加到proxy_pass表達式后面。

location / {proxy_pass http://127.0.0.1:8080/oa/;....} request urllocation表達式截取所得的部分proxy_pass 表達式proxy 后的 url是否符合預期
http://127.0.0.1/logo.png/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/logo.png符合預期
http://127.0.0.1/images/logo.png/images/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/images/logo.png符合預期
http://127.0.0.1/user/add/user/addhttp://127.0.0.1:8080/http://127.0.0.1:8080/user/add符合預期
http://127.0.0.1/logo.png/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/logo.png不符合預期
http://127.0.0.1/images/logo.png/images/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/images/logo.png不符合預期
http://127.0.0.1/user/add/user/addhttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/user/add不符合預期
http://127.0.0.1/logo.png/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oalogo.png不符合預期
http://127.0.0.1/images/logo.png/images/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oa/imageslogo.png不符合預期
http://127.0.0.1/user/add/user/addhttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oa/useradd不符合預期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/oa/logo.png符合預期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/oa/images/logo.png符合預期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/http://127.0.0.1:8080/oa/user/add符合預期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/logo.png不符合預期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/http://127.0.0.1:8080/images/logo.png不符合預期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/http://127.0.0.1:8080/user/add不符合預期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/logo.png符合預期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/images/logo.png符合預期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/user/add符合預期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oalogo.png不符合預期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oaimages/logo.png不符合預期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oauser/add不符合預期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa//logo.png符合預期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa//images/logo.png符合預期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa//user/add符合預期
http://127.0.0.1/oauser/add/oauser/addhttp://127.0.0.1:8080/oa/http://127.0.0.1:8080/oa/user/add不符合預期
http://127.0.0.1/oa/logo.png/oa/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oa/logo.png符合預期
http://127.0.0.1/oa/images/logo.png/oa/images/logo.pnghttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oa/images/logo.png符合預期
http://127.0.0.1/oa/user/add/oa/user/addhttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oa/user/add符合預期
http://127.0.0.1/oauser/add/oauser/addhttp://127.0.0.1:8080/oahttp://127.0.0.1:8080/oauser/add符合預期

注:按照使用代理方式將url中的端口號去掉進行評判,比如將http://127.0.0.1/logo.png代理成ttp://127.0.0.1:8080/logo.png。

proxy_pass 表達式中端口號后面包含/字符時:

  • proxy_pass 表達式以/結尾時,location 表達式也應以/結尾
  • proxy_pass 表達式不以/結尾時,location 表達式也不應以/結尾

多次代理配置

location / {proxy_pass http://127.0.0.1:8081;proxy_set_header Host $host:$server_port;proxy_set_header Remote_Addr $remote_addr;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;set $xscheme $scheme;if ( $http_x_nginx_proxy ) {set $xscheme $http_x_forwarded_proto;}proxy_set_header X-Forwarded-Proto $xscheme;proxy_set_header X-Nginx-Proxy true;index index; }

以某個路徑開頭

location ^~ /api {...}

或者

location ~* ^/api {...}

weblogic:https請求轉成http請求

location / {...proxy_set_header WL-Proxy-SSL true; ...}
  • proxy_set_header WL-Proxy-SSL true; https請求轉成http請求時,weblogic設置

tomcat:https請求轉成http請求

location / {...proxy_set_header X-Forwarded-Proto $scheme;...}
  • proxy_set_header X-Forwarded-Proto $scheme; https請求轉成http請求時,tomcat設置

nginx多次代理:(tomcat中)scheme或https丟失問題

  • 在第1層代理上設置X-Forwarded-Proto
location / {...proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Nginx-Proxy true;...}
  • 在第2層代理上再次設置X-Forwarded-Proto
location / {...set $xscheme $scheme;if ( $http_x_nginx_proxy ) {set $xscheme $http_x_forwarded_proto;}proxy_set_header X-Forwarded-Proto $xscheme;...}
  • 在第3層及更多層,參考第2層的設置

nginx多次代理:客戶端真實IP丟失 問題

  • 在第1層代理上設置X-Real-IP
location / {...proxy_set_header X-Real-IP $remote_addr;...}
  • 在第2層代理上再次設置X-Real-IP
location / {...proxy_set_header X-Forwarded-Proto $http_x_real_ip;...}
  • 在第3層及更多層,參考第2層的設置

總結

以上是生活随笔為你收集整理的nginx http proxy 反向代理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。