Docker berbasis reverse proxy Dengan NginX untuk beberapa domain

Saya mencoba membuat pengaturan multi-kontainer berbasis docker dengan proxy terbalik untuk melayani beberapa domain, di mana situs web, database, dan proxy terbalik berbasis nginx berjalan dalam wadah, tetapi saya tidak tahu apa yang saya lewatkan (saya baru mengenal nginx).

Rincian:

  • Wadah docker berbasis NginX untuk proxy terbalik
  • Ada 2 domain yang saya miliki
    • misalnya.: my_example_domain_1.com, my_example_domain_2.net
  • Saya ingin meletakkan wadah di belakang setiap nama domain untuk melayani dua domain
    • Saya ingin menjalankan dua situs web terpisah: misalnya: dua situs web WordPress
  • Saya memerlukan rute NginX antara dua domain dan wadah yang sesuai

Contoh saya: (basedir: multi-container)

nginx_revproxy / bawaan.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-alpineCOPY default.conf /etc/nginx/conf.dEXPOSE 80/tcpEXPOSE 443/tcpCMD ["/bin/sh", "-c", "exec nginx -g 'daemon off;';"]WORKDIR /usr/share/nginx/html

./ docker-menulis.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:       - mynetvolumes:    data_mysql1:    data_mysql2:    data_wordpress1:    data_wordpress2:networks:    mynet:

Sidenotes:

  • Tes: jika saya mengambil revproxy dari docker-compose.yml file dan eksekusi docker-compose up -d di mesin desktop saya, saya dapat mengakses kedua situs web (tanpa revproxy) di port kontainer: localhost:8081, localhost:8082
  • PROD: ketika saya mengeksekusi docker-compose up -d di server Saya, situs web tidak dapat dijangkau
  • PROD: semua kontainer tampaknya berjalan tanpa kesalahan
$ docker ps -aCONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                  NAMES974fa178b964        wordpress:latest             "docker-entrypoint.s…"   45 seconds ago      Up 41 seconds       0.0.0.0:8081->80/tcp   wp1bdace64331b7        wordpress:latest             "docker-entrypoint.s…"   45 seconds ago      Up 41 seconds       0.0.0.0:8082->80/tcp   wp29877a41d4bfa        multi-container-2_revproxy   "/bin/sh -c 'exec ng…"   47 seconds ago      Up 41 seconds       80/tcp, 443/tcp        revproxye88e226499f2        mysql:5.7                    "docker-entrypoint.s…"   47 seconds ago      Up 44 seconds       3306/tcp, 33060/tcp    db29feaec1b2314        mysql:5.7                    "docker-entrypoint.s…"   47 seconds ago      Up 44 seconds       3306/tcp, 33060/tcp    db1
  • PROD:
$ docker logs -f revproxy2020/04/09 08:34:21 [emerg] 1#1: host not found in upstream "wp1" in /etc/nginx/conf.d/default.conf:20nginx: [emerg] host not found in upstream "wp1" in /etc/nginx/conf.d/default.conf:20
  • PROD: semua wadah tampaknya berbagi antarmuka jaringan yang sama
$ docker inspect multi-container_mynetdocker 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"        }    }]

Apakah ada yang punya ide?

Sementara itu saya sudah menemukan jawabannya sendiri..
Ini adalah konfigurasi kerja (jika ada yang tertarik di masa depan):

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-alpineCOPY default.conf /etc/nginx/conf.dEXPOSE 80/tcpEXPOSE 443/tcpCMD ["/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:       - mynetvolumes:    data_mysql1:    data_mysql2:    data_wordpress1:    data_wordpress2:networks:    mynet:

Terima kasih atas semua bantuan Anda!