【问题标题】:How to create custom error 502 nginx in core of nginx (not using redirect to error page)?如何在 nginx 核心中创建自定义错误 502 nginx(不使用重定向到错误页面)?
【发布时间】:2017-11-16 02:50:14
【问题描述】:

有时,当 httpd 服务关闭时,我会遇到错误 502 的问题。 但仅在 1 分钟后网站就恢复了。

我需要自定义 502 消息以要求用户等待 1 分钟然后刷新页面,或者嵌入 JavaScript 或元刷新标签以在 1 分钟后自动刷新页面。 页面的URL必须相同才能产生刷新效果

请注意,我知道自定义错误页面重定向,例如location = /502.html,但这种类型的自定义错误页面会将用户重定向到其他页面,如果他们刷新页面,他们将再次收到错误页面。

任何想法都会很有帮助。


编辑更新以获取更多详细信息 10/06/2012。

我的 nginx 配置:

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/*";
}

和虚拟主机配置:

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

     }

== 案例测试..

如果 Apache httpd 服务停止:#service httpd stop

然后在浏览器中打开此链接: http://www.xaluan.com/modules.php?name=News&file=article&sid=123456 您将在浏览器地址上看到具有相同 URL 的 502 错误。

== 自定义错误页面 我需要在 Apache 失败时提供帮助的配置,将 显示自定义消息 告诉用户等待 1 分钟返回服务,然后使用相同的 URL 刷新当前页面(刷新我可以通过 JavaScript 轻松完成) , Nginx 不会更改 URL,因此 JavaScript 可以正常工作。

【问题讨论】:

    标签: nginx


    【解决方案1】:

    我找到了适合我的答案。 在 vhost 配置文件中,我将 放在 server 块的末尾,在右大括号之前:

     error_page 502 /502.html;
     location = /502.html {
    
          root  /home/xaluano/public_html;
    
      }
    

    当然,我还需要在我的域根目录创建一个文件 502.html,使用元标记刷新和 java-script 自动刷新。

    html页面内容为:

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

    【讨论】:

    • 请注意,我还必须将 try_files $uri /502.html; 添加到 location = /502.html 块(使用 nginx 版本 1.2.1)
    【解决方案2】:

    http://nginx.org/r/error_page

    注意error_page 502 /502.html; 执行内部重定向。它不会改变浏览器地址栏中的 URL。

    【讨论】:

    • 你的意思是流代码不会改变浏览器的url吗?:error_page 502 /502.html;位置 = /502.html { 根 /home/xaluano/public_html; }
    • 我做了一个测试..它仍然改变了浏览器的url;我也用内部测试;但仍然是相同的结果。它把我送到 domain.com/502.html 的根目录
    • 你能展示你的完整配置吗?只是 server { error_page 502 /502.html; location = /502.html { root /home/xaluano/public_html; } } 不会那样做。
    • 在 nginx.conf 我有:包括“/etc/nginx/vhosts/*”;然后在该文件夹中有文件 xaluan.com 内容: server {error_page 405 = @backend; add_header X-Cache "来自后端的命中"; #proxy_set_header 服务器“缓存代理”; #add_header X-Cache-Vinahost "来自后端的命中"; proxy_pass 123.30.137.66:8081;包括proxy.inc; } 位置@backend { 内部; proxy_pass 123.30.137.66:8081;包括proxy.inc; } 位置 ~ .*\.(php|jsp|cgi|pl|py)?$ { #proxy_cache wwwcache; #proxy_cache_valid 200 15m;}
    • 内容与以下下载链接相同:xaluan.com/nginxhost.txt(抱歉不能在这里发长:()
    猜你喜欢
    • 1970-01-01
    • 2013-02-27
    • 2020-11-27
    • 2015-08-12
    • 2011-12-09
    • 2014-04-24
    • 2016-04-16
    • 1970-01-01
    • 2011-03-08
    相关资源
    最近更新 更多