【问题标题】:Nginx with worpress as sub-directory returning 404 in postsNginx 与 wordpress 作为子目录在帖子中返回 404
【发布时间】:2023-04-06 09:45:02
【问题描述】:

在Wordpress子目录中添加各种配置后,我可以毫无问题地访问WP主页,但我仍然无法访问帖子,返回404。

www.test.com/blog - WP 主页,运行良好

www.test.com/blog/test-post-for-blog - WP Post,404

这里是配置:

server {
  listen 80;
  listen [::]:80;

  server_name www.test.com;

  location ^~ /media {
    alias /var/local/test/static/media;
  }

  location ^~ /icons {
    alias /var/local/test/static/icons;
  }

  location /blog {
    alias /var/www/html;
    index  index.html index.htm index.php;
    try_files $uri $uri/ /blog/index.php?$args /blog/index.php?q=$uri&$args;
    # try_files $uri $uri/ /blog/index.php?q=$uri$args;

    location ~ \.php$ {
      try_files $uri =404;
      include fastcgi_params;
      fastcgi_intercept_errors on;
      fastcgi_index index.php;
      fastcgi_pass blog-test:9000;
      fastcgi_split_path_info ^(/blog)(/.*)$;
      fastcgi_param SCRIPT_FILENAME $request_filename;
      fastcgi_param PATH_INFO $fastcgi_path_info;
    }
  }
}

还有日志:

2022/02/09 14:47:25 [error] 32#32: *45 open() "/etc/nginx/html/index.php" failed (2: No such file or directory), client: 10.10.10.10, server: www.test.com, request: "GET /blog/test-post-for-blog/ HTTP/1.1", host: "www.test.com", referrer: "https://www.test.com/blog/"
10.10.10.10 - admin [09/Feb/2022:14:47:25 +0000] "GET /blog/test-post-for-blog/ HTTP/1.1" 404 187 "https://www.test.com/blog/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Safari/537.36"

查看日志,好像找错地方了,是/etc/nginx/html/index.php,但是如果我在里面加了别名php location 块,然后主页也将停止工作,这让我有点困惑。

目前,我不太确定问题是否是博客块中的 alias (nginx 建议不要将 aliastry_files一起使用>,显然是由于错误)或其他任何原因。如果确实是 alias 指令,我会尝试添加 root,并加上一些重写规则,以避免修改文件结构。

我确实花了很多时间才弄清楚,但仍然无法真正理解这里可能出现的问题。

更新 1:

这非常奇怪。使用以下博客配置,将 php 位置与博客分开,WP 帖子可以访问,但不能访问主页和管理员,返回 404:

  location /blog {
    alias /var/www/html;
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ \.php$ {
    alias /var/www/html;
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_intercept_errors on;
    fastcgi_index index.php;
    fastcgi_pass blog-test:9000;
    fastcgi_split_path_info ^(/blog)(/.*)$;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }

更新 2:

现在可以了(可以访问博客和帖子),但管理员似乎进入了重定向循环:

  location /blog {
    alias /var/www/html;
    index index.php;
    try_files $uri $uri/ /blog/index.php?q=$uri&$args;
  }

  location ~ \.php$ {
    alias /var/www/html;
    try_files $uri $uri/ /index.php?$args;
    include fastcgi_params;
    fastcgi_intercept_errors on;
    fastcgi_index index.php;
    fastcgi_pass blog-test:9000;
    fastcgi_param SCRIPT_FILENAME $request_filename;
  }

更新 3 和修复:

这显然可行,但配置似乎真的令人不快且未优化。如果我尝试将 php 位置嵌套到 blog 块中,则 php 文件将下载而不是渲染。如果我尝试使用别名而不是 root,某些页面将不会显示,导致 404。无论如何,这似乎是有效的:

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

  location /blog {
    rewrite ^/blog(.*)$ /$1 break;
    try_files $uri $uri/ /blog/index.php?$args;
  }
          
  location ~ \.php$ {
    rewrite ^/blog(.*)$ $1 break;
    fastcgi_pass blog-test:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_split_path_info ^(/blog)(/.*)$;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }

【问题讨论】:

  • 据我了解:您的整个 wordpress 安装在 blog 目录中吗?或者您是否尝试通过blog slug 显示帖子存档?
  • @JosFaber,是的,/blog 应该指向整个 wordpress 安装。当前的 nginx 设置也用于提供一些静态文件,与 wordpress 配置完全无关。

标签: php wordpress nginx http-status-code-404 permalinks


【解决方案1】:

在 /blog 目录之外,您可能还有 php 文件。应该有一个单独的块用于具有相同设置的 wordpress 安装,但随后用于博客目录。

这可能是 wordpress 的块(在 Docker 中测试)

  location /blog {
    try_files $uri $uri/ /blog/index.php?$args;
    
    location ~ \.php$ {
      fastcgi_split_path_info ^/blog/(.+\.php)(/.+)$;
      fastcgi_pass wp:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
  }

所以你完整的 nginx 配置应该是这样的:

server {
  listen 80;
  listen [::]:80;

  server_name www.test.com;
  root /var/www/html; 
  index index.php; 

  location ^~ /media {
    alias /var/local/test/static/media;
  }

  location ^~ /icons {
    alias /var/local/test/static/icons;
  }

  # This is for Wordpress
  location /blog {
    try_files $uri $uri/ /blog/index.php?$args;
    
    location ~ \.php$ {
      fastcgi_split_path_info ^/blog/(.+\.php)(/.+)$;
      fastcgi_pass wp:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
  }

  # This is for php files in the root. If there is no php to be parsed there you could leave these blocks out.
  location / {
    try_files $uri $uri/ /index.php?$args;
  }
  
  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass wp:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  }
}

更新:我在 nginx 服务器配置中添加了 rootindex 属性。

【讨论】:

  • 博客本身就是wordpress,所以我其实不需要单独配置。
  • 然后你可以把那些底部位置块排除在外,你的配置就可以正确设置为 /blog
  • 是的,但它几乎是一样的,它不会修复帖子的错误。
  • 我已经在 Docker 环境中测试过答案,所以肯定还有别的。您是否正确设置了 WP_SITEURL 和 WP_HOME?
  • 为了确保这一点,实际的 wordpress 安装必须位于 docroot 中的 blog 目录中。因此,如果将 nginx 设置为从 /var/www/html 提供服务,则应将 wordpress 安装在 /var/www/html/blog 中。
【解决方案2】:

更新 3 和修复:

这显然可行,但配置似乎真的令人不快且未优化。如果我尝试将 php 位置嵌套到 blog 块中,则 php 文件将下载而不是呈现。如果我尝试使用别名而不是 root,某些页面将不会显示,从而导致 404。无论如何,这似乎是有效的:

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

  location /blog {
    rewrite ^/blog(.*)$ /$1 break;
    try_files $uri $uri/ /blog/index.php?$args;
  }
          
  location ~ \.php$ {
    rewrite ^/blog(.*)$ $1 break;
    fastcgi_pass blog-test:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_split_path_info ^(/blog)(/.*)$;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-13
    • 2013-06-21
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    相关资源
    最近更新 更多