0

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;
        }
    }
}
1
  • The solution was to add wordpress:80; as proxy_pass Commented Oct 1, 2023 at 21:20

1 Answer 1

0

The solution was to add the destination-container-name:port as proxy_pass values. So in this case http://wordpress:80;

Also I needed to open the website through a private tab, since the browser kept information stored and didn’t refresh properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.