NGINX Docker reverse proxy not working on server (Works locally)

I am running a Django app on Docker with Nginx, the configuration works perfectly locally but on the server, it doesn't.

i get the error 502 Bad Gateway in the web page

and the logs I get

2023/01/13 07:55:56 [error] 28#28: *1 connect() failed (111: Connection refused) while connecting 
to upstream, client: xx.xxx.xx.xxx, server: , request: "GET /favicon.ico HTTP/1.1", upstream: 
"http://xxx.xx.x.x:8000/favicon.ico", host: "xx.xxx.xxx.xxx", referrer: "http://xx.xxx.xxx.xxx/"

This is how the configuration looks

Dockerfile

FROM nginx:1.23.3-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY ./default.conf /etc/nginx/conf.d/default.conf

default.conf

upstream apii {
    server api:8000;
}

server {
    client_max_body_size 20M;
    listen 80;

    location / {
        proxy_pass http://apii;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    # location = /favicon.ico { access_log off; log_not_found off; }

    location /api {
        proxy_pass http://apii;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /realestateadmin {
        proxy_pass http://apii;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;

    }

    location /staticfiles/ {
        alias /app/staticfiles/;
    }

    location /mediafiles/ {
        alias /app/mediafiles/;
    }

}

docker-compose.yml

version: "3.9"

services:
    api:
        build:
            context: .
            dockerfile: ./docker/local/django/Dockerfile
        command: /start
        volumes:
            - .:/app
            - static_volume:/app/staticfiles
            - media_volume:/app/mediafiles
        # ports:
        #     - "8000:8000"
        expose:
            - 8000
        env_file:
            - .env
        depends_on:
            - postgres-db
        networks:
            - real-estate

    postgres-db:
      image: postgres:12.0-alpine
      ports:
          - "5432:5432"
      volumes:
          - postgres_data:/var/lib/postgresql/data/
      environment:
          - POSTGRES_USER=${DB_USER}
          - POSTGRES_PASSWORD=${DB_PASSWORD}
          - POSTGRES_DB=${DB_NAME}
      networks:
          - real-estate
    
    nginx:
        restart: always
        depends_on:
            - api
        volumes:
            - static_volume:/app/staticfiles
            - media_volume:/app/mediafiles
        build: 
            context: ./docker/local/nginx
            dockerfile: Dockerfile
        ports:
          - "80:80"
        networks:
            - real-estate

networks:
  real-estate:
    driver: bridge

volumes:
  postgres_data:
  static_volume:
  media_volume:

I have tried different things. I doesn't make sense to me.

Back to Top