server {
listen 80;
server_name www.test.com;
root /var/www/test;
index index.html index.htm;
location / {
}
# 定義錯(cuò)誤頁面碼,如果出現(xiàn)相應(yīng)的錯(cuò)誤頁面碼,轉(zhuǎn)發(fā)到那里。
error_page 404 403 500 502 503 504 /404.html;
# 承接上面的location。
location = /404.html {
# 放錯(cuò)誤頁面的目錄路徑。
root /usr/share/nginx/html;
}
}
第二種:反向代理的錯(cuò)誤頁面
如果后臺(tái)Tomcat處理報(bào)錯(cuò)拋出404,想把這個(gè)狀態(tài)叫Nginx反饋給客戶端或者重定向到某個(gè)連接,配置如下:
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ù):這個(gè)變量開啟后,我們才能自定義錯(cuò)誤頁面,當(dāng)后端返回404,nginx攔截錯(cuò)誤定義錯(cuò)誤頁面
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代碼的錯(cuò)誤頁面
如果后端是php解析的,需要加一個(gè)變量
在http段中加一個(gè)變量 fastcgi_intercept_errors on 就可以了。
指定一個(gè)錯(cuò)誤頁面:
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
指定一個(gè)url地址:
error_page 404 /404.html;
error_page 404 = http://www.test.com/error.html;
更多關(guān)于云服務(wù)器,域名注冊(cè),虛擬主機(jī)的問題,請(qǐng)?jiān)L問西部數(shù)碼官網(wǎng):www.ps-sw.cn