Prog_is_life
Prog_is_life

Reputation: 884

WordPress in Docker container throws error 500 with Post title permalinks

I am trying to host WordPress blog under a subdirectory URL like:

https://example.com/blog

Because I have another application running on https://example.com/ Everything is under a Nginx reverse proxy

To do so, I created the following environment:

Docker Compose.yml

# docker-compose.yml
version: '3'

services:
    wordpress_seo:
        image: wordpress
        restart: always
        ports:
            - 8094:80
        environment:
            WORDPRESS_DB_HOST: db-wordpress_seo
            WORDPRESS_DB_USER: ${MYSQL_WORDPRESS_USER}
            WORDPRESS_DB_PASSWORD: ${MYSQL_WORDPRESS_PASSWORD}
            WORDPRESS_DB_NAME: ${MYSQL_DATABASE}-wordpress
        volumes:
            - ${STORAGE_PATH}/wordpress:/var/www/html
        networks:
            - env-dev

    db-wordpress_seo:
        image: mariadb:10.1.41
        restart: always
        environment:
            MYSQL_DATABASE: ${MYSQL_DATABASE}-wordpress
            MYSQL_USER: ${MYSQL_WORDPRESS_USER}
            MYSQL_PASSWORD: ${MYSQL_WORDPRESS_PASSWORD}
            MYSQL_RANDOM_ROOT_PASSWORD: '1'
        volumes:
            - ${STORAGE_PATH}/wordpress-data:/var/lib/mysql
        networks:
            - env-dev

networks:
    env-dev:
        external:
            name: env-dev

With this config inside my server Nginx config:

    location /blog/ {
        proxy_pass http://127.0.0.1:8094/;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_read_timeout    90;
        proxy_connect_timeout 90;
        proxy_redirect        off;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Port 443;

       proxy_set_header Proxy "";
    }

Then I modified the wp-config.php by adding this:

define('WP_HOME','https://example.com/blog');
define('WP_SITEURL','https://example.com/blog');

if (strpos($_SERVER['REQUEST_URI'], '/blog/wp-admin/') === false) {
    $_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);
}// If we're behind a proxy server and using HTTPS, we need to alert WordPress of that fact

// see also https://wordpress.org/support/article/administration-over-ssl/#using-a-reverse-proxy
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
    $_SERVER['HTTPS'] = 'on';
}

Installation went well, and I could access to articles with this kind of permalink:

https://example.com/blog/?p=1

However, I want now to use post title permalinks. So when I do the switch inside WordPress config panel, posts suddenly throw a 500 error, for example if I access this URL:

https://example.com/blog/bonjour-tout-le-monde/

I checked for the .htaccess file in my container which seems to be fine:

# BEGIN WordPress
# Les directives (lignes) entre « BEGIN WordPress » et « END WordPress » sont générées
# dynamiquement, et doivent être modifiées uniquement via les filtres WordPress.
# Toute modification des directives situées entre ces marqueurs sera surchargée.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /freelance/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /freelance/index.php [L]
</IfModule>

# END WordPress

When I check for logs in my WordPress container with huge verbose activated, I get the following (I removed some parts because it's just a repeating pattern):

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.
r->uri = /blog/index.php, referer: https://example.com/blog/wp-admin/edit.php
redirected from r->uri = /blog/index.php, referer: https://example.com/blog/wp-admin/edit.php
redirected from r->uri = /blog/index.php, referer: https://example.com/blog/wp-admin/edit.php
redirected from r->uri = /blog/index.php, referer: https://example.com/blog/wp-admin/edit.php
redirected from r->uri = /blog/index.php, referer: https://example.com/blog/wp-admin/edit.php
redirected from r->uri = /blog/index.php, referer: https://example.com/blog/wp-admin/edit.php
redirected from r->uri = /blog/index.php, referer: https://example.com/blog/wp-admin/edit.php
redirected from r->uri = /blog/index.php, referer: https://example.com/blog/wp-admin/edit.php
redirected from r->uri = /blog/index.php, referer: https://example.com/blog/wp-admin/edit.php
redirected from r->uri = /blog/index.php, referer: https://example.com/blog/wp-admin/edit.php
redirected from r->uri = /bonjour-tout-le-monde/, referer: https://example.com/blog/wp-admin/edit.php
Response sent with status 500, referer: https://example.com/blog/wp-admin/edit.php

Do you have any idea where the bug is coming from?

Upvotes: 1

Views: 882

Answers (1)

Prog_is_life
Prog_is_life

Reputation: 884

For those wondering what is the solution, you have to edit the .htaccess file of wordpress and override wordpress automatically generated rules.

This is my .htaccess file now :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>


# BEGIN WordPress
# Les directives (lignes) entre             BEGIN WordPress             et             END WordPress             sont g      n      r      es
# dynamiquement, et doivent       tre modifi      es uniquement via les filtres WordPress.
# Toute modification des directives situ      es entre ces marqueurs sera surcharg      e.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress

Upvotes: 1

Related Questions