
我正在嘗試使用 nginx 設定 docker 容器的反向代理。
docker 容器可用並在連接埠 8000 上運作。
我希望能夠透過地址 mydomain.com:80/mycontainer 存取容器。
反向代理似乎成功與容器通信,但是當容器請求重定向到其登錄頁面時,nginx 嘗試在連接埠 80 而不是 8000 上載入登入頁面,但失敗了。
到目前為止,這是我的 nginx 配置:
upstream docker-container {
server 127.0.0.1:8000;
}
server {
listen 80;
location /mycontainer {
proxy_redirect off;
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-Host $server_name;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://docker-container;
}
}
這是我跑步時得到的結果wget -S 127.0.0.1/mycontainer
:
wget -S 127.0.0.1/mycontainer
--2021-08-29 20:30:12-- http://127.0.0.1/mycontainer
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 302 Found
Server: nginx/1.18.0 (Ubuntu)
Date: Sun, 29 Aug 2021 20:30:12 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 28
Connection: keep-alive
Content-Language: en
X-Frame-Options: SAMEORIGIN
X-Download-Options: noopen
X-Content-Type-Options: nosniff
Referrer-Policy: origin-when-cross-origin
X-XSS-Protection: 1; mode=block
Location: /login
Vary: Accept
Set-Cookie: some cookie
Location: /login [following]
--2021-08-29 20:30:12-- http://127.0.0.1/login <--- HERE IS THE PROBLEM. Should be 127.0.0.1:8000/login
Reusing existing connection to 127.0.0.1:80.
HTTP request sent, awaiting response...
HTTP/1.1 404 Not Found
Server: nginx/1.18.0 (Ubuntu)
Date: Sun, 29 Aug 2021 20:30:12 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
2021-08-29 20:30:12 ERROR 404: Not Found.
我絕不是這方面的專業人士,所以我確信其中有很多錯誤。
如何讓自動重定向指向正確的連接埠 (8000) 而不是連接埠 80?