我在 Nginx 上配置了我的 WordPress 网站,一切运行正常,但对于不存在的 .php 页面,它们会返回 nginx 默认 404 页面未找到,为什么?
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/vpsproject.site;
# Add index.php to the list if you are using PHP
index index.php;
server_name vpsproject.site;
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.4-fpm:
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
# deny access to .git related folders or files
#
location ~ /\.git {
deny all;
}
答案1
这是因为 nginx 将文件请求发送.php
给监听套接字的 PHP-FPM 进程unix:/run/php/php7.4-fpm.sock
。然后 PHP-FPM 进程检查文件是否存在,如果不存在,则返回 404 状态代码。
同时,其他所有 URL 的默认请求流程是 nginx 首先检查文件或目录是否存在。如果存在,则发送内容。
否则,nginx 将请求发送到/index.php
,然后通过套接字发送到 PHP-FPM 进程。
然后 WordPress 前端控制器处理请求,如果未找到则提供其自己的 404 页面。