【问题标题】:WordPress in sub-directory is serving root index.php on nginx fastcgi子目录中的 WordPress 在 nginx fastcgi 上提供根 index.php
【发布时间】:2015-12-15 11:56:41
【问题描述】:

我在我的 NGINX 服务器上遇到了一个奇怪的问题。

如 Codex 中所述,我将 wordpress 从根目录移至子目录 /blog/。

它成功显示了博客索引,但如果我想显示其他内容,例如特定的帖子或存档页面,它会为根 index.php 提供服务

即使不存在的 URL 也被用作根 index.php

如果我删除根 index.php,它会返回 404 错误。

也许是因为我的 nginx 和 fastcgi 设置,但我真的不知道:

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

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

    client_max_body_size 10M;

    # Make site accessible from http://localhost/
    server_name


            set $no_cache 0;
            if ($request_method = POST){set $no_cache 1;}
            if ($query_string != ""){set $no_cache 1;}
            if ($http_cookie = "PHPSESSID"){set $no_cache 1;}
            if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {set $no_cache 1;}
            if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in"){set $no_cache 1;}



    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_cache  microcache;
            fastcgi_cache_key $scheme$host$request_uri$request_method;
            fastcgi_cache_valid 200 301 302 30s;
            fastcgi_cache_use_stale updating error timeout invalid_header http_500;
            fastcgi_pass_header Set-Cookie;
            fastcgi_no_cache $no_cache;
            fastcgi_cache_bypass $no_cache;
            fastcgi_pass_header Cookie;
            fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }


    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

}

编辑:用整个服务器块更新

【问题讨论】:

  • copy 你的.httacces 到根了吗?您是否使用永久链接重建了它?你更新了你的ìindex.php`路径吗?
  • 服务器 {} 块的其余部分是什么。您粘贴的代码显示了以 .php 结尾的请求会发生什么,但您的示例不以 .php 结尾。
  • 我已经编辑并粘贴了整个服务器块。 @史蒂夫E。我需要编辑位置/部分吗?

标签: php wordpress nginx fastcgi


【解决方案1】:

try_files 语句将所有不存在的文件发送到您的 Web 根目录中的 index.php。将专门为您的博客添加到配置的第二个位置:

location /blog {
    try_files $uri $uri/ /blog/index.php;
}

try_files 的工作原理如下:

按指定顺序检查文件是否存在并使用 首次找到的用于请求处理的文件;执行处理 在当前情况下。文件的路径由 根据根和别名指令的文件参数。它是 可以通过在 名称的结尾,例如“$uri/”。如果没有找到任何文件,则 内部重定向到最后一个参数中指定的 uri。 例如:

【讨论】:

  • 你是对的,谢谢!我将您的答案添加到 nginx 站点可用文件夹中的配置文件中,现在它显示所有这些 URL 的 404 错误。您在答案中忘记了 /,它应该说 try_files $uri $uri/ /blog/index.php 以使用漂亮的永久链接
猜你喜欢
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 2015-08-05
  • 1970-01-01
  • 2015-09-18
相关资源
最近更新 更多