解决SELinux阻止Nginx访问服务
作者:
来源:网络
点击数: 次
发布时间:2021年09月19日
在使用 yum 安装 nginx 后可能会出现配置完成后却无法访问的问题,查看 audit.log 会发现类似于以下的错误信息
出现此问题的原因是 SELinux 基于最小权限原则默认拦截了 Nginx 的请求,SELinux 是 Linux 的安全子系统,提供更安全的访问控制,许多运维人员嫌麻烦可能会直接关闭此组件,但是治标不治本,本文演示在启用 SELinux 基础上完成对 Nginx 请求的放行。
首先我们需要确认 SELinux 的运行状态,当然出现此问题肯定是运行中。
[root@centos-server-02 local]# sestatus SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Max kernel policy version: 31
临时关闭 SELinux:setenforce 0
临时启动 SELinux:setenforce 1
永久关闭/启动:修改/etc/sysconfig/selinux后重启系统
2.开启 HTTP 访问。
[root@centos-server-01 audit]# setsebool -P httpd_can_network_connect 1
3.分析现有日志并生成关联模块,执行完此命令可以看到在当前目录下会生成后缀为*.pp和*.te文件,如果该服务器上的服务未被访问过,此命令执行无效。
[root@centos-server-01 audit]# ausearch -c 'nginx' --raw | audit2allow -M my-nginx
4.加载前一步生成的模块内容
[root@centos-server-01 audit]# semodule -i my-nginx.pp
5.执行完成以上命令后即可对 Nginx 进行正常访问。