简体   繁体   中英

NGINX multiple server blocks - 502 Bad Gateway

I'm trying to setup multiple servers using Nginx and Docker . For now, I want to make it work locally but I'll export this into to use it in a website. My nginx.conf is:

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;
        }
    }
}

I've added these entries to my /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

My docker-compose.yml contains:

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

So, locally, NGINX_EXTERNAL_PORT is set to 5000. I can access to db.example.com:5000 and to admin.example.com:5000 , but when I try to access to my main page example.com:5000 I get:

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"

Am I missing something on my configuration for that server block? Thanks!

It turned out to be because the application that was in example.com was not exposing any port, so after including EXPOSE 3000 to the Dockerfile and changing the upstream to

upstream docker-frontend{
    server frontend:3000;
}

Works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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