Nginx

基於 Docker 的反向代理與 NginX 用於多個域

  • April 11, 2020

我正在嘗試創建一個基於 docker 的多容器設置,並為多個域提供反向代理,其中網站、數據庫和基於 nginx 的反向代理在容器中執行,但我不知道我錯過了什麼(我是 nginx 的新手)。

細節:

  • 用於反向代理的基於 NginX 的 docker 容器

  • 我擁有 2 個域

    • 例如my_example_domain_1.com:,my_example_domain_2.net
  • 我想在每個域名後面放一個容器來服務兩個域

    • 我想執行兩個獨立的網站:例如:兩個 WordPress 網站
  • 我需要兩個域和相應容器之間的 NginX 路由

multi-container我的**例子:(**基於:)

nginx_revproxy/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  my_example_domain_1.com;
   location / {
       proxy_pass http://wp1:80;
   }
}

server {
   listen   80;
   server_name  my_example_domain_2.net;
   location / {
       proxy_pass http://wp2:80;
   }
}

./nginx_revproxy/Dockerfile:

FROM nginx:stable-alpine
COPY default.conf /etc/nginx/conf.d
EXPOSE 80/tcp
EXPOSE 443/tcp
CMD ["/bin/sh", "-c", "exec nginx -g 'daemon off;';"]
WORKDIR /usr/share/nginx/html

./docker-compose.yml

version: '3.7'

services:

  revproxy:
    container_name: revproxy
    build: nginx_revproxy
    restart: always
    networks:
      - mynet

  db1:
    container_name: db1
    image: mysql:5.7
    volumes:
      - "${PWD}/data_mysql1:/var/lib/mysql"
      - "/tmp/db_site1:/tmp"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - mynet

  db2:
    container_name: db2
    image: mysql:5.7
    volumes:
      - "${PWD}/data_mysql2:/var/lib/mysql"
      - "/tmp/db_site2:/tmp"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - mynet

  wp1:
    container_name: wp1
    depends_on:
      - db1
      - revproxy
    image: wordpress:latest
    ports:
      - "8081:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db1:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - "${PWD}/data_wordpress1:/var/www/html"
      - "/tmp/wp_site1:/tmp"
    networks:
      - mynet

  wp2:
    container_name: wp2
    depends_on:
      - db2
      - revproxy
    image: wordpress:latest
    ports:
      - "8082:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db2:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - "${PWD}/data_wordpress2:/var/www/html"
      - "/tmp/wp_site2:/tmp"
    networks:
      - mynet

volumes:
   data_mysql1:
   data_mysql2:
   data_wordpress1:
   data_wordpress2:

networks:
   mynet:

旁注:

  • 測試:如果我revproxydocker-compose.yml文件中取出並docker-compose up -d在我的台式機上執行,我可以訪問容器埠上的兩個網站(沒有 revproxy)localhost:8081:,localhost:8082
  • 產品:當我docker-compose up -d在伺服器上執行時,無法訪問網站
  • 產品:所有容器似乎都可以正常執行
$ docker ps -a
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                  NAMES
974fa178b964        wordpress:latest             "docker-entrypoint.s…"   45 seconds ago      Up 41 seconds       0.0.0.0:8081->80/tcp   wp1
bdace64331b7        wordpress:latest             "docker-entrypoint.s…"   45 seconds ago      Up 41 seconds       0.0.0.0:8082->80/tcp   wp2
9877a41d4bfa        multi-container-2_revproxy   "/bin/sh -c 'exec ng…"   47 seconds ago      Up 41 seconds       80/tcp, 443/tcp        revproxy
e88e226499f2        mysql:5.7                    "docker-entrypoint.s…"   47 seconds ago      Up 44 seconds       3306/tcp, 33060/tcp    db2
9feaec1b2314        mysql:5.7                    "docker-entrypoint.s…"   47 seconds ago      Up 44 seconds       3306/tcp, 33060/tcp    db1
  • 產品:
$ docker logs -f revproxy
2020/04/09 08:34:21 [emerg] 1#1: host not found in upstream "wp1" in /etc/nginx/conf.d/default.conf:20
nginx: [emerg] host not found in upstream "wp1" in /etc/nginx/conf.d/default.conf:20
  • PROD:所有容器似乎共享相同的網路介面
