2017-08-03 28 views
0

我使用AWS ec2 ami和nginx和node.js express如何更改nginx錯誤頁面?

如何更改nginx err頁面html或ejs?

要快的錯誤處理...

我的nginx服務器塊

server { 
     listen  80 default_server; 
     listen  [::]:80 default_server; 
     server_name localhost; 
     root   /usr/share/nginx/html; 

     # Load configuration files for the default server block. 
     include /etc/nginx/default.d/*.conf; 

     location/{ 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_set_header X-NginX-Proxy true; 
     proxy_pass http://127.0.0.1:3000; 
     proxy_redirect off; 
     } 

error_page 404 http://example.com/error; 
error_page 500 502 503 504 http://example.com/error; 

回答

0

爲了有一個自定義錯誤頁爲404和50倍的錯誤:

首先保存在您的自定義錯誤頁/ usr/share/nginx/html目錄,這是Nginx的默認文檔根目錄。 爲名爲customerror_404.html的404錯誤創建一個頁面,對於50x錯誤,創建一個名爲customerror_50x.html的頁面。

回聲「這不是你要找的頁面!!錯誤404」|須藤發球/usr/share/nginx/html/customerror_404.html

同樣,對於customerror_50x.html文件。

Nginx的默認值必須意識到使用這些自定義文件 如此開放nginx的默認,

VI的/ etc/nginx的/啓用的站點 - /默認

server { 
    listen 80 default_server; 
    listen [::]:80 default_server ; 
    server_name localhost; 
    # Load configuration files for the default server block. 
    include /etc/nginx/default.d/*.conf; 
    . . . 

    error_page 404 /customerror_404.html; 
    proxy_intercept_errors on; 
    location = /customerror_404.html { 
      root /usr/share/nginx/html; 
      internal; 
    } 
    error_page 500 502 503 504 /customerror_50x.html; 
    location = /customerror_50x.html { 
      root /usr/share/nginx/html; 
      internal; 
    } 

} 

保存文件並重新啓動nginx 測試更改: http://server_hostname_or_IP/anynonexistingfilename

你很好走。

+0

ok thx!我嘗試/etc/nginx/nginx.conf追加此代碼不起作用,但error_page追加proxy_intercept_errors;此代碼正在工作! 爲什麼它有效? @Shashi Bhushan – young

+0

proxy_intercept_errors on |關閉; 確定代碼大於或等於300的代理響應應傳遞給客戶端,還是將其攔截並重定向到nginx以便使用error_page指令進行處理。 Ref:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors –

+0

很好,你的問題解決了。 @young 而這個指令定義了nginx攔截HTTP響應。 默認情況下,所有響應將從代理服務器按原樣發送。 通過設置這個「on」,nginx將攔截由error_page指令顯式處理的狀態碼。所有其他具有未定義狀態代碼的響應將按原樣從代理服務器發送。 –