You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

nginx-proxy环境下.NET 8站点报502网关错误的最简解决方法咨询

解决nginx-proxy下.NET 8站点502 Bad Gateway错误的最简方法

问题背景

我通过nginx-proxy和acme-companion部署两个.NET站点:

  • net7site(基于.NET 7):容器内监听80、443端口,访问正常
  • net8site(基于.NET 8):容器内监听8080、8081端口,访问时出现502 Bad Gateway错误

当前使用的Docker配置命令:

docker stop nginx-proxy
docker rm nginx-proxy
docker run --detach \
    --name nginx-proxy \
    --publish 80:80 \
    --publish 443:443 \
    --volume certs:/etc/nginx/certs \
    --volume vhost:/etc/nginx/vhost.d \
    --volume html:/usr/share/nginx/html \
    --volume /var/run/docker.sock:/tmp/docker.sock:ro \
    nginxproxy/nginx-proxy:1.6-alpine

docker stop nginx-proxy-acme
docker rm nginx-proxy-acme
docker run --detach \
    --name nginx-proxy-acme \
    --volumes-from nginx-proxy \
    --volume /var/run/docker.sock:/var/run/docker.sock:ro \
    --volume acme:/etc/acme.sh \
    --env "DEFAULT_EMAIL=alibaba@alibaba.com" \
    nginxproxy/acme-companion:2.4

docker stop net7site
docker rm net7site
docker run --detach \
    --name net7site \
    --env VIRTUAL_HOST=net7site.com,www.net7site.com \
    --env LETSENCRYPT_HOST=net7site.com,www.net7site.com \
    --expose 80 \
    --expose 443 \
    net7site

docker stop net8site
docker rm net8site
docker run --detach \
    --name net8site \
    --env VIRTUAL_HOST=net8site.com,www.net8site.com \
    --env LETSENCRYPT_HOST=net8site.com,www.net8site.com \
    --expose 80 \
    --expose 443 \
    net8site

最简解决方法

问题根源

nginx-proxy默认会将请求转发到容器暴露的80/443端口,但net8site实际监听的是8080/8081,端口不匹配导致转发失败,触发502错误。

修正后的net8site启动命令

只需添加VIRTUAL_PORT环境变量指定容器内实际服务端口,并修正--expose参数暴露正确端口:

docker stop net8site
docker rm net8site
docker run --detach \
    --name net8site \
    --env VIRTUAL_HOST=net8site.com,www.net8site.com \
    --env LETSENCRYPT_HOST=net8site.com,www.net8site.com \
    --env VIRTUAL_PORT=8080 \  # 指定容器内HTTP服务端口
    --expose 8080 \  # 暴露实际监听的HTTP端口
    --expose 8081 \  # 暴露实际监听的HTTPS端口(如果需要)
    net8site

说明

  • VIRTUAL_PORT:告诉nginx-proxy将外部请求转发到容器的8080端口(若你的.NET 8站点在8081提供HTTPS服务,可根据需求调整该值)
  • --expose:修正为容器实际监听的端口,确保nginx-proxy能正确识别容器内的服务端口

内容的提问来源于stack exchange,提问作者Cyberdrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 10:53:10