Christian Strang
Christian Strang

Reputation: 8530

Nginx reverse proxy to map /blog to wordpress docker container

I have a docker compose setup that starts a database, wordpress and nginx container. The nginx container is my reverse proxy and I want to map the "/blog" location to the wordpress container.

I can access the blog directly via http://localhost:8000 but get a 502 error when trying to access the blog via http://localhost/blog

Not sure if I'm missing something or have an error in my setup.

docker-compose.yml

version: '3.3'

services:
   wpdb:
     image: mysql:5.7
     container_name: wpdb
     volumes:
       - wpdb_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - wpdb
     image: wordpress:latest
     container_name: wordpress
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: wpdb:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress

   nginx: 
    depends_on:
      - wordpress
    image: nginx:latest
    container_name: nginx
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    ports:
      - 80:80
      #- 443:443

volumes:
    wpdb_data: {}

nginx.conf

events {

}

http {
  #error_log /etc/nginx/error_log.log warn;
  #client_max_body_size 20m;

  #proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;

  server {
    server_name localhost;

    location /blog {
      proxy_pass http://localhost:8000;
      rewrite ^/blog(.*)$ $1 break;
    }
  }
}

Upvotes: 2

Views: 1283

Answers (1)

jmaitrehenry
jmaitrehenry

Reputation: 2420

You need to change your proxy_pass configuration to point on your wordpress container: proxy_pass http://wordpress;.

This works because docker-compose will create an internal network for your containers and all containers can communicate by their name inside the network.

And, because it's on the same network, you need to use the real port use by the container and not the one exposed (here, port 80 and not 8000).

Upvotes: 2

Related Questions