在使用 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
後重啓系統
- 開啓 HTTP 訪問。
[root@centos-server-01 audit]# setsebool -P httpd_can_network_connect 1
- 分析現有日誌並生成關聯模塊,執行完此命令可以看到在當前目錄下會生成後綴爲
*.pp
和*.te
文件,如果該服務器上的服務未被訪問過,此命令執行無效。
[root@centos-server-01 audit]# ausearch -c 'nginx' --raw | audit2allow -M my-nginx
- 加載前一步生成的模塊內容
[root@centos-server-01 audit]# semodule -i my-nginx.pp
- 執行完成以上命令後即可對 Nginx 進行正常訪問。