Nginx
Nginx 代理/上游重定向使用錯誤的埠和協議
我正在使用 NGINX 伺服器作為其他 NGINX 伺服器的 ssl 代理。不幸的是,如果請求被上游伺服器重定向,則位置欄位包含錯誤的目標埠。
curl -v "https://example.com/site"
:> GET /site HTTP/2 > Host: example.com > User-Agent: curl/7.58.0 > Accept: */* > * Connection state changed (MAX_CONCURRENT_STREAMS updated)! < HTTP/2 301 < server: nginx < date: Sun, 18 Mar 2018 20:01:44 GMT < content-type: text/html < content-length: 178 < location: http://example.com:9101/site/ < strict-transport-security: max-age=15768000; includeSubDomains
NGINX 代理:
# Proxy: example.com #################### server { listen 0.0.0.0:80; listen [::]:80; server_name example.com; root /srv/www/_empty; location / { return 301 https://example.com$request_uri; } } upstream myupstream { server [::1]:9101; } server { listen 0.0.0.0:443 ssl http2; listen [::]:443 ssl http2; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; root /srv/www/_empty; location / { proxy_set_header Host $http_host; 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-Frame-Options ""; proxy_set_header X-Content-Type-Options ""; proxy_pass http://myupstream; } }
NGINX 上游伺服器:
# NGINX Site: example.com ######################### server { # Server listen [::1]:9101; server_name example.com; # Root root /srv/www/example.com/staging; # Disable Caching sendfile off; # Charset charset utf-8; # Default location / { index index.html; try_files $uri $uri/ =404; } }
兩個 NGINX 實例在同一台機器上作為單獨的主程序執行。其他任何東西都可以完美執行。
我想,NGINX 代理會重寫上游響應,以便將其轉換
http://example.com:9001/site/
為https://example.com/site/
.我忘記了什麼?
proxy_redirect default
不會按您期望的方式工作,因為您使用的是upstream
塊並且nginx
無法從proxy_pass
語句中獲取所需的字元串。您可以使用以下命令明確指定它:proxy_redirect http://example.com:9101/ /;
有關更多資訊,請參閱此文件。
或者,告訴您的上游伺服器停止在其重定向響應中指定埠:
port_in_redirect off;
有關更多資訊,請參閱此文件。