Docker容器WordPress子目录启用文章标题永久链接报500错误
WordPress子目录部署反向代理后固定链接500错误原因及修复
问题背景
在Nginx反向代理环境下,将WordPress部署在子目录https://example.com/blog,根目录运行其他应用,通过Docker Compose搭建WordPress+MariaDB环境。配置反向代理规则后,使用?p=1形式的固定链接可正常访问,但切换为文章标题形式后出现500错误,容器日志显示内部重定向超过10次。
核心原因
- Nginx代理URI传递错误:反向代理配置中
proxy_pass http://127.0.0.1:8094/末尾的斜杠导致请求被转发时丢失了/blog前缀。比如用户访问https://example.com/blog/bonjour-tout-le-monde/,容器收到的请求是/bonjour-tout-le-monde/,但WordPress配置的WP_HOME和WP_SITEURL带/blog前缀,WP会尝试将无前缀的请求重定向到带前缀的地址,触发循环重定向。 - .htaccess配置不匹配:容器内的
.htaccess中RewriteBase和RewriteRule写的是/freelance/,和实际子目录/blog完全不符,导致WordPress的URL重写规则无法正确解析请求,进一步加剧循环。 - wp-config.php冗余代码干扰:手动添加的
/wp-admin/URI替换逻辑,在代理配置错误的情况下,会打乱WP正常的请求路径识别,加重循环问题。
修复步骤
1. 修正Nginx反向代理配置
去掉proxy_pass末尾的斜杠,让Nginx保留/blog前缀转发请求:
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 ""; }
2. 修正.htaccess配置
将.htaccess中的/freelance/替换为/blog/:
# 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
或者直接登录WordPress后台,进入「设置-固定链接」页面重新保存一次,让系统自动生成正确的重写规则。
3. 清理wp-config.php冗余代码
删除那段手动替换/wp-admin/的逻辑,修正代理配置后WP能自动识别子目录前缀:
define('WP_HOME','https://example.com/blog'); define('WP_SITEURL','https://example.com/blog'); if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) { $_SERVER['HTTPS'] = 'on'; }
内容的提问来源于stack exchange,提问作者Prog_is_life
相关产品推荐
相关产品推荐

