nginx reverse proxy in Docker-Container in Docker-Compose 502 Bad Gateway error

I m trying to start a docker-compose.yml with a nginx reverse-proxy as one of the services.
The proxy should forward requests on port 80 to the database-container:8080.


I have unsuccessfully tried localhost:8080 and then 0.0.0.0:8080 for proxy_pass values.
The containers run inside the same network.


Finally after my last compose up, i checked the database’s-container IPAddress and then inserted that address into nginx’s proxy_pass as follows:

http {
    server {
        listen 80;
        location / {
            proxy_pass http://192.168.48.2:8080;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}

Then did inside the nginx-container: nginx -s reload

Sadly i still get a 502 Bad Gateway when i visit localhost:80.
localhost:8080 takes me to the WordPress Set-Up Page.

Happy to learn what’s happening here!



And for full documentation here the docker-compose.yml, the Dockerfile of the nginx-container and my original nginx.conf

docker-compose.yml

version: "3"
services:
  reverse_proxy:
    container_name: reverse_proxy
    build: ./reverseproxy
    restart: always
    ports:
      - 80:80
  db:
    container_name: database
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: cool-password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: user
      MYSQL_PASSWORD: user-wordpress
  wordpress:
    container_name: wordpress
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_PASSWORD: user-wordpress
      WORDPRESS_DB_NAME: wordpress
volumes: 
  db_data:
networks:
  default:
    name: revprox

Dockerfile

FROM nginx:mainline-alpine
RUN rm /etc/nginx/conf.d/default.conf
RUN rm /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx/nginx.conf
VOLUME ["something/docker/wordpress-reverse-proxy/reverseproxy/logs", "/var/log/nginx"]
# RUN nginx -c /etc/nginx/nginx.conf
# RUN nginx -s reload
# CMD ["nginx", "-s", "reload"]

nginx.conf

worker_processes 1;
 
events { worker_connections 1024; }
 
http {
    server {
        listen 80;
        location / {
            proxy_pass http://0.0.0.0:8080;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}

Leave a Comment