4

Ok guys here is the problem: i have my webserver serving a website, a cms and mobile app api using a subdirectory approach like this

  • localhost -> site
  • localhost/cms
  • localhost/api

everything is built using docker compose

services:
  server:
    build: ./docker/nginx
    links:
      - fpm
    ports:
      - 80:80
      - 443:443

  fpm:
    build: ./docker/php
    links:
      - database
      - smtp

  database:
    build: ./docker/mariadb
    volumes:
      - dbdata:/var/lib/mysql

volumes:
  dbdata:

What i'd like to do is to add 2 new containers: one for wordpress and one for the wordpress db in order to proxy pass every request to mydomain.dom/blog to the wordpress container. I was able to pass the request using a nginx config like below but unfortunately i'm fighting with the container which keeps redirecting everything to localhost instead of localhost/blog, i think i'm missing some config in wordpress.

location /blog/ {
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_read_timeout    90;
    proxy_connect_timeout 90;
    proxy_redirect        off;
    proxy_pass http://blog:80; # the wordpress container name

    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $proxy_connection;
    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 $proxy_x_forwarded_proto;
    proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
    proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;

    # Mitigate httpoxy attack (see README for details)
   proxy_set_header Proxy "";
}

# Cms stuff
location /cms/ {
    try_files $uri $uri/ /staff/index.php?$args;

    include _php.conf; # php-fpm setup
}

# Site stuff
location / {

    try_files $uri $uri/ /index.php?$args;

    include _php.conf; # php-fpm setup
}
javal88
  • 143
  • 1
  • 7
  • I think the problem is in the WordPress root URL setting, which points to a wrong place. – Tero Kilkanen Mar 26 '18 at 18:34
  • 2
    I'm trying to set this up on docker-compose as well, but I face the same issue as you. I've added `WORDPRESS_CONFIG_EXTRA: define('WP_HOME','http://localhost/blog');define('WP_SITEURL','http://localhost/blog');` in the service with `image: wordpress:latest`. I've tried in nginx to use `location /blog/ { proxy_pass http://wordpress; }`, but it would redirect me to `http://localhost/blog/wp-login.php/` When trying `location /blog/ { proxy_pass http://wordpress/; }` I can log in but then redirects to `http://localhost/wp-admin/`. I cannot find a way to have it always work on `http:localhost/blog` – Guillaume Nov 10 '18 at 13:09
  • 1
    @Guillaume I have the same problem, Wordpress does not include the subdirectory in URLs to `wp-admin` and `wp-content` – Niklas R Jul 14 '19 at 19:02

2 Answers2

2

You need to tell WordPress the new base URL to use. The easiest way to do it is to add the following two lines to wp-config.php in wordpress base dir:

define('WP_HOME','http://example.com/blog');
define('WP_SITEURL','http://example.com/blog');
Eduardo Cuomo
  • 192
  • 1
  • 5
Luca Gibelli
  • 2,611
  • 1
  • 21
  • 29
  • 1
    I'm trying to do the same as the OP. The problem with this answer is that it's not possible to edit the wp-config.php file; it's part of the docker container that's supplied. The documentation for the container (see https://hub.docker.com/_/wordpress/) claims that an environment variable called `WORDPRESS_CONFIG_EXTRA` will be appended verbatim to `wp-config.php` but this appears to be a bald-faced lie. – Tom Feb 26 '20 at 16:04
  • it will be appended only if wp-config.php does not already exists, see https://github.com/docker-library/wordpress/issues/333 – Luca Gibelli Feb 27 '20 at 08:40
  • very dirty way to solve your issue: create your custom wp-config.php in /usr/local/etc/myconfig.php and then start the container with something like: docker run --name mywp -d -v /usr/local/etc/myconfig.php:/usr/src/wordpress/wp-config.php wordpress. You get the idea :) – Luca Gibelli Feb 27 '20 at 08:46
0

To initialize Wordpress in a subdirectory like /blog, you can use the working_dir attribute.

The default Wordpress image uses /var/www/html as root, and you can move to /var/www/html/blog to initialize at this point.

# ...
  wordpress:
    image: wordpress:apache
    working_dir: /var/www/html/blog
    volumes:
      - ./data/blog:/var/www/html/blog/wp-content
      - ./blog/wp-config.php:/var/www/html/blog/wp-config.php
# ...

Now, you can navigate to http://localhost/blog.

Full example

Following is a full docker-compose.yml example:

version: '3.1'

x-wordpress: &wordpress
  depends_on:
    - db
  environment:
    WORDPRESS_DB_HOST: db:3306
    WORDPRESS_DB_NAME: wordpress
    WORDPRESS_DB_USER: wordpress
    WORDPRESS_DB_PASSWORD: wordpress
  working_dir: /var/www/html/blog
  volumes:
    - wp_root:/var/www/html/blog
    - ./wp-content/plugins:/var/www/html/blog/wp-content/plugins
    - ./wp-content/themes:/var/www/html/blog/wp-content/themes
    - ./wp-content/uploads:/var/www/html/blog/wp-content/uploads
    - ./wp-content/languages:/var/www/html/blog/wp-content/languages

services:
  # Database
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
      - ./backup/blog.sql:/docker-entrypoint-initdb.d/dump.sql:ro
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  # phpmyadmin
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    ports:
      - 8080:80
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password

  # Wordpress
  wordpress:
    <<: *wordpress
    image: wordpress:apache
    ports:
      - 80:80

  wpcli:
    <<: *wordpress
    image: wordpress:cli

volumes:
  wp_root:
    driver_opts:
      type: tmpfs
      device: tmpfs
  db_data:
Eduardo Cuomo
  • 192
  • 1
  • 5