nginx docker 不重定向 gogs docker 容器

nginx docker does not redirect gogs docker container

我是 docker 网络和 nginx 的新手,但请尝试“dockerize”本地开发服务器上的所有内容。用于测试带有 nginx 的 docker 图像,它应该将另一个容器(gogs)从端口 3000 重定向到特定的 url 和端口 80。我想要反向代理配置和 docker图片“分开”,每个“应用程序”都有自己的 docker-compose 文件。

所以我应该 http://app.test.local 安装 gog。 但是:我用 http://app.test.local 只有一个坏的 nginx 网关和 http://app.test.local:3000 我到达了 gog 安装...

我试了很多教程,但总有一个错误,每次都漏掉

所以我做了什么:

$ docker network create testserver-network

已创建 docker-为 nginx 编写:

version: '3'

services:
  proxy:
    container_name: proxy
    hostname: proxy
    image: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - /docker/proxy/config:/etc/nginx
      - /docker/proxy/certs:/etc/ssl/private
    networks:
      - testserver-network
networks:
  testserver-network:

还有一个给 gogs:

version: '3'

services:
  gogs:
    container_name: gogs
    hostname: gogs
    image: gogs/gogs
    ports:
      - 3000:3000
      - "10022:22"
    volumes:
      - /docker/gogs/data:/var/gogs/data
    networks:
      - testserver-network
networks:
  testserver-network:

(映射目录有效)

已配置 default.conf 的 nginx:

# upstream gogs {
#   server      0.0.0.0:10880;
# }

server {
  listen        80;
  server_name   app.test.local;

  location / {
    proxy_pass  http://localhost:3000;
  }
}  

并添加到客户端的主机文件中 app.test.local <server ip>

docker exec proxy nginx -tdocker exec proxy nginx -s reload 说一切都很好...

回答

您应该将两个容器连接到同一个 docker 网络,然后代理到 http://gogs:3000。你也不应该需要在本地主机上公开端口 3000,除非你希望 http://app.test.local:3000 to work. I think ideally you should remove that, so http://app.test.local should proxy to your gogs server, and http://app.test.local:3000 应该出错。

说明

gogs 在其容器内的端口 3000 上公开,然后在主机上的端口 3000 上进一步公开。 nginx 容器无法访问您主机上的端口 3000,因此当它尝试代理到 http://localhost:3000 时,它正在代理到 nginx 容器内的端口 3000(没有托管任何内容)。

将容器加入同一网络后,您应该能够通过其主机名(已设置为 gogs)从 nginx 容器引用 gogs 容器。现在 nginx 将通过 docker 网络进行代理。因此,您应该能够执行代理而无需在本地计算机上公开 3000。