1. 程式人生 > >nginx禁止直接通過ip進行訪問並跳轉到自定義500頁面

nginx禁止直接通過ip進行訪問並跳轉到自定義500頁面

直接上配置檔案

  server {                                                                                                         
        listen 80 default;          # 要禁止直接訪問ip,需要加上default                                                                          
        server_name ~.*;                                                                                                                                                                                   
        error_page 404 400 402 401 /404.html;     # 定義錯誤狀態碼對應的html檔案                                                                 
        location = /404.html {                                                                                       
        # 如果使用相對路徑一般將檔案放在nginx安裝目錄下的html/下,比如:/usr/local/nginx/html/404.html
             root html;                                                              
        }                                                                                                            
        error_page 500 /500.html;                                                                                    
        location = /500.html {                                                                                       
             root html;                                                                                          
        }                                                                                                                                                                                          
        error_page 502 /502.html;                                                                                    
        location = /502.html {                                                                                       
             root html;                                                                                         
        }                                                                                                   
        location / {          # 這裡必須這樣寫訪問ip才能跳轉到自定義的500.html                                                                                       
            return 500;                                                                                              
        }
        #return 500;          # 如果這樣寫,會跳轉到nginx預設的500頁面                                                                                                             
} 

這是寫 return 500;的結果
這裡寫圖片描述

這是 location的
這裡寫圖片描述