Zero's Blog - nginx https://l2dy.sourceforge.io/tag/nginx/ Nginx proxy_pass 502 Bad Gateway https://l2dy.sourceforge.io/2022/08/20/nginx-selinux-connect.html 2022-08-20T03:22:00+00:00 Rocky Linux 8 上安装 nginx 后对 nginx.conf 进行修改增加了一个 proxy_pass 配置,但用浏览器访问时提示 502 Bad Gateway,也就是连接不上后端服务。照例先怀疑一下 SELinux。# getsebool httpd_can_network_connect httpd_can_network_connect --> off检查 SELinux 配置发现没有给 HTTP 服务主动发起 TCP 连接的权限,用 setsebool 调整后就正常了。setsebool -P httpd_can_network_connect on Nginx 访问本地文件 403 https://l2dy.sourceforge.io/2022/06/03/nginx-selinux.html 2022-06-03T09:23:00+00:00 Rocky Linux 8 上安装 nginx 后对 nginx.conf 进行修改增加了一个 /var/www 路径的 root 配置,但用 curl 测试访问一个文件时却返回了 403 Forbidden。2022/06/03 <masked> [error] <pid>#0: *1 open() "/var/www/.well-known/test" failed (13: Permission denied), client: <masked>, server: _, request: "GET /.well-known/test HTTP/1.1", host: "localhost"用 namei 命令查看一下权限,没有发现问题。# namei -om /var/www/.well-known/test f: /var/www/.well-known/test dr-xr-xr-x root root / drwxr-xr-x root root var drwxr-xr-x root root www drwxr-xr-x root root .well-known -rw-r--r-- root root test网上查阅资料发现可能是 SELinux 问题,故执行 setenforce 0 后再次测试,此时可以访问了。但关闭 SELinux 并不是最合理的方案,进一步查找资料发现 https://www.nginx.com/blog/using-nginx-plus-with-selinux/ 这篇文章,文中介绍了 SELinux 对文件上下文的永久变更是通过 semanage fcontext 控制的,让我们来看一下目前系统的配置。# semanage fcontext -l | grep httpd_sys_content_t <trimmed> /var/lib/trac(/.*)? all files system_u:object_r:httpd_sys_content_t:s0 /var/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0 /var/www/icons(/.*)? all files system_u:object_r:httpd_sys_content_t:s0 /var/www/svn/conf(/.*)? all files system_u:object_r:httpd_sys_content_t:s0 # ls -Z /var/www/.well-known/test unconfined_u:object_r:var_t:s0 /var/www/.well-known/test可以发现 /var/www 目录下任意路径已经在 httpd_sys_content_t 的 file\_spec 中了但没有应用到文件上,所以直接用 restorecon 命令恢复文件和文件夹默认的安全上下文就行。restorecon -Rv /var/www/.well-known