无法访问托管在 LXD 容器中的 Kestrel + NGINX(代理)网站

Cannot Reach Kestrel + NGINX (proxy) Website Hosted in LXD Container

我尝试在 LXD 容器中托管我的 ASPNet Core web 以及作为反向代理服务器的 NGINX,但无济于事。

当我通过 dotnet <app.dll> 命令使用 Kestrel 运行 时,浏览器 (Firefox) returns Unable to Connect。但是,当我没有应用程序 运行,在这种情况下,只有 NGINX 服务,浏览器 returns 标准 NGINX 502 Bad Gateway。

我开始想,问题可能出在Kestrel身上。但是,据我了解,无论何时我们使用反向代理,都不需要为外部访问配置 Kestrel。

但是,当我将网站托管在 Linux 虚拟机(而非容器)中时,我设法访问了该网站。我也使用了相同的配置。是否缺少在 LXD 容器上启用此功能的配置?

以下是我的 NGINX 服务器配置:

    server {
       listen 80;
       location / {
          proxy_pass http://localhost:5000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection keep-alive;
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
        }
    }

我想知道损坏的部分是在 NGINX、Kestrel 还是 LXD 上。我应该怎么办?我可以从外面 ping 容器就好了。此外,其他容器也无法访问容器中托管的网站。

不知何故,您需要向 Kestrel 提供允许的 url,即使它使用 NGINX 作为反向代理。这只发生在 LXD 容器中;这意味着 Kestrel 和 NGINX 托管在 LXD 容器中。

这是解决方案。我在命令行中添加了环境变量,瞧,它起作用了。

ASPNETCORE_URLS=http://0.0.0.0:80 dotnet <your_app.dll>