http - HTTP请求返回502状态码时Nginx返回“502 Bad GateWay”+requestUrl
问题描述
我想实现一个功能。
HTTP请求返回502状态码时Nginx返回“502 Bad GateWay”+requestUrl
如何配置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";
}
解决方案
您需要该proxy_intercept_errors
指令。
该指令的默认值为off
. on
如果您想拦截来自代理服务器的状态码大于/等于 300(当然,包括 502)的响应,则必须打开它。有关此指令的更多详细信息。
这是我测试过的示例配置文件。
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
测试一下。
[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
我在配置中添加了一些解释。希望它会有所帮助。
推荐阅读
- html - 通过 HTML 音频标签播放音频
- logstash - 如何使用 logstash 管道配置将 Okta 系统日志记录转换为 Elastic Common Schema 版本 1.5
- c# - 在循环中读取 dbc 文件和字符串块
- c++ - memcpy 一个缓冲区到另一个具有 2 种不同大小的缓冲区
- c++ - 如何在 C++ 中更快地读取文件
- java - PropertySource 属性文件被扫描 env 属性文件忽略
- highcharts - 箱线图中的平均标记与多个系列
- html - 使用 flexbox 内联两个元素并将一个元素放在其他元素的下方
- google-analytics - 在 Power BI 中加载 Google Analytics 数据时如何解决数据源错误?
- python - 具有多个过滤器的 Django 搜索表单