You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu+Nginx环境下部分页面/目录出现403 Forbidden错误求助

WordPress站点Nginx 403错误排查与解决方案

问题现象

  • 访问主域名www.subdomain.com返回403 Forbidden,访问www.subdomain.com/wordpress可正常进入安装页
  • WordPress登录后跳转至/wp-admin/报403,手动输入/wp-admin/index.php可正常访问
  • Nginx错误日志提示「目录索引被禁止」

排查与解决步骤

1. 修正目录所有者与权限

仅设置chmod 755无法确保Nginx进程拥有访问权限,需将目录所有者改为Nginx运行用户(默认www-data):

sudo chown -R www-data:www-data /var/www/subdomain.com

同时统一文件与目录权限:

sudo find /var/www/subdomain.com -type d -exec chmod 755 {} \;
sudo find /var/www/subdomain.com -type f -exec chmod 644 {} \;

2. 调整Nginx配置解决目录索引问题

当前配置中,try_files会优先尝试访问目录,而目录索引被禁用会触发403。可通过两种方式修复:

  • 方式一:指定默认索引文件
    在server块中添加index指令,明确将index.php作为默认入口:
upstream subdomain-php-handler {
    server unix:/var/run/php/php8.1-fpm.sock;
}
server {
    listen 80;
    server_name subdomain.com www.subdomain.com;
    root /var/www/subdomain.com/wordpress;
    index index.php index.html index.htm; # 新增该行
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass subdomain-php-handler;
    }
}
  • 方式二:优化try_files规则
    跳过目录访问直接解析PHP入口,更贴合WordPress的单页应用特性:
location / {
    try_files $uri /index.php?$args; # 移除$uri/
}

修改配置后验证并重启Nginx:

sudo nginx -t && sudo systemctl restart nginx

3. 更新WordPress固定链接设置

登录WordPress后台,进入设置→固定链接,选择「文章名称」或其他非默认结构,点击保存。这会同步WordPress内部的链接解析规则,避免因路径跳转触发403。

4. 验证PHP-FPM权限一致性

检查PHP-FPM运行用户是否与Nginx一致:
打开/etc/php/8.1/fpm/pool.d/www.conf,确认以下参数:

user = www-data
group = www-data
listen.owner = www-data
listen.group = www-data

重启PHP-FPM服务:

sudo systemctl restart php8.1-fpm

内容的提问来源于stack exchange,提问作者M Christiansen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 23:35:30