执门文章
Selinux授权nginx访问的静态文件目录
于 2023-12-15 07:48:50 发布
访问534
收藏1
文章标签:
nginx

背景:在我们安装好nginx之后,nginx默认访问题是OK的,但配置了自己的网页静态html之后,访问就变成了403,无法访问,检查了nginx所有权限都是对的,如(1)nginx启动用root,(2)网页html路径都存在,(3)网页html路径对应的nginx启动用户都有访问权限;这个时间自己的网页还报403,肯定是Selinux安全做了拦截,Selinux是Linux最重要的安全组件,是美国国家安全局开发,对于生产环境,不能直接禁用,禁用了,就降低了服务器的安全性

检查Selinux状态

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

为enablee,为在运行状态


一、查看nginx进程

ps -efZ | grep nginx

system_u:system_r:httpd_t:s0    root      1178     1  0 09:24 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

system_u:system_r:httpd_t:s0    root      1179  1178  0 09:24 ?        00:00:00 nginx: worker process

system_u:system_r:httpd_t:s0    root      1180  1178  0 09:24 ?        00:00:00 nginx: worker process

system_u:system_r:httpd_t:s0    root      1181  1178  0 09:24 ?        00:00:00 nginx: worker process

system_u:system_r:httpd_t:s0    root      1182  1178  0 09:24 ?        00:00:00 nginx: worker process

system_u:system_r:httpd_t:s0    root      1183  1178  0 09:24 ?        00:00:00 nginx: worker process

system_u:system_r:httpd_t:s0    root      1184  1178  0 09:24 ?        00:00:00 nginx: worker process

system_u:system_r:httpd_t:s0    root      1185  1178  0 09:24 ?        00:00:00 nginx: worker process

system_u:system_r:httpd_t:s0    root      1186  1178  0 09:24 ?        00:00:00 nginx: worker process

unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 3566 1863  0 10:19 pts/1 00:00:00 grep --color=auto nginx

注意此时进程对应的是httpd_t


二,查看nginx自己可以访问的目录

cd /usr/share/nginx/html

ls -Z

-rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 50x.html

drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 aaaa

-rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 index.html

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 my.txt

关键点,此时nginx可以正常访问的httpd_sys_content_t,我们需要将我们的网站目录也要改成httpd_sys_content_t,即可以访问了,自己用ls -z命令查看自己的目录,肯定不是httpd_sys_content_t,


三,开始动手了

1、安装policycoreutils-python工具


yum -y install policycoreutils-python

2、修改目录/home/www


semanage fcontext -a -t httpd_sys_content_t '/home/www(/.*)?'

3、刷新/home/www


restorecon -R /home/www

4、查看


cd /home/wwww

ls -Z

此时就可以正常访问网站了

Selinux授权nginx访问的静态文件目录

于 2023-12-15 07:48:50 发布
访问534
收藏1

实力雄厚的技术网站

产品

概述

资源

文档

版权问题

请联系客服

联系我们

联系我们


© 财瑞智能科技 2024蜀ICP备2023018175号-2川公网安备51011202000656号