🌍Hosting WordPress Docker behind NGINX reverse proxy

Many examples feature differing variations on this combo, but few seemed to use a wordpress container and a separate NGINX proxy

My hosting setup uses a few different websites – some are dot net, some are angular (which is just serving up static files) and some are WordPress

I wanted to add WordPress to the list of containers that I host, but the setup wasn’t as straight forward as I would have liked.

Here is what worked for me:

Spin up a docker container

Grab the official docker image from here. I used the docker-compose method of getting started.

You can grab the details here:

https://hub.docker.com/_/wordpress

version: "3.9"
    
services:
  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
    
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wordpress_data:/var/www/html
    ports:
      - "9000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  wordpress_data: {}

Notice how we are exposing port 9000? Keep that in mind as we look at the NGINX config 😉

Set up your NGINX config like so:

server {


    listen 443 ssl; # managed by Certbot

    server_name www.yourservername.com.au;

    ssl_certificate /etc/letsencrypt/live/yourservername.com.au/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/yourservername.com.au/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


    #  proxy_redirect off;
    location / {
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://localhost:9000;

        
    }

}
server {

    server_name yourservername.com.au www.yourservername.com.au;

    if ($host = www.yourservername.com.au) {
        return 301 https://$host$request_uri;
        } # managed by Certbot


        if ($host = yourservername.com.au) {
            return 301 https://$host$request_uri;
            } # managed by Certbot
            listen 80;
            return 404; # managed by Certbot
        }

Recall we exposed port 9000 for our docker container?

We are telling NGINX to forward the requests that match yourservername.com to localhost:9000

The proxy_set_header lines help to translate the url yourservername.com to the container url localhost:9000

Let me know how you get on 👍


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *