首先,第一次使用 nginx 用户。所以,我仍在学习 Apache 和 nginx 的区别。
我有一个 nginx 的库存安装。 (apt-get install nginx-full)我修改了在“/etc/nginx/sites-enabled/default”中找到的默认配置。然而,错误页面根本不起作用。我的文档根目录是/server/www,我的错误页面位于目录/server/errors/中。下面是我的配置。
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /server/www;
index index.html;
server_name website.com;
location / {
try_files $uri $uri/ /index.html;
}
error_page 403 404 405 /40x.html;
location /40x.html {
root /server/errors;
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /server/errors;
internal;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
所有应该生成403或404页面的页面,只需加载index.html文件即可。我相信可能会发生 500 个错误,但是生成起来有点困难。
此外,如果相关的话,我将错误页面移至 docroot 上方,但在此之前它们无法正常工作。我之所以采取这一行动,是因为“内部”标志似乎并未像文档声称的那样使页面成为内部页面,因为我仍然可以直接访问它们。
您能提供的任何建议/帮助都会很棒!
最佳答案
该错误问题与位置/结束值/index.html 有关。本质上,nginx 首先尝试将 uri 作为文件,然后作为目录,然后恢复为 index.html。相反,我希望它返回 404。所以我将其更改为下面的内容,现在它可以工作了。
location / {
try_files $uri $uri/ =404;
}
关于nginx 配置默认错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22162274/