Nginx 反向代理背后的 Portainer docker 容器

Geo*_*ght 3 server nginx https reverse-proxy letsencrypt

我已经在 Ubuntu 22.04 服务器上设置了 nginx 反向代理,并且已成功从 Lets Encrypt 获取 ssl 证书。两个密钥存储在这里:

/etc/letsencrypt/live/test.ddns.net/fullchain.pem;
/etc/letsencrypt/live/test.ddns.net/privkey.pem
Run Code Online (Sandbox Code Playgroud)

在我的默认 nginx 配置中,我有两个路径helloworldportainer。两条路径都重定向到 docker 容器。

server {
    listen 80;
    listen [::]:80;
    server_name test.ddns.net;
    return 301 https://$host$request_uri;
}
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name test.ddns.net;
    ssl_certificate /etc/letsencrypt/live/test.ddns.net/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/test.ddns.net/privkey.pem;

    location /helloworld {
        proxy_pass http://localhost:32768;
    }

    location /portainer {
        proxy_pass http://localhost:9000;
    }
Run Code Online (Sandbox Code Playgroud)

helloworld 路径按预期工作,并使用 https 协议加载 html 页面(因此我的 ngnix 配置是正确的)。但portainer路径不是。我尝试了端口 9000、端口 8000、端口 9443。但都不起作用。我收到 404 错误,或者通过 http 发送的请求,而预期是 https,或者其他错误。有没有人有相同的设置并且能够帮助我?

谢谢。

小智 5

关于您提到的尝试的端口:
  • 8000Portainer Edge Agent 使用端口来远程管理边缘设备及其服务并与之交互。由于您没有提到任何有关边缘计算的内容,因此您的用例不需要此端口。

  • Port9000用于建立与 Portainer Web UI 的连接,但现在仅因遗留原因才可用,强烈建议避免使用它。

  • httpsPortainer于 2021 年中期添加了支持,使 port成为安全连接到 Portainer Web UI 的推荐9443选项。因此,您应该使用它。

关于您的 Nginx 配置:

你已经足够接近了,但为了让 Portainer 在子路径上工作(就像你想要的那样),你应该在和字段中添加一个尾部斜杠,如下所示:locationproxy_pass

location /portainer/ {
     proxy_pass https://localhost:9443/;
}
Run Code Online (Sandbox Code Playgroud)

使用命令重新启动 Nginx 后sudo systemctl restart nginx,404 错误应该得到解决。