nginx反向代理502-Bad Gateway问题解决

发布时间 2023-10-13 13:04:32作者: bret_chen

nginx反向代理502-Bad Gateway问题解决

参考自:https://blog.csdn.net/josephhao1989/article/details/103087226

故障现象:

直接访问目标地址正常,通过Nginx代理的地址访问报错502-Bad Gateway。

原因分析:

查看错误日志:error.log,以centos7为例,默认路径为:/var/log/nginx/error.log:

::1 - - [27/Nov/2018:14:15:51 +0800] “GET /test HTTP/1.1” 502 3693 “-” “curl/7.29.0” “-”

没有相关错误,说明nginx反向代理配置没有出错,那就可能是httpd服务发生异常,但直接访问服务正常,就只能排查防火墙等设置。

查看SELinux日志/var/log/audit/audit.log
发现nginx转发8080端口被拒绝

SELinux缺省会通过Linux审计系统auditd将日志写在/var/log/audit/audit.log内,而该服务缺省为启用的;假若auditd长驻程序并未运行,信息将会被写进/var/log/messages

type=AVC msg=audit(1543286075.634:78781): avc: denied { name_connect } for pid=3232 comm=“nginx” dest=8080 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_cache_port_t:s0 tclass=tcp_socket

解决方法

使用以下指令查看selinux配置:

#cmd: getsebool httpd_can_network_connect

#httpd_can_network_connect --> off

SELinux配置将httpd网络连接关闭,所以很自然将其启用即可:

cmd : setsebool -P httpd_can_network_connect 1

再次访问,即可正常访问,当然,直接关闭SELinux也可以访问,但是不建议这样解决问题。