I am fairly noob on nginx as well as docker

I have two wordpress and one nginx ( sepperate) docker-compose.yml. I will like to get the nginx conainer as reverse proxy for my two wordpress. For example i will like to go to http://localhost/wordpressblog or http://localhost/wordpressphotography

I am trully open to recomendations

One of the compose is

version: '3'
services:
  wp:
    image: wordpress:latest
    hostname: wordpressblog
    ports:
      - 81:80
    volumes:
      - /opt/wordpress/config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
      - /opt/wordpress/wp-app:/var/www/html
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: "wordpress"
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: "password"
    depends_on:
      - db
    links:
      - db

This is my nginx config

upstream wordpressblog {
    server wordpressblog:81;
}
upstream wordpressphotography {
    server wordpressphotography:82;
}


server {
  listen 80;

  location /wordpressblog/ {
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      proxy_pass http://wordpressblog/;
  }
  location /wordpressphotography/ {
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      proxy_pass http://wordpressphotography/;
  }

}

1 Answers

1
VonC On

Instead of using NGinX, try and use Traefik, which is a Docker-aware reverse proxy.

You can see an example in "Traefik on Docker for Web Developers - With bonus Let's Encrypt SSL!", from Juan Treminio, in order to register automatically your containers and access them through a pre-defined URL.

Juan describes how to solve the "port dance":

If port 80 is mapped to web-server-A you must choose another port to bind for web-server-B and web-server-C.
This can quickly get old because you must remember that http://localhost goes to A, http://localhost:81 goes toB and http://localhost:82 goes to C.

He points out:

On virtual machines this problem does not really occur because you can assign a static IP address to your servers, and bind it to your system’s hosts file (/etc/hosts).
Containers are ephemeral by nature and do not normally get created on your host’s network but rather private networks with their own random IP addresses within special ranges. However, you must edit /etc/hosts for every VM you spin up and the list grows with the number of projects you handle.

Træfik solves both of these problems, first by removing the need to use ports in URLs and second by not needing you to edit /etc/hosts at all.

The way Traefik detects a new container is through is docker-deicated network:

 docker network create --driver bridge traefik_webgateway

A new container will register itself to said Traefik docker network:

docker run -d --name some-mailhog \
    --network traefik_webgateway \
    --label traefik.docker.network=traefik_webgateway \
    --label traefik.frontend.rule=Host:mailhog.localhost \
    --label traefik.port=8025 \
    mailhog/mailhog

The URL becomes simple http://mailhog.localhost.

You can similarly register your two Wordpress components.

Advantage: no need to maintain a static NGiNX config file: any number of components can register their URL.


The old pre-Traefik way would be, with NGiNX, to use jwilder/nginx-proxy

It is an Automated Nginx Reverse Proxy for Docker:

Docker provides a remote API to inspect containers and access their IP, Ports and other configuration meta-data. In addition, it also provides a real-time events API that can be used for notifications when containers are started and stopped. These APIs can be used to generate a reverse proxy config automatically.

docker-gen is a small utility that uses these APIs and exposes container meta-data to templates. Templates are rendered and an optional notification command can be run to restart the service.

Using docker-gen, we can generate Nginx config files automatically and reload nginx when they change.