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

访问WordPress文章时出现‘404 Not Found Nginx’问题求助

NodeJS主站与子目录WordPress伪静态文章无法访问的Nginx配置修复

我在域名根目录(example.com)部署了NodeJS应用,近7天尝试在子目录(example.com/blog)添加WordPress博客。博客主页可正常访问,但文章页面打不开。只有在WordPress设置中将固定链接设为纯静态(Plain)时才能访问文章,但这种方式对SEO不友好。

错误日志

2022/09/02 15:43:43 [error] 86838#86838: *6 "/var/www/html/example.com/blog/advantages-of-social-media-2/index.php" is not found (2: No such file or directory), client: 142.52.23.144, server: www.example.com, request: "GET /blog/advantages-of-social-media-2/ HTTP/1.1", host: "www.example.com", referrer: "https://www.example.com/blog/"

环境信息

  • WordPress安装路径:/var/www/html/example.com/blog
  • 当前Nginx配置未添加/blog路径时可访问博客主页,添加后反而无法打开文章

当前不含SSL的Nginx配置

server {
       server_name example.com;
       return 301 https://www.example.com$request_uri;    
}

server {
        server_name www.example.com;

        # NodeJS App
        location / {
                proxy_pass http://localhost:3000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $http_cf_connecting_ip;
                proxy_set_header X-Forwarded-For $http_x_forwarded_for;
                proxy_cache_bypass $http_upgrade;
        }

        # Wordpress Blog
        location /blog {
                access_log /var/log/nginx/blog_access.log;
                error_log /var/log/nginx/blog_error.log;

                root /var/www/html/example.com;
                index index.php;

                # Add a trailing slash if missing
                if (!-f $request_filename) {
                        rewrite [^/]$ $uri/ permanent;
                }
                
                # try_files $uri $uri/ /index.php?$args;
                # try_files $uri $uri/ /blog/index.php?q=$uri&$args;
                try_files $uri $uri/ /blog/index.php?$args;

                location ~ \.php {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;

                        fastcgi_index index.php;
                        # Change this to your fpm socket
                        fastcgi_pass  unix:/var/run/php/php8.1-fpm.sock;

                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_param PATH_INFO $fastcgi_path_info;
                        include fastcgi_params;
                }
        }
}

问题分析

从错误日志能看出,Nginx错误地尝试在文章路径(/blog/advantages-of-social-media-2/)下找index.php,但实际上WordPress的核心index.php在/blog/目录下,伪静态路径需要重写到这个index.php来处理。当前配置的问题在于:

  1. 嵌套的location ~ \.php继承了外层的root,但当处理伪静态转发过来的请求时,$fastcgi_script_name会包含完整的/blog/advantages-of-social-media-2/index.php,导致SCRIPT_FILENAME指向不存在的文件
  2. if语句的使用可能干扰try_files的正常逻辑,Nginx中应尽量避免用if处理这类场景

修复后的Nginx配置

server {
       server_name example.com;
       return 301 https://www.example.com$request_uri;    
}

server {
        server_name www.example.com;

        # NodeJS App
        location / {
                proxy_pass http://localhost:3000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $http_cf_connecting_ip;
                proxy_set_header X-Forwarded-For $http_x_forwarded_for;
                proxy_cache_bypass $http_upgrade;
        }

        # Wordpress Blog
        location ^~ /blog {
                access_log /var/log/nginx/blog_access.log;
                error_log /var/log/nginx/blog_error.log;

                root /var/www/html/example.com;
                index index.php;

                # 处理伪静态,直接转发到/blog/index.php
                try_files $uri $uri/ /blog/index.php?$args;

                location ~ \.php$ {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(/blog/.+\.php)(/.+)$;

                        fastcgi_index index.php;
                        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;

                        # 确保SCRIPT_FILENAME指向正确的文件路径
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_param PATH_INFO $fastcgi_path_info;
                        # 明确WordPress处理请求的脚本路径,适配子目录部署
                        fastcgi_param SCRIPT_NAME /blog/index.php;
                        include fastcgi_params;
                }
        }
}

关键修改点说明

  • 使用location ^~ /blog确保该路径优先级高于正则表达式location,避免NodeJS的location干扰
  • 移除了容易引发问题的if语句,try_files结合WordPress后台设置可处理尾随斜杠需求
  • 调整fastcgi_split_path_info的正则,匹配/blog开头的php文件路径,确保PATH_INFO解析正确
  • 添加fastcgi_param SCRIPT_NAME /blog/index.php,明确告诉WordPress处理请求的脚本路径,适配子目录部署场景

后续操作

  1. 保存修改后的配置,执行nginx -t验证配置语法正确
  2. 重启Nginx服务:systemctl restart nginx
  3. 进入WordPress后台,确认固定链接设置为非纯静态模式(如“文章名”),无需修改其他路径设置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 13:57:33