简体   繁体   English

当HTTP请求返回502状态代码时,Nginx返回“ 502 Bad GateWay” + requestUrl

[英]Nginx returns “502 Bad GateWay” + requestUrl when HTTP request returns 502 status code

I want to implement a function. 我想实现一个功能。

Nginx returns "502 Bad GateWay" + requestUrl when HTTP request returns 502 status code 当HTTP请求返回502状态代码时,Nginx返回“ 502 Bad GateWay” + requestUrl

How to configure nginx to achieve this function, thank you. 怎样配置nginx来实现这个功能,谢谢。

#/usr/local/nginx/lua/auth/404.lua
ngx.say("502 Bad GateWay ")
local request_method = ngx.var.request_method
ngx.say(request_method)
local request_uri = ngx.var.request_uri
ngx.say(request_uri)
#nginx.conf
 proxy_intercept_errors on ;
 error_page 502 /502.html;
 location =/502.html {
      content_by_lua_file "/usr/local/nginx/lua/auth/404.lua";
 }

You need the proxy_intercept_errors directive. 您需要proxy_intercept_errors指令。
The default value of this directive is off . 此伪指令的默认值为off You must turn it on if you want to intercept response from proxied server with status code bigger/equal than 300(of course, 502 included). 如果要拦截状态代码大于/等于300(当然包括502)的代理服务器的响应on则必须将其on More details about this directive . 有关此指令的更多详细信息

Here is an example configuration file which I've tested. 这是我测试过的示例配置文件。

upstream tomcat502 {
  server 10.10.100.131:28889; # There is no such a backend server, so it would return 502
}

server {
  listen       10019; # it's up to you
  server_name  10.10.100.133;

  location /intercept502 {
    proxy_intercept_errors on;  # the most important directive, make it on;
    proxy_pass  http://tomcat502/;
    error_page  502 = @502; # redefine 502 error page
  }

  location @502 {
    return 502 $request_uri\n;  # you could return anything you want.
  }
}

After reloading nginx, use curl to test it. 重新加载nginx之后,使用curl进行测试。

[root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502
/intercept502 
[root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502 -I
HTTP/1.1 502 Bad Gateway
Server: nginx/1.12.1
Date: Wed, 09 Jan 2019 13:48:05 GMT
Content-Type: application/octet-stream
Content-Length: 14
Connection: keep-alive

I have added some explanation up in configuration. 我在配置中添加了一些解释。 Hope it would help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 CodeIgniter 时出现错误 502 Bad Gateway - Error 502 Bad Gateway When Using CodeIgniter HTTP 代理应该在什么时候使用状态码 502? - When should an HTTP proxy use status code 502? 如何在大标头上修复Nginx 502 Bad Gateway? - How do I fix Nginx 502 Bad Gateway on large headers? Azure /映像请求502(错误网关)错误 - 间歇性 - Azure / Image request 502 (Bad gateway) error - intermittent HTTP请求返回状态码0时的含义是什么? - What does it mean when an HTTP request returns status code 0? JMeter(5.5) API 测试无法发出正常的HTTP请求,收到502状态码 - JMeter(5.5) API testing fails to make a normal HTTP request, receives 502 status code 如果代理根本没有收到响应,是否应该使用 502 HTTP 状态代码? - Should a 502 HTTP status code be used if a proxy receives no response at all? HTTP标头获取没有内容长度的响应会给502错误的网关 - HTTP header Get response with no content-length gives 502 bad gateway 为什么我从亚马逊弹性负载均衡器后面发送重定向时会得到502坏网关? - Why am I getting a 502 bad gateway when sending a redirect from behind amazon elastic load balancer? 当我在同一执行中多次使用HtmlUnit请求时获得502 Bad Gateway - Getting 502 Bad Gateway when I use HtmlUnit requests multiple times in the same execution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM