Nginx 反向代理自动重定向到错误端口

Nginx 反向代理自动重定向到错误端口

我正在尝试使用 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?

相关内容