Nginx設(shè)置404錯誤頁面跳轉(zhuǎn)的幾種方法總結(jié)
一、Nginx在Linux上設(shè)置404錯誤頁面
- Linux版本:Centos 7.4
- Nginx版本:nginx-1.14.0.tar.gz
- nginx安裝目錄參考: /usr/local/nginx則是我的安裝目錄
說明:我Linux服務(wù)器上已經(jīng)在tomcat上部署了一個項目,使用Nginx進(jìn)行的代理,
訪問項目不存在的頁面時,出現(xiàn)的是Nginx默認(rèn)的404頁面,現(xiàn)在我配置我自己寫的404頁面進(jìn)行提示
注意:網(wǎng)上大多數(shù)博客寫的都只有一種情況,要么就是使用 proxy_intercept_errors on;, 要么就是使用fastcgi_intercept_errors on; 沒有說明這兩種的區(qū)別, 還有也沒有說明404.html文件應(yīng)該放在服務(wù)器的什么位置,我在此處優(yōu)先進(jìn)行說明, 如果你本地有部署項目,優(yōu)先使用proxy_intercept_errors on;這個配置進(jìn)行嘗試, 如果沒有部署項目,則使用fastcgi_intercept_errors on; 這個進(jìn)行嘗試,也可以兩個全加上, 其次404.html文件放在nginx安裝目錄的html文件夾下
1.1 第一種配置情況(跳轉(zhuǎn)網(wǎng)絡(luò)地址)
error_page配置的是http這種的網(wǎng)絡(luò)地址
在http下配置 proxy_intercept_errors on;
http {
include mime.types;
default_type application/octet-stream;
proxy_intercept_errors on;
... 以下省略
在server下配置 error_page
以下三種情況都可以起作用, 可以配置在server第一層的任何位置, 不受影響
也可以配置在location里面,我下面代碼注釋的地方都是可以配置的
server {
listen 80;
server_name www.xxxxxxx.com;
#error_page 404 http://www.baidu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://search-masteryee;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 20m;
#error_page 404 http://www.baidu.com;
}
location /upload {
root /usr/;
index index.html index.htm;
}
error_page 404 http://www.baidu.com;
}
1.2 第二種配置情況(跳轉(zhuǎn)本地地址)
error_page配置的是本地服務(wù)器的頁面地址,
- 說明:我的404.html頁面文件放在nginx安裝目錄下的html文件夾內(nèi)
- 如果編寫的404.html頁面中有圖片等外部文件,使用相對地址是不行的
在http下配置 proxy_intercept_errors on;
http {
include mime.types;
default_type application/octet-stream;
proxy_intercept_errors on;
... 以下省略
在server中配置error_page
說明:我的nginx安裝在/usr/local/下
server {
listen 80;
server_name www.xxxxxxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://search-masteryee;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 20m;
}
location /upload {
root /usr/;
index index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
#使用絕對地址, 跳轉(zhuǎn)服務(wù)器/usr/local/nginx/html/404.html
root /usr/local/nginx/html;
}
# 這種方式和上面的方式均可起作用,只需要選擇一種即可,本文中沒有進(jìn)行進(jìn)一步注釋
error_page 404 /404.html;
location = /404.html {
# 使用相對地址, 跳轉(zhuǎn)nginx安裝目錄下的html/404.html
root html;
# 下面這種多了一個/ 反而不起作用
#root /html;
}
# 以下這幾種網(wǎng)上比較多的方式,均試過,無法跳轉(zhuǎn)正確頁面或不起跳轉(zhuǎn)作用
#error_page 404 404.html;
#error_page 404 /404.html;
#error_page 404 html/404.html;
#error_page 404 /html/404.html;
#error_page 404 /usr/local/nginx/html/404.html;
#error_page 404 usr/local/nginx/html/404.html;
}
可以配置多種返回碼的多個錯誤頁面,也可以同時配置多個錯誤碼跳轉(zhuǎn)一個頁面,可以同時存在 如下所示
server {
listen 80;
server_name www.xxxxxxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://search-masteryee;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 20m;
}
location /upload {
root /usr/;
index index.html index.htm;
}
#error_page 404 /404.html;
# 錯誤頁面的種類也可以是多個
# 這里的錯誤碼可以是多個
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 這里是錯誤嗎也可以是一個
error_page 404 /404.html;
location = /404.html {
root html;
}
}
1.3 第三種情況(tomcat未啟動時)
當(dāng)我把我的tomcat服務(wù)器關(guān)掉時,我服務(wù)器就沒有運行項目了,這時在訪問頁面,則上述配置沒有產(chǎn)生效果,此時則需要添加一個配置
fastcgi_intercept_errors on;
在http下配置 fastcgi_intercept_errors on;
http {
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errors on;
proxy_intercept_errors on;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
#
server中配置如下,將500 502 503 504等狀態(tài)碼一致性跳轉(zhuǎn)404頁面
server {
listen 80;
server_name www.xxxxxxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://search-masteryee;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 20m;
}
location /upload {
root /usr/;
index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /404.html;
error_page 404 /404.html;
location = /404.html {
root html;
}
}
1.4 第四種情況(proxy_intercept_errors的配置地址可多樣)
proxy_intercept_errors on;這個配置不一定需要放在http下面,也可以是server下,也可以是server的location下
server {
listen 80;
server_name www.masteryee.com;
proxy_intercept_errors on;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://search-masteryee;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 20m;
#可以是這里
#proxy_intercept_errors on;
}
location /upload {
root /usr/;
index index.html index.htm;
}
error_page 500 502 503 504 /404.html;
error_page 404 /404.html;
location = /404.html {
root html;
}
}
1.5 proxy_intercept_errors和fastcgi_intercept_errors的理解
配置proxy_intercept_errors on; 時配置的錯誤頁面表示的是當(dāng)服務(wù)器返回的狀態(tài)碼為我們配置的狀態(tài)碼時,我們才進(jìn)行的頁面跳轉(zhuǎn)。如:服務(wù)器中沒有xxxx.do接口時,我們訪問了這個接口,配置了
proxy_intercept_errors on;則也會進(jìn)行頁面跳轉(zhuǎn)
如果服務(wù)器中沒有開啟服務(wù),則配置proxy_intercept_errors on; 無用,則需要再添加fastcgi_intercept_errors on; 配置, 這樣的話,出現(xiàn)頁面錯誤時也會進(jìn)行跳轉(zhuǎn)
以上就是Nginx設(shè)置404錯誤頁面跳轉(zhuǎn)的幾種方法總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Nginx設(shè)置404錯誤頁面的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Nginx中的用戶認(rèn)證配置及阻止用戶使用代理訪問的方法
這篇文章主要介紹了Nginx中的用戶認(rèn)證配置及阻止用戶使用代理訪問的方法,用戶認(rèn)證部分用到了自帶的ngx_http_auth_basic_module模塊,需要的朋友可以參考下2016-01-01
Nginx?Gunicorn?flask項目部署思路分析詳解
這篇文章主要為大家介紹了Nginx?Gunicorn?flask項目部署思路分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

