在使用 yum 安裝 nginx 后可能會出現(xiàn)配置完成后卻無法訪問的問題,查看 audit.log 會發(fā)現(xiàn)類似于以下的錯誤信息

audit.log
出現(xiàn)此問題的原因是 SELinux 基于最小權(quán)限原則默認(rèn)攔截了 Nginx 的請求,SELinux 是 Linux 的安全子系統(tǒng),提供更安全的訪問控制,許多運(yùn)維人員嫌麻煩可能會直接關(guān)閉此組件,但是治標(biāo)不治本,本文演示在啟用 SELinux 基礎(chǔ)上完成對 Nginx 請求的放行。
- 首先我們需要確認(rèn) SELinux 的運(yùn)行狀態(tài),當(dāng)然出現(xiàn)此問題肯定是運(yùn)行中。
[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
臨時關(guān)閉 SELinux:setenforce 0
臨時啟動 SELinux:setenforce 1
永久關(guān)閉/啟動:修改/etc/sysconfig/selinux后重啟系統(tǒng)
- 開啟 HTTP 訪問。
[root@centos-server-01 audit]# setsebool -P httpd_can_network_connect 1
- 分析現(xiàn)有日志并生成關(guān)聯(lián)模塊,執(zhí)行完此命令可以看到在當(dāng)前目錄下會生成后綴為
*.pp和*.te文件,如果該服務(wù)器上的服務(wù)未被訪問過,此命令執(zhí)行無效。
[root@centos-server-01 audit]# ausearch -c 'nginx' --raw | audit2allow -M my-nginx
- 加載前一步生成的模塊內(nèi)容
[root@centos-server-01 audit]# semodule -i my-nginx.pp
- 執(zhí)行完成以上命令后即可對 Nginx 進(jìn)行正常訪問。