答案是肯定的,有一種簡單的方法可以將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錯誤頁面的路徑。
說明:
error_page
- 指定該行屬於HTTP錯誤頁面。
404
- 此頁面將響應的特定HTTP錯誤代碼。
/404.html
- 包含文件名的文件的「根相對」路徑。
- 如果您的根目錄不同於上述目錄,您需要更改此文件路徑以反映差異(如上所述)。
然而,如果你去的路徑上面,你會發現,有是在目錄名稱的文件。因此,將顯示默認的nginx錯誤頁面。
要改變這種情況,只需製作一個自定義的HTML頁面,它將成爲您的404錯誤頁面,並將其作爲「404.html」保存在根目錄中。然後,出現404錯誤時,上述路徑(error_page 404 /404.html;
)將能夠顯示您的自定義頁面。
我相當肯定,你可以添加多個相同的一般錯誤頁面必要的「error_page」線任何其他HTTP錯誤代碼,你不妨以支付或路由(但我沒有試過這個)。另外,我相當肯定這個設置對於其他服務器類型是一樣的,但是我還沒有嘗試過。
請讓我知道這是不正確的(或可以做得更好),或者如果這可能會導致更遠的路上意外的問題。