答案是肯定的,有一种简单的方法可以将 404 页面自定义为 HTML 文件,至少在 LEMP 堆栈上是这样。假设您没有重新组织“太多”文件,唯一需要做的就是创建一个文件。
说明:
如果你去/etc/nginx/sites-available/default.conf 并打开它,你会注意到一个看起来像这样的块:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost unaviamedia.ca;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
.......
}
免责声明:
从这一行可以看出:root /usr/share/nginx/html;,我的主目录是“/usr/share/nginx/html”。如果您的不同,您将需要更改我提到的下一个文件路径。
注意到这一行:error_page 404 /404.html;,在页面中间?这是您网站的 404 HTML 错误页面的路径。
说明:
-
error_page - 指定此行与 HTTP 错误页面有关。
-
404 - 此页面将响应的特定 HTTP 错误代码。
-
/404.html - 文件的“相对根”路径,包括文件名。
- 如果您的根目录与上述不同,则需要更改此文件路径以反映差异(如上所述)。
但是,如果您转到上面的路径,您会注意到目录中没有该名称的文件。所以会显示默认的nginx错误页面。
要改变这一点,只需制作一个自定义 HTML 页面,该页面将成为您的 404 错误页面,并将其作为“404.html”保存在您的根目录中。那么上面的路径(error_page 404 /404.html;)就可以在发生404错误时显示你的自定义页面了。
我相当确定您可以为您可能希望涵盖的任何其他 HTTP 错误代码添加必要的“error_page”行,或者将几个路由到同一个通用错误页面(但我没有尝试过)。此外,我很确定其他服务器类型的设置将相同,但我还没有尝试过。
请让我知道这是否不正确(或可以做得更好),或者这是否会导致进一步的意外问题。