我在我的 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
}
}
编辑:已更新整个服务器块
答案1
try_files 语句将所有不存在的文件发送到 Web 根目录中的 index.php。我需要在配置中添加第二个专门用于博客的位置:
location /blog {
try_files $uri $uri/ /blog/index.php;
}
现在它运行正常。
(感谢 Steve E. 在堆栈溢出)