Can not save post with Wordpress on Docker

Viewed 1171

I am running Wordpress on Docker-Compose with the following docker-compose.yml:

version: '2'

services:
  db_wordpress:
    image: mysql:5.7
    volumes:
      - db_wordpress_data:/var/lib/mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: pwd
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: pwd

  wordpress:
    image: wordpress:latest
    volumes:
      - ./wp-content:/var/www/html/wp-content
    restart: unless-stopped
    expose:
      - 80
    depends_on:
      - db_wordpress
    environment:
      WORDPRESS_DB_HOST: db_wordpress:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: pwd
      WORDPRESS_DEBUG: 1
      WORDPRESS_CONFIG_EXTRA: |
        // Enable Debug logging to the /wp-content/debug.log file
        define('WP_DEBUG_LOG', true);
        // Disable display of errors and warnings
        define('WP_DEBUG_DISPLAY', false);
        // Handle subpath /blog
        define('WP_HOME','https://www.example.com/blog');
        define('WP_SITEURL','https://www.example.com/blog');
        $$_SERVER['REQUEST_URI'] = '/blog' . $$_SERVER['REQUEST_URI'];

  nginx:
    image: nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    links:
      - wordpress
    volumes:
      - /var/www/letsencrypt:/var/www/letsencrypt
      - ./logs/nginx:/var/log/nginx
      - ./nginx.conf:/etc/nginx/nginx.conf

volumes:
    db_wordpress_data:

So the wordpress site is behind an nginx container with the following nginx.conf file:

events {
    worker_connections 1024;
}

http {
  upstream wordpress {
    server wordpress:80;
  }

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  sendfile on;
  keepalive_timeout 65;

  server_names_hash_bucket_size 512;

  proxy_cache_path /var/cache levels=1:2 keys_zone=STATIC:10m inactive=24h  max_size=1g;

  server {
    listen 80;
    server_name example.com;

    location / {
      return 301 https://$host$request_uri;
    }
  }

  server {
    listen 443 default_server ssl;

    server_name example.com;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    try_files   $uri $uri/ =404;

    location /blog/ {
      proxy_pass http://wordpress/;
      proxy_buffering off;
      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_set_header X-Forwarded-Proto $scheme;
    }
  }
}

Now I can access my wordpress admin, install plugins, etc. but when I want to update Posts or Pages, I get a 500 error on the browser, and looking at the logs (docker logs -f clearroad_wordpress_1):

[Thu Jan 03 17:03:36.105145 2019] [core:error] [pid 97] [client 172.18.0.5:51210] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: https://www.example.com/blog/wp-admin/post.php?post=62&action=edit
172.18.0.5 - - [03/Jan/2019:17:03:36 +0000] "POST /wp-json/wp/v2/posts/62/autosaves?_locale=user HTTP/1.0" 500 806 "https://www.example.com/blog/wp-admin/post.php?post=62&action=edit" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko)

So it seems the requests on /blog/wp-json end up in a redirect loop, what could be the cause?

I checked the .htaccess file in the wordpress container:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress
2 Answers

You are having redirect loop since you proxy to http on Wordpress container, but configured Wordpress to work on https.

for your configuration, please add following to your wp-config.php before the happy blogging line to resolve it:

    if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
        $_SERVER['HTTPS'] = 'on';
    }
/* That's all, stop editing! Happy blogging. */
Related