Nginx尝试加载不存在的目录
当我键入mydomain.com/randomfolder或domain.com/somefolder时,它只加载索引页 我的问题是,有没有更好的方法将其配置为将用户重定向到错误页面,而不仅仅是尝试加载目录或处理请求Nginx尝试加载不存在的目录,nginx,Nginx,当我键入mydomain.com/randomfolder或domain.com/somefolder时,它只加载索引页 我的问题是,有没有更好的方法将其配置为将用户重定向到错误页面,而不仅仅是尝试加载目录或处理请求 server { #listen 80 default_server; #listen [::]:80 default_server ipv6only=on; listen 443 ssl; root /var/www; autoindex on; index index.htm
server {
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
root /var/www;
autoindex on;
index index.html index.htm index.php;
server_name mydomain.com;
ssl_certificate /etc/nginx/ssl/mydomain.com.crt;
ssl_certificate_key /etc/nginx/ssl/mydomain.com.key;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /var/www/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 300;
fastcgi_index index.php;
include fastcgi.conf;
}
这是发布的配置的正常行为 如果您想改变这一点,违反的指令是:
try_files $uri $uri/ /index.php;
如果你的指令冒犯了你,就把它拔出来
这是一种冒犯,因为它说对每个http请求执行以下操作:
/
/index.php
但是,在请求失败之前,您必须清除
try\u文件中的/index.php
,甚至整行,只有在失败时才会显示自定义404。这是发布配置的正常行为
如果您想改变这一点,违反的指令是:
try_files $uri $uri/ /index.php;
如果你的指令冒犯了你,就把它拔出来
这是一种冒犯,因为它说对每个http请求执行以下操作:
首先按原样尝试请求
然后尝试在末尾添加一个/
最后,尝试取回/index.php
你可以砍掉第三个项目,或者完全扔掉它
您还可以添加:
但是,在请求失败之前,您必须清除try_files
中的/index.php
,甚至整个行,只有在请求失败之后,自定义404才会显示出来。谢谢Paul完全清除行似乎是个诀窍。谢谢Paul完全清除行似乎是个诀窍。