web-dev-qa-db-ja.com

複数ドメイン用のNginXを使用したDockerベースのリバースプロキシ

提供する複数のドメインのリバースプロキシを使用して、Dockerベースのマルチコンテナー設定を作成しようとしていますが、Webサイト、データベース、nginxベースのリバースプロキシがコンテナーで実行されていますが、何が欠けているのかわかりません(私はnginxが初めてです)。

詳細:

  • リバースプロキシ用のNginXベースのDockerコンテナー
  • 私が所有する2つのドメインがあります
    • 例:my_example_domain_1.commy_example_domain_2.net
  • 2つのドメインを提供するために、各ドメイン名の後ろにコンテナを配置したいと思います
    • 2つの別々のウェブサイトを実行したい:例:2つのWordPressウェブサイト
  • 2つのドメインと対応するコンテナーの間にNginXルートが必要です

私の例:(basedir: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:

補足:

  • テスト:docker-compose.ymlファイルからrevproxyを取り出し、デスクトップマシンでdocker-compose up -dを実行すると、コンテナーポートで両方のWebサイト(revproxyなし)にアクセスできます:localhost:8081localhost:8082
  • PROD:サーバーでdocker-compose up -dを実行すると、ウェブサイトにアクセスできません
  • PROD:すべてのコンテナーがエラーなしで実行されているように見える
$ 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
  • PROD:
$ 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"
        }
    }
]

誰かが何か考えを持っていますか?

1
elaspog

その間私は自分でそれを理解しました。
これは機能する構成です(将来に興味がある場合):

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:

助けてくれてありがとう!

2
elaspog