Home > Back-end >  Can't see the address when nginx processes the port and outputs 502 Bad Gateway
Can't see the address when nginx processes the port and outputs 502 Bad Gateway

Time:02-12

I encountered a problem when learning nginx. I have 2 servers on 2 different ports. I want a page with information stored on port 8000 to be returned when accessing "http://localhost/api/v1/clients/". Now I get error 502 Bad Gateway. В консоли: connect() failed (111: Connection refused) while connecting to upstream, client: 111.11.0.1, server: localhost, request: "GET /api/v1/clients/ HTTP/1.1", upstream: "http://111.11.0.2:80/api/v1/clients/", host: "localhost" If I go to the address: "http://localhost:8000/api/v1/clients/", everything is fine. What's my mistake?

nginx:

upstream backend {
    server 127.0.0.1:8000;
    server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name localhost;

    location /api/v1/clients/ {
        proxy_pass http://backend_client/api/v1/clients/; 
    }
}

docker:

services:
  backend_client:
    build: ./Client
    volumes:
      - ./Client/:/app
    ports:
      - "8000:8000"
    restart: always
    command:
      "gunicorn --bind=0.0.0.0:8000 django_app.wsgi"
  nginx:
    image: nginx:1.17
    container_name: ngx
    ports:
      - "80:80"
    volumes:
      - ./nginx:/etc/nginx/conf.d
    depends_on:
      - backend_client

CodePudding user response:

Change your NGINX Configuration. Make sure you are reading some basic information about

Your backend will be available as backend_client in your nginx container. The upstream is backend. Your app is still listing on port 8000 and this information is important. So tell NGINX about it :).

upstream backend {
    server backend_client:8000;
}

server {
    listen 80;

    location /api/v1/clients/ {
        proxy_pass http://backend/; 
    }
}


  • Related