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

如何在docker-compose中使用nginx镜像转发多个项目的服务端口

问题原因

你nginx-proxy配置中的proxy_pass指向的是容器自身的localhost,而非宿主机的端口,nginx-proxy容器内部并没有运行监听8080/8081的服务,所以请求转发失败。
由于三个容器已经加入了同一个Docker网桥nginx-net,可以直接通过Docker内置的DNS用容器名访问其他服务,不需要走宿主机端口映射。


修复步骤

  1. 修改nginx-proxy目录下的nginx.conf,替换proxy_pass地址为对应服务的容器名+容器内部端口:
# nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  300;
    server {
      listen       80;
      server_name  localhost;
      location / {
        proxy_pass  http://test-nginx1:80;
      }
    }
    server {
      listen       81;
      server_name  localhost;
      location / {
        proxy_pass  http://test-nginx2:80;
      }
    }
}
  1. 重启nginx-proxy容器生效:
docker-compose restart test-nginx-proxy

可选优化

如果不需要直接通过localhost:8080和localhost:8081访问两个站点,可以删除nginx1和nginx2的docker-compose.yml中的ports配置,仅通过内部网络通信即可,降低端口暴露风险。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 16:45:03