30

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.

jamieb
  • 9,847
  • 14
  • 48
  • 63
Binh Nguyen
  • 1,313
  • 3
  • 17
  • 27

2 Answers2

52

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>
crockeea
  • 21,651
  • 10
  • 48
  • 101
Binh Nguyen
  • 1,313
  • 3
  • 17
  • 27
  • 1
    Note that I also had to add `try_files $uri /502.html;` to the `location = /502.html` block (using nginx version 1.2.1) – Ryan Buckley May 25 '15 at 00:12
8

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.

VBart
  • 14,714
  • 4
  • 45
  • 49
  • are you meaning flowing code will not chage url of browser?: error_page 502 /502.html; location = /502.html { root /home/xaluano/public_html; } – Binh Nguyen Jun 06 '12 at 02:44
  • 1
    I did a test .. and it still change the url of browser; i also test with internal; but still the same result. it send me me to root of domain.com/502.html – Binh Nguyen Jun 07 '12 at 02:06
  • Could you show your full config? Just `server { error_page 502 /502.html; location = /502.html { root /home/xaluano/public_html; } }` doesn't do that. – VBart Jun 07 '12 at 10:23
  • in the nginx.conf i have: include "/etc/nginx/vhosts/*"; then in the that folder has file xaluan.com with content: server {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;} – Binh Nguyen Jun 07 '12 at 10:45
  • content same as in following download link: http://www.xaluan.com/nginxhost.txt ( sorry cant post long here :( ) – Binh Nguyen Jun 07 '12 at 11:37
  • nginxhost.txt looks completely invalid, it doesn't even pass config test. Probably, your nginx just ignore it on reload. And note, that if you pass this request to backend (like you do in location @backend) then your backend actually responsable for redirects. – VBart Jun 07 '12 at 14:46
  • sorry may be that config invalid cause i did coppy from ssh, it may missing some words. i did restart, reload nginx config and it work ok. could you gime sample about backend or confirm the following code is will not change or redirect original url to the domain.com/502.html : location / ( error_page 502 @fallback; ) location @fallback ( proxy_pass http://www.xaluan.com/502.html; ) – Binh Nguyen Jun 08 '12 at 00:50
  • How can `proxy_pass xaluan.com/502.html;` return 502 error page if xaluan.com doesn't anwser, which is already caused 502 in nginx? With your code it doesn't start with the error message: `nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /home/vbart/Development/Nginx/tests/build/../test.conf:15` – VBart Jun 08 '12 at 06:58
  • I tested with this config http://pastebin.com/K2fTYeAj and found that your backend return redirect: "Location: http://www.xaluan.com/502.html", it's not nginx do, it's your xaluan.com. – VBart Jun 08 '12 at 07:05
  • Ok, with this config: http://pastebin.com/1gSXRKfF it doesn't redirect. `xaluan.com -> www.xaluan.com` Do you configure redirect from www. to non www. domain? So... – VBart Jun 08 '12 at 07:09
  • thanks for helping me :) I have put your config on my vhost nginx but it allway create errors, may be i'm did wrong.. so I had create basbin files for more detail of both nginx and vhost config .. could you have look and edit as you wish.. thanks very much – Binh Nguyen Jun 10 '12 at 01:58
  • thanks very much .. I just managed the issue by your fist advice .. I did wrong test cause I put the code in side location / {} so that will failed all the time.. my apologies for taking your time. thank a ton. – Binh Nguyen Jun 10 '12 at 02:42