Zero's Blog - nginx https://l2dy.sourceforge.io/tag/nginx/ zh-CN Sat, 20 Aug 2022 03:22:00 +0000 Sat, 20 Aug 2022 03:22:00 +0000 Nginx proxy_pass 502 Bad Gateway https://l2dy.sourceforge.io/2022/08/20/nginx-selinux-connect.html https://l2dy.sourceforge.io/2022/08/20/nginx-selinux-connect.html Sat, 20 Aug 2022 03:22:00 +0000 Zero 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
]]>
0 https://l2dy.sourceforge.io/2022/08/20/nginx-selinux-connect.html#comments https://l2dy.sourceforge.io/feed/tag/nginx/
Nginx 访问本地文件 403 https://l2dy.sourceforge.io/2022/06/03/nginx-selinux.html https://l2dy.sourceforge.io/2022/06/03/nginx-selinux.html Fri, 03 Jun 2022 09:23:00 +0000 Zero 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
]]>
0 https://l2dy.sourceforge.io/2022/06/03/nginx-selinux.html#comments https://l2dy.sourceforge.io/feed/tag/nginx/