繁体   English   中英

NGINX 多个服务器块 - 502 Bad Gateway

[英]NGINX multiple server blocks - 502 Bad Gateway

我正在尝试使用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设置admin.example.com:5000 db.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"

我是否在该服务器块的配置中遗漏了什么? 谢谢!

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

upstream docker-frontend{
    server frontend:3000;
}

作品!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM