基于 Docker 的 NginX 反向代理,适用于多个域

基于 Docker 的 NginX 反向代理,适用于多个域

我正在尝试创建一个基于 docker 的多容器设置,并使用反向代理为多个域提供服务,其中网站、数据库和基于 nginx 的反向代理在容器中运行,但我不知道我错过了什么(我是 nginx 新手)。

细节:

  • 基于 NginX 的 docker 容器用于反向代理
  • 我拥有 2 个域名
    • 例如:my_example_domain_1.commy_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:

旁注:

  • revproxy测试:如果我从文件中删除docker-compose.yml并在我的台式机上执行docker-compose up -d,我可以通过容器端口访问两个网站(没有 revproxy)localhost:8081:,localhost: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
  • 产品:
$ 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

同时,我自己也弄清楚了。
这是一个有效的配置(如果将来有人感兴趣的话):

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

这是重复特拉菲克拴住其他的

相关内容