Home > Net >  How to fix 502 Bad Gateway error in nginx?
How to fix 502 Bad Gateway error in nginx?

Time:05-20

I have a server running docker-compose. Docker-compose has 2 services: nginx (as a reverse proxy) and back (as an api that handles 2 requests). In addition, there is a database that is not located on the server, but separately (database as a service).

Requests processed by back:

  1. get('/api') - the back service simply replies "API is running" to it
  2. get('/db') - the back service sends a simple query to an external database ('SELECT random() as random, current_database() as db')

request 1 - works fine, request 2 - the back service crashes, nginx continues to work and a 502 Bad Gateway error appears in the console.

  • An error occurs in the nginx service Logs: upstream prematurely closed connection while reading response header from upstream.
  • The back service Logs: connection terminated due to connection timeout.

These are both rather vague errors. And I don’t know how else to get close to them, given that the code is not in a container, without Nginx and with the same database, it works as it should.

What I have tried:

  1. increase the number of cores and RAM (now 2 cores and 4 GB of Ram);
  2. add/remove/change proxy_read_timeout, proxy_send_timeout and proxy_connect_timeout parameters;
  3. test the www.test.com/db request via postman and curl (fails with the same error);
  4. run the code on your local machine without a container and compose and connect to the same database using the same pool and the same ip (everything is ok, both requests work and send what you need);
  5. change the parameter worker_processes (tested with a value of 1 and auto);
  6. add/remove attribute proxy_set_header Host $http_host, replace $http_host with "www.test.com".

Question: What else can I try to fix the error and make the db request work?

My nginx.conf:

worker_processes  1;
events {
  worker_connections  1024;
}    
http{
  upstream back-stream {
    server back:8080;
  }
  server {
    listen 80;
    listen [::]:80;
    server_name test.com www.test.com;
    location / {
      root   /usr/share/nginx/html;
      resolver 121.0.0.11;
      proxy_pass http://back-stream;
    }
  }
}

My docker-compose.yml:

version: '3.9'
services:
  nginx-proxy:
    image: nginx:stable-alpine
    container_name: nginx-proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    networks:
       - network
  back:
    image: "mycustomimage"
    container_name: back
    restart: unless-stopped
    ports:
      - '81:8080'
    networks:
       - network
networks:
  network:
    driver: bridge

I can upload other files if needed. Just taking into account the fact that the code does not work correctly in the container, the problem is rather in setting up the container.

I will be grateful for any help.

Code of the back: here

CodePudding user response:

The reason for the error is this: I forgot to add my server's ip to the list of allowed addresses in the database cluster.

  • Related