最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、Nginx在Linux上設置404錯誤頁面

  • Linux版本:Centos 7.4
  • Nginx版本:nginx-1.14.0.tar.gz
  • nginx安裝目錄參考: /usr/local/nginx則是我的安裝目錄

說明:我Linux服務器上已經(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ōu)先進行說明, 如果你本地有部署項目,優(yōu)先使用proxy_intercept_errors on;這個配置進行嘗試, 如果沒有部署項目,則使用fastcgi_intercept_errors on; 這個進行嘗試,也可以兩個全加上, 其次404.html文件放在nginx安裝目錄的html文件夾下

1.1 第一種配置情況(跳轉(zhuǎn)網(wǎng)絡地址)

error_page配置的是http這種的網(wǎng)絡地址

在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配置的是本地服務器的頁面地址,

  • 說明:我的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)服務器/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服務器關掉時,我服務器就沒有運行項目了,這時在訪問頁面,則上述配置沒有產(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; 時配置的錯誤頁面表示的是當服務器返回的狀態(tài)碼為我們配置的狀態(tài)碼時,我們才進行的頁面跳轉(zhuǎn)。如:服務器中沒有xxxx.do接口時,我們訪問了這個接口,配置了
proxy_intercept_errors on;則也會進行頁面跳轉(zhuǎn)

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

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

相關文章

  • nginx解決跨域問題的實例方法

    nginx解決跨域問題的實例方法

    在本篇文章里小編給各位分享了關于nginx怎么解決跨域問題的方法和實例代碼,需要的朋友們參考下。
    2019-07-07
  • nginx+php出現(xiàn)No input file specified解決辦法

    nginx+php出現(xiàn)No input file specified解決辦法

    這篇文章主要介紹了nginx+php出現(xiàn)No input file specified解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • nginx配置文件詳解中文版

    nginx配置文件詳解中文版

    這篇文章主要介紹了nginx配置文件nginx.conf的配置參數(shù)詳解,并增加了中文版注釋,需要的朋友可以參考下
    2014-03-03
  • HipChat上傳文件報未知錯誤的原因分析及解決方案

    HipChat上傳文件報未知錯誤的原因分析及解決方案

    HipChat的功能類似于Campfire、Sazneo等在線協(xié)同工具,并且和Yammer以及Salesforce的Chatter等企業(yè)社交平臺有一定相似之處。你可以為單個項目或者小組搭建自有的聊天室,也可以很方便的發(fā)起一對一聊天
    2016-01-01
  • 詳解Linux中Nginx反向代理下的tomcat集群

    詳解Linux中Nginx反向代理下的tomcat集群

    本篇文章主要介紹了詳解Linux中Nginx反向代理下的tomcat集群,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • nginx服務器access日志中大量400 bad request錯誤的解決方法

    nginx服務器access日志中大量400 bad request錯誤的解決方法

    這篇文章主要介紹了nginx服務器access日志中大量400 bad request錯誤的解決方法,本文結(jié)論是空主機頭導致的大量400錯誤日志,關閉默認主機的日志記錄就可以解決問題,需要的朋友可以參考下
    2015-01-01
  • Filebeat 采集 Nginx 日志的方法

    Filebeat 采集 Nginx 日志的方法

    這篇文章主要介紹了Filebeat 采集 Nginx 日志的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Nginx的搭建與核心配置方法

    Nginx的搭建與核心配置方法

    Nginx是一個高性能的反向代理和 Web服務器軟件,最初是由俄羅斯人 Igor Sysoev開發(fā)的,其源代碼基于雙條款BSD許可證發(fā)布,因其系統(tǒng)資源消耗低、運行穩(wěn)定且具有高性能的并發(fā)處理能力等特性,這篇文章主要介紹了Nginx的搭建與核心配置,需要的朋友可以參考下
    2024-06-06
  • 從零實現(xiàn)Nginx風格內(nèi)存池完整代碼

    從零實現(xiàn)Nginx風格內(nèi)存池完整代碼

    本文將從為什么需要內(nèi)存池講起,重點探討內(nèi)存池的設計哲學,并詳細拆解其實現(xiàn)方案,從而搭建起讀者對內(nèi)存池從理論到實踐的完整認知模型,感興趣的朋友跟隨小編一起看看吧
    2026-05-05
  • nginx中path模式配置示例

    nginx中path模式配置示例

    這篇文章主要介紹了nginx中path模式配置示例,nginx服務器默認是不支持pathinfo模式的,需要修改配置才可以實現(xiàn),本文即給出了配置示例,需要的朋友可以參考下
    2014-12-12

最新評論

永胜县| 盐边县| 台中县| 镇原县| 泗洪县| 湟源县| 行唐县| 宁蒗| 邯郸县| 富平县| 西和县| 华安县| 安国市| 永兴县| 偏关县| 封开县| 玉林市| 交城县| 丰城市| 秭归县| 治多县| 宜兴市| 祁阳县| 会同县| 泸水县| 鲁甸县| 元谋县| 东乡族自治县| 西充县| 克什克腾旗| 武功县| 越西县| 密云县| 大理市| 镇康县| 龙里县| 武隆县| 邛崃市| 绩溪县| 弥勒县| 鲁山县|