I am working on adding a Dockerized blog stack (Wordpress + MariaDB) to our existing website that runs on Nginx. location / already serves out the website, and I have been instructed to add logic to the Nginx config where /blog redirects all traffic to the Docker container. 
localhost:9999 on the server.
        location ^~ /blog {
            proxy_pass http://localhost:9999;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
Problem:
Wordpress redirected http://server/blog to http://wp-admin/install.php, and since there was no rule for this I got a 404.
I changed the location to process /blog as well as any URL containing wp-.*. The assumption is that all Wordpress pages will have that URL.
        location ~ ^/(blog|wp-.*) {
This worked through the Wordpress setup. 
And if I explicitly visit http://server/wp-admin I am able to go to the blog admin pages.
Problem:
http://server/?p=31 is a link to a blog post. Just for the heck of it I redirected location / to the Docker container and the blog works perfectly. But that is not the problem statement I have, sadly.
What do I do next, in order to serve all wordpress-specific requests from? I feel I am getting into a loop of fighting regexes when there might be a more elegant solution here.
According to a self-answered SO question https://stackoverflow.com/a/41479776/1264360
// add these lines to wp-config.php
define('WP_SITE_URL', 'http://example.com/blog');
define('WP_HOME', 'http://example.com/blog');
$_SERVER['REQUEST_URI'] = '/blog' . $_SERVER['REQUEST_URI'];
                        Suppose you want to have your wordpress website accessible via wp subdirectory. For example:
http://your-website.com/wp
Here is how I managed to get this working without altering wp-config.php. The key is to use working_dir directive.
docker-compose.yml
version: '3'
services:
 nginx:
   image: nginx:1.17
   container_name: nginx
   restart: unless-stopped
   depends_on:
     - wordpress
   ports:
     - '80:80'
   networks:
     - app-network
   volumes:
     - ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
 db:
   image: mysql:5.7
   container_name: db
   volumes:
     - db_data:/var/lib/mysql
   restart: always
   environment:
     MYSQL_ROOT_PASSWORD: somewordpress
     MYSQL_DATABASE: wordpress
     MYSQL_USER: wordpress
     MYSQL_PASSWORD: wordpress
   networks:
     - app-network
 wordpress:
   container_name: wordpress
   depends_on:
     - db
   image: wordpress:latest
   restart: always
   environment:
     WORDPRESS_DB_HOST: db:3306
     WORDPRESS_DB_USER: wordpress
     WORDPRESS_DB_PASSWORD: wordpress
     WORDPRESS_DB_NAME: wordpress
   working_dir: /var/www/html/wp
   volumes:
    - wordpress:/var/www/html/wp
   networks:
     - app-network
networks:
 app-network:
   driver: bridge
volumes:
 db_data:
 wordpress:
nginx.conf
events {}
http {
    server {
        listen 80;
        location /wp/ {
            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_pass http://wordpress;
        }
     }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With