Can not save post with Wordpress on Docker
Asked Answered
A

2

7

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
Aleedis answered 3/1, 2019 at 18:24 Comment(6)
#41702353Showy
@Showy I don't have this error when opening the website but only when trying to update a post or page, so not the same problem. And solution there seems to update apache which I don't useAleedis
I have a similar setup (no Wordpress but a php application) and in my nginx config I don't use upstream but directly the hostname and port ( if needed ). Have you tried to do it in that way? I'm not sure if using the smae name for the upstream and host is a good idea.Feune
@Aleedis in your docker-compose.yml' there are webapp` link to nginx service, is that already working properly? The solution will be different if there are wordpress only or wordpress + any web apps.Fernyak
@DharmaSaputra sorry forgot to remove the line from the file, it's another service I didn't show here but yes everything works fine as I can access the wordpress site, just some post request don't workAleedis
Does this answer your question? Nginx WordpressLichi
D
1

.htaccess should not have effect in your wordpress install because you are using Nginx.

Refer these posts to configure correctly your Nginx instance. There are several recipes depending if you are using HTTPS or not, cache plugins, etc.

Danelledanete answered 8/1, 2019 at 16:43 Comment(0)
E
0

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. */
Enthalpy answered 8/1, 2019 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.