新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Nginx設(shè)置404頁面具體方法
Nginx是一個web服務(wù)器也可以用來做負載均衡及反向代理使用,目前使用最多的就是負載均衡,本篇文章重點為大家講解一下Nginx設(shè)置404頁面具體方法。

成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)利川,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
第一種:Nginx自己的錯誤頁面
Nginx訪問一個靜態(tài)的html 頁面,當(dāng)這個頁面沒有的時候,Nginx拋出404,那么如何返回給客戶端404呢?
看下面的配置,這種情況下不需要修改任何參數(shù),就能實現(xiàn)這個功能。
server {
listen 80;
server_name www.test.com;
root /var/www/test;
index index.html index.htm;
location / {
}
# 定義錯誤頁面碼,如果出現(xiàn)相應(yīng)的錯誤頁面碼,轉(zhuǎn)發(fā)到那里。
error_page 404 403 500 502 503 504 /404.html;
# 承接上面的location。
location = /404.html {
# 放錯誤頁面的目錄路徑。
root /usr/share/nginx/html;
}
}
第二種:反向代理的錯誤頁面
如果后臺Tomcat處理報錯拋出404,想把這個狀態(tài)叫Nginx反饋給客戶端或者重定向到某個連接,配置如下:
upstream www {
server 192.168.1.201:7777 weight=20 max_fails=2 fail_timeout=30s;
ip_hash;
}
server {
listen 80;
server_name www.test.com;
root /var/www/test;
index index.html index.htm;
location / {
if ($request_uri ~* ‘^/$’) {
rewrite .* http://www.test.com/index.html redirect;
}
# 關(guān)鍵參數(shù):這個變量開啟后,我們才能自定義錯誤頁面,當(dāng)后端返回404,nginx攔截錯誤定義錯誤頁面
proxy_intercept_errors on;
proxy_pass http://www;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-FOR $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
}
第三種:Nginx解析php代碼的錯誤頁面
如果后端是php解析的,需要加一個變量
在http段中加一個變量 fastcgi_intercept_errors on 就可以了。
指定一個錯誤頁面:
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
指定一個url地址:
error_page 404 /404.html;
error_page 404 = http://www.test.com/error.html;
標(biāo)題名稱:Nginx設(shè)置404頁面具體方法
網(wǎng)頁鏈接:http://m.fisionsoft.com.cn/article/coiijos.html


咨詢
建站咨詢