$ docker inspect multi-container_mynet
docker inspect multi-container_mynet
[
   {
       "Name": "multi-container_mynet",
       "Id": "24e1dd604427db695709056290b9c86021674916d57133b24e33fe765cd26165",
       "Created": "2020-04-09T10:44:59.568919008+02:00",
       "Scope": "local",
       "Driver": "bridge",
       "EnableIPv6": false,
       "IPAM": {
           "Driver": "default",
           "Options": null,
           "Config": [
               {
                   "Subnet": "172.31.0.0/16",
                   "Gateway": "172.31.0.1"
               }
           ]
       },
       "Internal": false,
       "Attachable": true,
       "Ingress": false,
       "ConfigFrom": {
           "Network": ""
       },
       "ConfigOnly": false,
       "Containers": {
           "18d60a010dafca22dd03c2f60cf1b2a607f7ac19ea00e88cd6baff6ada392545": {
               "Name": "revproxy",
               "EndpointID": "14974557d1876df15e35ffacb333a4d15883b7645cd90fe819cc2763118c7151",
               "MacAddress": "02:42:ac:1f:00:02",
               "IPv4Address": "172.31.0.2/16",
               "IPv6Address": ""
           },
           "4b591fcb01c464d9ce932ab8e67e9409c2e19e725ab75faa5bcfd2c322ced1b8": {
               "Name": "db1",
               "EndpointID": "a3631f7943701b333b203e48d816853ef7da4739cfbd265edc6126aad9fbc832",
               "MacAddress": "02:42:ac:1f:00:04",
               "IPv4Address": "172.31.0.4/16",
               "IPv6Address": ""
           },
           "885434c047c3eb2f5794a307a51dc5057384744a5483857474e41c697d2c954d": {
               "Name": "db2",
               "EndpointID": "d1840498f59f96840f44e1b91be0a69ff00cd78e06fea736e8f22f01e7fb8a94",
               "MacAddress": "02:42:ac:1f:00:03",
               "IPv4Address": "172.31.0.3/16",
               "IPv6Address": ""
           },
           "9d90045cfe089418cb1886af89f79780f0d52f08bb876241c3d90d5fa5ab0dfd": {
               "Name": "wp1",
               "EndpointID": "2052ae635d5317e6928bb961d183c280add219b09ac042fc56a6f26c3410531e",
               "MacAddress": "02:42:ac:1f:00:06",
               "IPv4Address": "172.31.0.6/16",
               "IPv6Address": ""
           },
           "b9caf876f7f4f8bbf2d77c422f0179e647bcfd22b0129de0a83c90360df54612": {
               "Name": "wp2",
               "EndpointID": "479bbd8d29cff9644c008316c91cdecac75f3f8ef107ad59f0d2e4683cd4b81c",
               "MacAddress": "02:42:ac:1f:00:05",
               "IPv4Address": "172.31.0.5/16",
               "IPv6Address": ""
           }
       },
       "Options": {},
       "Labels": {
           "com.docker.compose.network": "mynet",
           "com.docker.compose.project": "multi-container",
           "com.docker.compose.version": "1.23.1"
       }
   }
]

有誰有想法嗎?

同時,我已經自己弄清楚了。

這是一個有效的配置(如果有人對未來感興趣的話):

nginx_revproxy/default.conf:

upstream domain1 {
 server wp1:80;
}

upstream domain2 {
 server wp2:80;
}


server {
   listen       80;
   server_name  domain1.com;
   location / {
       proxy_pass http://domain1/;
       proxy_set_header Host            $host;
       proxy_set_header X-Forwarded-For $remote_addr;
   }
}

server {
   listen       80;
   server_name  domain2.com;
   location / {
       proxy_pass http://domain2/;
       proxy_set_header Host            $host;
       proxy_set_header X-Forwarded-For $remote_addr;
   }
}

nginx_revproxy/Dockerfile:

FROM nginx:stable-alpine
COPY default.conf /etc/nginx/conf.d
EXPOSE 80/tcp
EXPOSE 443/tcp
CMD ["/bin/sh", "-c", "exec nginx -g 'daemon off;';"]
WORKDIR /usr/share/nginx/html

docker-compose.yml:

version: '3.7'

services:

  revproxy:
    container_name: revproxy
    build: nginx_revproxy
    depends_on:
      - wp1
      - wp2
    restart: always
    ports:
      - 80:80
    networks:
      - mynet

  db1:
    container_name: db1
    image: mysql:5.7
    volumes:
      - "${PWD}/data_mysql1:/var/lib/mysql"
      - "/tmp/db_site1:/tmp"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - mynet

  db2:
    container_name: db2
    image: mysql:5.7
    volumes:
      - "${PWD}/data_mysql2:/var/lib/mysql"
      - "/tmp/db_site2:/tmp"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - mynet

  wp1:
    container_name: wp1
    depends_on:
      - db1
    image: wordpress:latest
    ports:
      - "8081:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db1:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - "${PWD}/data_wordpress1:/var/www/html"
      - "/tmp/wp_site1:/tmp"
    networks:
      - mynet

  wp2:
    container_name: wp2
    depends_on:
      - db2
    image: wordpress:latest
    ports:
      - "8082:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db2:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - "${PWD}/data_wordpress2:/var/www/html"
      - "/tmp/wp_site2:/tmp"
    networks:
      - mynet

volumes:
   data_mysql1:
   data_mysql2:
   data_wordpress1:
   data_wordpress2:

networks:
   mynet:

謝謝你的幫助!

引用自:https://serverfault.com/questions/1011561