首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTTP请求返回502状态码时,Nginx返回"502 Bad GateWay“+ requestUrl

HTTP请求返回502状态码时,Nginx返回"502 Bad GateWay“+ requestUrl
EN

Stack Overflow用户
提问于 2019-01-03 08:55:45
回答 1查看 2.2K关注 0票数 0

我想实现一个函数。

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

如何配置nginx来实现此功能,谢谢。

代码语言:javascript
复制
#/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";
 }
EN

回答 1

Stack Overflow用户

发布于 2019-01-09 21:48:53

您需要proxy_intercept_errors指令。

此指令的默认值为off。如果你想截获来自代理服务器的响应,并且状态码大于/等于300(当然,包括502 ),你必须将其设置为onMore details about this directive

这是一个我测试过的示例配置文件。

代码语言:javascript
复制
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.
  }
}

重新加载nginx后,使用curl进行测试。

代码语言:javascript
复制
[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

我已经在配置中添加了一些解释。希望能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54015065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档