![尝试不存在的文件夹时 Nginx 不显示 404](https://linux22.com/image/773274/%E5%B0%9D%E8%AF%95%E4%B8%8D%E5%AD%98%E5%9C%A8%E7%9A%84%E6%96%87%E4%BB%B6%E5%A4%B9%E6%97%B6%20Nginx%20%E4%B8%8D%E6%98%BE%E7%A4%BA%20404.png)
当尝试访问不存在的文件夹(即 https://domain/nonexistent)时,它只会加载根 index.php。
尝试加载特定文件(https://domain/nonexistent/index.php)时确实显示 404。
这是我的虚拟主机配置:
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
{{ssl_certificate_key}}
{{ssl_certificate}}
server_name (domain);
{{root}}
{{nginx_access_log}}
{{nginx_error_log}}
if ($bad_bot = 1) {
return 403;
}
if ($scheme != "https") {
rewrite ^ https://$host$uri permanent;
}
location ~ /.well-known {
auth_basic off;
allow all;
try_files $uri =404;
}
{{basic_auth}}
try_files $uri $uri/ /index.php?$args;
index index.php index.html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri =404;
fastcgi_read_timeout 3600;
fastcgi_send_timeout 3600;
fastcgi_param HTTPS $fastcgi_https;
{{php_fpm_listener}}
{{php_settings}}
}
location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm|webp|zip|swf)$ {
add_header Access-Control-Allow-Origin "*";
expires max;
access_log off;
}
if (-f $request_filename) {
break;
}
}
我尝试寻找解决方案但似乎我的问题太具体了。
答案1
缺少的是 =404:
try_files $uri $uri/ /index.php?$args;
改成:
try_files $uri $uri/ /index.php?$args =404;
问题解决了。