简体   繁体   English

Docker Nginx反向代理分离容器

[英]Docker Nginx reverse proxy to separate container

I'm having trouble creating a reverse proxy and having it point at apps that are in other containers.我在创建反向代理并将其指向其他容器中的应用程序时遇到问题。

What I have now is a docker-compose for Nginx, and then I want to have separate docker-containers for several different apps and have Nginx direct traffic to those apps.我现在拥有的是 Nginx 的 docker-compose,然后我想为几个不同的应用程序拥有单独的 docker-containers,并让 Nginx 直接访问这些应用程序。

My Nginx docker-compose is:我的 Nginx docker-compose 是:

version: "3"

services:
  nginx:
    image: nginx:alpine
    volumes:
       - ./default.conf:/etc/nginx/conf.d/default.conf

My default.conf is:我的 default.conf 是:

server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

server {
  listen 80;
  server_name www.mydomain.com;
  location /confluence {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://192.168.1.50:8090/confluence;
  }
}

I can access confluence directly at: http://192.168.1.50:8090/confluence我可以直接访问 confluence: http : //192.168.1.50 : 8090/ confluence

My compose for confluence is:我的汇合作文是:

version: "3"
services:
  db:
    image: postgres:9.6
    container_name: pg_confluence
    env_file:
      - env.list
    ports:
      - "5434:5432"
    volumes:
      - ./pg_conf.sql:/docker-entrypoint-initdb.d/pg_conf.sql
      - dbdata:/var/lib/postgresql/data
  confluence:
    image: my_custom_image/confluence:6.11.0
    container_name: confluence
    volumes:
      - confluencedata:/var/atlassian/application-data/confluence
      - ./server.xml:/opt/atlassian/confluence/conf/server.xml
    environment:
      - JVM_MAXIMUM_MEMORY=2g
    ports:
      - "8090:8090"
    depends_on:
      - db

volumes:
  confluencedata:
  dbdata:

I am able to see the Nginx "Welcome" screen when I hit mydomain.com but if I hit mydomain.com/confluence it gives a not found.当我点击 mydomain.com 时,我能够看到 Nginx 的“欢迎”屏幕,但是如果我点击 mydomain.com/confluence,它会显示未找到。

So it looks like Nginx is running, just not sending the traffic to the other container properly.所以看起来 Nginx 正在运行,只是没有正确地将流量发送到另一个容器。

======================== ========================

=== Update With Solution === === 更新解决方案 ===

======================== ========================

I ended up switching to Traefik instead of Nginx.我最终切换到 Traefik 而不是 Nginx。 When I take the next step and start learning k8s this will help as well.当我采取下一步并开始学习 k8s 时,这也会有所帮助。

Although these network settings are what you need even if you stick with Nginx, I just didn't test them against Nginx, so hopefully they are helpful no matter which one you end up using.尽管即使您坚持使用 Nginx,这些网络设置也是您所需要的,但我只是没有针对 Nginx 对它们进行测试,因此希望无论您最终使用哪一个,它们都会有所帮助。

For the confluence docker-compose.yml I added:对于汇合 docker-compose.yml 我添加了:

networks:
  proxy:
    external: true
  internal:
    external: false
services: 
  confluence: 
    ... 
    networks:
      - internal
      - proxy
  db: 
    ...
    networks: 
      - internal

And for the traefik docker-compose.yml I added:对于 traefik docker-compose.yml 我添加了:

networks:
  proxy:
    external: true

services:
  reverse-proxy:
    networks:
      - proxy

I had to create the network manually with:我必须手动创建网络:

docker network create proxy

It is not really how to use docker the correct way.这并不是如何以正确的方式使用 docker。

If you are in a production environment, use a real orchestration tools (nowaday Kubernetes is the way to go)如果您在生产环境中,请使用真正的编排工具(现在Kubernetes是最佳选择)

If you are on you computer, you can reference a name of a container (or an alias) only if you use the same network AND this network is not the default one.如果您在计算机上,则仅当您使用相同的网络并且此网络不是默认网络时,您才能引用容器的名称(或别名)。

A way is to have only one docker-compose file.一种方法是只有一个 docker-compose 文件。

Another way is to use the same network across your docker-compose.另一种方法是在 docker-compose 中使用相同的网络。

  • Create a network docker network create --driver bridge my_network创建网络docker network create --driver bridge my_network
  • use it on each docker-compose you have: networks: default: external: name: my_network在您拥有的每个 docker-compose 上使用它: networks: default: external: name: my_network

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM