我在 Ubuntu 16.04 机器上设置了一个 LEMP 环境,按照如何在 Ubuntu 16.04 上使用 LEMP 安装 WordPressDigitalOcean 社区档案上的教程。所有功能都很好,除了当导航到不存在的页面时(例如https://www.example.com/page-that-doesnt-exist/
:),它会返回 200 并呈现主页(没有 301 重定向),而不是返回 404。但是,当导航到不存在的范围下的页面时/blog/
,则会返回 404,正如您预期的那样(例如https://www.example.com/blog/post-that-doesnt-exist/
:)。
这不是 WordPress 的预期行为,对吧?
以下是使用的 Nginx 配置:
# Virtual Host configuration for www.example.com
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
server { # Redirect http:// to https://
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/nginx/ssl/example.com.au/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/example.com.key;
root /var/www/www.example.com;
index index.php index.html;
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
答案1
你分享的nginx配置没有问题,请检查一下wordpress配置中的404页面设置。