Binh Nguyen
Binh Nguyen

Reputation: 1313

How to create custom error 502 nginx in core of nginx (not using redirect to error page)?

Sometimes I get an issue with error 502 when httpd service is down. But only in 1 minute the website come back.

I need to custom the 502 message to ask user to wait for 1 minute then refresh page, or embed JavaScript or meta refresh tag to auto refresh page after 1 minute. Page's URL must be the same to make refresh effect

Notice that I know about custom error page redirect eg location = /502.html, but that type of custom error page will redirect user to other page, if they will refresh page they will got error page again.

Any idea will be very helpful.


EDIT UPDATE for more detail 10/06/2012.

My nginx config:

user  nobody;
# no need for more workers in the proxy mode
worker_processes  24;
error_log  /var/log/nginx/error.log crit;
#worker_rlimit_nofile 20480;
events {
 worker_connections 109024; # increase for busier servers
 use epoll; # you should use epoll here for Linux kernels 2.6.x
}

http {
 server_name_in_redirect off;
 server_names_hash_max_size 2048;
 server_names_hash_bucket_size 256;
 include    mime.types;
 default_type  application/octet-stream;
 server_tokens off;
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout  20;

 ignore_invalid_headers on;
 client_header_timeout  50m;
 client_body_timeout 50m;
 send_timeout     20m;
 reset_timedout_connection on;
 connection_pool_size  2048;
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
 client_max_body_size 20M; 
 client_body_buffer_size 300k;
 request_pool_size  32k;
 output_buffers  14 32k;
 postpone_output  1460;
 proxy_temp_path  /tmp/nginx_proxy/;
 proxy_cache_path /dev/shm/nginx levels=1:2 keys_zone=wwwcache:45m inactive=5m max_size=1000m;
 client_body_in_file_only off;
 access_log off;
 open_log_file_cache off;
 #log_format bytes_log "$msec $bytes_sent .";
 include "/etc/nginx/vhosts/*";
}

and vhost config:

server {
 #         error_log /var/log/nginx/vhost-error_log warn;
          listen 123.30.137.66:80;
          server_name xaluan.net mtvvui.com www.daiduong.com.au www.xaluan.net xaluan.com www.xaluan.com www.daiduongrestaurant.net veryzoo.com www.mtvvui.com www.xaluan.org www.veryzoo.com daiduongrestaurant.net xaluan.org daiduong.com.au;
#          access_log /usr/local/apache/domlogs/xaluan.net combined;
          root /home/xaluano/public_html;
          location / {
      if ($http_cache_control ~ "max-age=0") {
                set $bypass 1;
          }

      location 
~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ 
{
      #root /home/xaluano/public_html;  
    #proxy_cache wwwcache;
      #proxy_cache_valid  200 15m;
      #proxy_cache_bypass $bypass;
          expires 1d;
          #try_files $uri @backend;
      proxy_pass http://123.30.137.66:8081;
          }
          error_page 405 = @backend;
          add_header X-Cache "HIT from Backend";
      #proxy_set_header Server "Caching-Proxy";
          #add_header X-Cache-Vinahost "HIT from Backend";
      proxy_pass http://123.30.137.66:8081;
          include proxy.inc;

          }


          location @backend {
          internal;
          proxy_pass http://123.30.137.66:8081;
          include proxy.inc;
          }
          location ~ .*\.(php|jsp|cgi|pl|py)?$ {
          #proxy_cache wwwcache;
      #proxy_cache_valid  200 15m;

      proxy_pass http://123.30.137.66:8081;
          include proxy.inc;
          }
          location ~ /\.ht {
          deny all;
          }

     }

== the case test..

If Apache httpd service stops: #service httpd stop

Then open in browser this link: http://www.xaluan.com/modules.php?name=News&file=article&sid=123456 You will see the 502 error with the same URL on browser address.

== Custom error page I need the config which will help when Apache fail, will show the custom message telling user to wait for 1 minute for service back, then refresh current page with same URL (refresh I can do easy by JavaScript), Nginx does not change URL so JavaScript can work out.

Upvotes: 30

Views: 51681

Answers (2)

Binh Nguyen
Binh Nguyen

Reputation: 1313

I found an answer that works for me. In the vhost config file, I put right at the end of the server block, before closing brace:

 error_page 502 /502.html;
 location = /502.html {

      root  /home/xaluano/public_html;

  }

Of course I also need to create a file 502.html at my domain root, with the meta-tag refresh, and java-script auto refresh.

The content of html page is:

<head>
<meta http-equiv="refresh" content="40" /> 
</head>
<body>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
var TimerVal = 40;
var TimerSPan = document.getElementById("CDTimer");
function CountDown(){
   setTimeout( "CountDown()", 1000 );
   TimerSPan.innerHTML=TimerVal;
   TimerVal=TimerVal-1;
   if (TimerVal<0) { TimerVal=0;
   location.reload(true); 
//   window.location.href = "http://www.xaluan.com";
   } //improvement by vivalibre, tq 
}
CountDown();
/*]]>*/ </script> 
</body>

Upvotes: 52

VBart
VBart

Reputation: 15110

http://nginx.org/r/error_page

Note that error_page 502 /502.html; performs internal redirect. It does not change the URL in browser address bar.

Upvotes: 8

Related Questions