2017-08-03 4 views
0

私はAWS ec2 amiとnginxとnode.jsを使用しています。nginxのエラーページを変更するには?

nginx errページhtmlまたはejsの変更方法はありますか? 404と50倍のエラーのカスタムエラーページを持つことが明示エラーハンドラのような

...

私の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

まずにおけるカスタムエラーページを保存/ usr/share/nginx/htmlディレクトリにあります。これはNginxのデフォルトドキュメントルートです。 customerror_404.htmlという404エラー用のページを作成し、50xエラー用のエラーコードcustomerror_50x.htmlを作成します。

echo「これはあなたが探しているページではありません!!エラー404」|同様customerror_50x.htmlファイルの

/usr/share/nginx/html/customerror_404.html sudoのティー。

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; 
    } 

} 

viのは/ etc/nginxの/サイト対応/デフォルトでは、これらのカスタムファイルに ので、オープンnginxのデフォルトを使用することを認識する必要があります

ファイルを保存し、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以上のコードを持つプロキシレスポンスをクライアントに渡すか、またはinterceptしてerror_pageディレクティブで処理するためにnginxにリダイレクトするかどうかを決定します。 Ref:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors –

+0

問題が解決したことは素晴らしいです。 @young このディレクティブは、HTTP応答のnginx傍受を定義します。 デフォルトでは、すべての応答は、プロキシされたサーバーからそのまま送信されます。 これを "on"に設定すると、nginxはerror_page指令で明示的に処理されるステータスコードを代行受信します。定義されていないステータスコードを持つ他のすべてのレスポンスは、プロキシされたサーバーからそのまま送信されます。 –

関連する問題