Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx subpath to redirect to wordpress docker container

Background

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.

Attempt 1

  • I started the wordpress container at localhost:9999 on the server.
  • Using the references I added additional logic:

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.

Attempt 2

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:

  • The Wordpress blog pages do not satisfy the regexes above, and return a 404. e.g. http://server/?p=31 is a link to a blog post.

Attempt 3

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.

Question

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.

References

  1. nginx proxy pass subpaths not redirected
  2. How can I have same rule for two locations in NGINX config?
  3. nginx redirect to docker container
like image 919
Vish Avatar asked Dec 09 '17 03:12

Vish


2 Answers

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'];
like image 200
Shawn C. Avatar answered Nov 15 '22 03:11

Shawn C.


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;
        }
     }
}
like image 40
Hamed Avatar answered Nov 15 '22 03:11

Hamed