samuelweckstrom
samuelweckstrom

Reputation: 229

Docker containers in different paths with Nginx

I'm trying to setup Nginx to route two apps (one Express and one Wordpress in Docker containers) to their own public paths. Until now it only works with one container at a time, but routing both from local ports to their public paths does not and I'm a bit out of ideas why. Any help or ideas to approach this much appreciated.

Heres my nginx config:

server {
  listen 80;
  proxy_set_header Host $http_host;
  proxy_set_header X-Read-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_redirect off;

  location /api {
    proxy_pass  http://0.0.0.0:3000;
  }

  location /blog {
    proxy_pass  http://0.0.0.0:8000;
  }
}

Docker compose:

version: '3.2'
services:
  api-service:
    restart: always
    depends_on:
      - wordpress-service
    image: mhart/alpine-node:latest
    build: .
    networks:
      main:
        aliases:
          - api-service
  wordpress-service:
    restart: always
    depends_on:
      - db
    image: wordpress:latest
    working_dir: /var/www/html
    volumes:
      - wordpress:/var/www/html/wp-content
    expose:
      - "8000"
    networks:
      main:
        aliases:
          - wordpress-service
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      main:
        aliases:
          - db
volumes:
  db_data:
  wordpress:
networks:
  main:

Upvotes: 2

Views: 1531

Answers (1)

samuelweckstrom
samuelweckstrom

Reputation: 229

Figured it out, and what was missing was trailing slashes for the path in the express app.

location /api/ {
  proxy_pass  http://0.0.0.0:3000/;
}

The answer here proved helpful.

Upvotes: 2

Related Questions