欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nginx設(shè)置404錯誤頁面跳轉(zhuǎn)的幾種方法總結(jié)

 更新時間:2024年03月18日 10:40:10   作者:MASTERYEE  
一個網(wǎng)站項目,肯定是避免不了404頁面的,通常使用Nginx作為Web服務(wù)器時,有些相關(guān)配置方法,下面小編給大家?guī)砹薔ginx實現(xiàn)404頁面的幾種方法,感興趣的朋友一起看看吧

一、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進行的代理,
訪問項目不存在的頁面時,出現(xiàn)的是Nginx默認的404頁面,現(xiàn)在我配置我自己寫的404頁面進行提示

注意:網(wǎng)上大多數(shù)博客寫的都只有一種情況,要么就是使用 proxy_intercept_errors on;, 要么就是使用fastcgi_intercept_errors on; 沒有說明這兩種的區(qū)別, 還有也沒有說明404.html文件應(yīng)該放在服務(wù)器的什么位置,我在此處優(yōu)先進行說明, 如果你本地有部署項目,優(yōu)先使用proxy_intercept_errors on;這個配置進行嘗試, 如果沒有部署項目,則使用fastcgi_intercept_errors on; 這個進行嘗試,也可以兩個全加上, 其次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;
        }
        
        # 這種方式和上面的方式均可起作用,只需要選擇一種即可,本文中沒有進行進一步注釋
        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未啟動時)

當我把我的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; 時配置的錯誤頁面表示的是當服務(wù)器返回的狀態(tài)碼為我們配置的狀態(tài)碼時,我們才進行的頁面跳轉(zhuǎn)。如:服務(wù)器中沒有xxxx.do接口時,我們訪問了這個接口,配置了
proxy_intercept_errors on;則也會進行頁面跳轉(zhuǎn)

如果服務(wù)器中沒有開啟服務(wù),則配置proxy_intercept_errors on; 無用,則需要再添加fastcgi_intercept_errors on; 配置, 這樣的話,出現(xiàn)頁面錯誤時也會進行跳轉(zhuǎn)

以上就是Nginx設(shè)置404錯誤頁面跳轉(zhuǎn)的幾種方法總結(jié)的詳細內(nèi)容,更多關(guān)于Nginx設(shè)置404錯誤頁面的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Nginx為已安裝nginx動態(tài)添加模塊

    Nginx為已安裝nginx動態(tài)添加模塊

    本篇文章主要介紹了Nginx之為已安裝nginx動態(tài)添加模塊的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • nginx 目錄密碼保護的設(shè)置方法

    nginx 目錄密碼保護的設(shè)置方法

    比如要對 網(wǎng)站目錄下的 test 文件夾 進行加密認證
    2010-12-12
  • LNMP簡介(最新推薦)

    LNMP簡介(最新推薦)

    LNMP是指一組通常一起使用來運行動態(tài)網(wǎng)站或者服務(wù)器的自由軟件名稱首字母縮寫,L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python
    2023-08-08
  • Nginx反向代理后臺報404遇到的解決方法

    Nginx反向代理后臺報404遇到的解決方法

    Nginx反向代理404錯誤通常是由服務(wù)器配置不正確、文件路徑不正確、文件權(quán)限不正確、文件名大小寫不正確等,本文主要介紹了Nginx反向代理后臺報404遇到的解決方法,感興趣的可以了解一下
    2023-11-11
  • Nginx的完整配置詳解及實例代碼

    Nginx的完整配置詳解及實例代碼

    這篇文章主要介紹了Nginx的完整配置詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • nginx中配置pathinfo模式示例

    nginx中配置pathinfo模式示例

    這篇文章主要介紹了nginx中配置pathinfo模式示例,本文詳細記錄了摸索過程和成功的配置案例,需要的朋友可以參考下
    2014-08-08
  • Nginx中的用戶認證配置及阻止用戶使用代理訪問的方法

    Nginx中的用戶認證配置及阻止用戶使用代理訪問的方法

    這篇文章主要介紹了Nginx中的用戶認證配置及阻止用戶使用代理訪問的方法,用戶認證部分用到了自帶的ngx_http_auth_basic_module模塊,需要的朋友可以參考下
    2016-01-01
  • Nginx?Gunicorn?flask項目部署思路分析詳解

    Nginx?Gunicorn?flask項目部署思路分析詳解

    這篇文章主要為大家介紹了Nginx?Gunicorn?flask項目部署思路分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Nginx安裝與使用教程詳解

    Nginx安裝與使用教程詳解

    這篇文章主要介紹了Nginx安裝與使用教程詳解的相關(guān)資料
    2016-09-09
  • Nginx配置并兼容HTTP實現(xiàn)代碼解析

    Nginx配置并兼容HTTP實現(xiàn)代碼解析

    這篇文章主要介紹了Nginx配置并兼容HTTP實現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11

最新評論