NGINX多个服务器块-502错误的网关

时间:2019-09-18 21:32:30

标签: docker nginx docker-compose

我正在尝试使用NginxDocker设置多台服务器。现在,我想使其在本地运行,但我将其导出到网站上使用。我的nginx.conf是:

worker_processes 1;

events { worker_connections 1024; }

http {
    client_max_body_size 2048M;
    sendfile on;

    upstream docker-phpmyadmin {
        server phpmyadmin;
    }
    upstream docker-wordpress {
        server wordpress;
    }
    upstream docker-api {
        server api;
    }
    upstream docker-frontend {
        server frontend;
    }

    server {
        listen 80;
        server_name example.com;
        location / {
            proxy_set_header Host $http_host;
            proxy_pass http://docker-frontend;
        }
    }
    server {
        listen 80;
        server_name api.example.com;
        location / {
            proxy_set_header Host $http_host;
            proxy_pass http://docker-api;
        }
    }

    server {
        listen 80;
        server_name db.example.com;
        location / {
            proxy_set_header Host $http_host;
            proxy_pass http://docker-phpmyadmin;
        }
    }
    server {
        listen 80;
        server_name admin.example.com;
        location / {
            proxy_read_timeout 3600;
            proxy_set_header Host $http_host;
            proxy_pass http://docker-wordpress;
        }
    }
}

我已将以下条目添加到我的/etc/hosts中:

127.0.0.1 example.com
127.0.0.1 db.example.com
127.0.0.1 api.example.com
127.0.0.1 admin.example.com

我的docker-compose.yml包含:

  nginx:
    build: ./backend/nginx
    links:
      - wordpress
      - phpmyadmin
      - frontend
    ports: 
      - ${NGINX_EXTERNAL_PORT}:80
    volumes:
      - "./backend/nginx/nginx.conf:/etc/nginx/nginx.conf"

因此,在本地NGINX_EXTERNAL_PORT设置为5000。我可以访问db.example.com:5000admin.example.com:5000,但是当我尝试访问主页example.com:5000时,得到:

nginx_1       | 2019/09/18 21:26:52 [error] 6#6: *8 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: example.com, request: "GET / HTTP/1.1", upstream: "http://172.18.0.7:80/", host: "example.com:5000"
nginx_1       | 172.18.0.1 - - [18/Sep/2019:21:26:52 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"

我是否缺少该服务器块的配置?谢谢!

1 个答案:

答案 0 :(得分:0)

原来是因为example.com中的应用程序没有公开任何端口,所以在将EXPOSE 3000包含到Dockerfile并将上游更改为

之后,
upstream docker-frontend{
    server frontend:3000;
}

有效!