WordPress 访问 503 与 Nginx 代理配置问题解决

1059 字
5 分钟
WordPress 访问 503 与 Nginx 代理配置问题解决

问题描述#

访问 WordPress 登录页 http://47.100.36.235/wp-login.php 时,页面返回 Spring Boot 风格的 Whitelabel 错误页面:

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Not Found, status=404).

修改配置后,页面变为 WordPress 维护模式提示,状态码 503:

正在执行例行维护,请一分钟后回来。

环境分析#

服务器架构涉及以下组件:

组件说明
Nginx监听 80/81/82/83 端口,80 为公网入口
Spring Boot监听 127.0.0.1:8080,处理业务 API 及非 PHP 请求
WordPress安装于 /home/wwwroot/wordpress,依赖 PHP-FPM
PHP-FPM监听 Unix Socket /tmp/php-cgi.sock

根因#

Nginx 的 80 端口配置中,location / 将所有请求(包括 *.php)全部代理至 Spring Boot(http://127.0.0.1:8080),导致 WordPress 的 PHP 脚本被错误转发到 Java 后端。Spring Boot 无法处理 .php 路由,返回 404 Whitelabel 错误。

后续修正 PHP 请求转发后出现维护页面,是因为 WordPress 根目录下存在 .maintenance 文件,表明之前发生过中断的自动更新,导致站点进入维护模式。

排查过程#

检查 Nginx 配置#

查看当前 Nginx 配置文件,发现 80 端口的 server 块仅包含:

location / {
proxy_pass http://127.0.0.1:8080;
...
}

未配置任何 location ~ \.php$ 规则,导致所有 .php 请求均被代理到 Spring Boot。

确定 WordPress 安装路径#

/home/wwwroot/wordpress/wp-config.php
find / -name wp-config.php 2>/dev/null

确认 PHP-FPM 状态及监听方式#

Terminal window
systemctl status php-fpm
# 显示 active (running)
grep "^listen" /usr/local/php/etc/php-fpm.conf
# 输出:listen = /tmp/php-cgi.sock

确认 PHP-FPM 使用 Unix Socket 通信,非 TCP 端口。

配置文件换行符问题#

vim 打开 Nginx 配置文件时,行尾出现大量 ^M 字符(Windows CRLF 格式),可能导致 Nginx 解析错误。

CRLF 换行符隐患

跨平台编辑配置文件时,Windows 的 CRLF 换行符会导致 Linux 上的 Nginx 解析异常。建议在 Linux 服务器上直接编辑,或设置编辑器默认使用 LF 换行符。

排查维护模式#

在修正 PHP 转发后访问出现”正在执行例行维护”页面,检查 WordPress 根目录存在 .maintenance 文件。

解决方案#

修改 Nginx 配置,增加 PHP 处理规则#

在监听 80 端口的 server 块中,在 location / 之前添加专门处理 .php 请求的 location:

server {
listen 80;
server_name intmotecare.com;
# PHP 请求转发至 PHP-FPM
location ~ \.php$ {
root /home/wwwroot/wordpress;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 其他请求代理到 Spring Boot
location / {
proxy_pass http://127.0.0.1:8080;
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;
}
}
Location 匹配优先级

Nginx 先匹配精确前缀(=),再匹配正则(~),最后匹配普通前缀。将 location ~ \.php$ 放在 location / 之前确保正则优先匹配 .php 请求,避免被通用规则捕获。

处理配置文件换行符#

Terminal window
yum install dos2unix -y
dos2unix /usr/local/nginx/conf/nginx.conf

或通过 Vim 内部命令:

:set fileformat=unix
:wq

删除 WordPress 维护模式标记文件#

Terminal window
rm -f /home/wwwroot/wordpress/.maintenance
Warning

删除 .maintenance 文件前请确保 WordPress 更新进程已结束,否则可能导致数据库损坏。

验证并重载 Nginx#

Terminal window
nginx -t # 检查语法
nginx -s reload # 重载配置

验证结果#

  • 访问 http://47.100.36.235/wp-login.php,成功显示 WordPress 登录页面
  • 访问 http://47.100.36.235(非 PHP 路径),正常代理至 Spring Boot 应用
  • 无 404 或 503 错误

总结#

问题根源#

  • Nginx location 优先级:未明确区分动态 PHP 请求,导致错误转发到 Spring Boot
  • 文件格式:跨平台编辑导致配置文件包含 Windows 换行符,影响 Nginx 解析
  • WordPress 维护文件残留:更新中断后未自动清理,阻塞访问

预防措施#

  1. 规划 location 匹配顺序:将特定后缀(如 .php)的 location 放在通用 location / 之前,确保精确匹配优先
  2. 统一换行符:在 Linux 服务器上直接编辑配置文件,或使用编辑器设置默认换行符为 LF
  3. 监控 WordPress 更新:若更新卡住,定期检查并手动清理 .maintenance 文件(确保更新进程已结束)
  4. 日志监控:关注 Nginx 错误日志(/var/log/nginx/error.log)和 PHP-FPM 日志

命令速查#

操作命令
查找 WordPress 根目录find / -name wp-config.php 2>/dev/null
检查 PHP-FPM 状态systemctl status php-fpm
查看 PHP-FPM 监听方式grep "^listen" /usr/local/php/etc/php-fpm.conf
转换文件换行符dos2unix /path/to/nginx.conf
删除维护文件rm -f /home/wwwroot/wordpress/.maintenance
检查 Nginx 配置nginx -t
重载 Nginxnginx -s reload

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
WordPress 访问 503 与 Nginx 代理配置问题解决
https://wwkcim.xyz/posts/wordpress-503-nginx-fix/
作者
wwk
发布于
2026-07-23
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
wwk
Hello, I'm wwk.
公告
欢迎来到我的博客!这是一则示例公告。
音乐
封面

音乐

暂未播放

0:000:00
暂无歌词
分类
标签
站点统计
文章
16
分类
5
标签
34
总字数
17,180
运行时长
0
最后活动
0 天前
站点信息
构建平台
Local
博客版本
Firefly v6.13.5
文章许可
CC BY-NC-SA 4.0

文章目录