centos nginx 配置静态资源 浏览器访问出现 403 forbidden

环境说明:

  • 新装系统centos7
  • 配置完成nginx,一切正常,浏览器可以访问成功
  • 浏览器访问静态资源报403 forbidden错误
  • nginx配置访问静态资源配置网上太多此处略过

原因分析:

  • 403 forbidden是HTTP协议中的一个状态码(Status Code)。可以简单的理解为没有权限访问此站。
  • 查看nginx日志,路径:/var/log/nginx/error.log;打开日志发现报错Permission denied,详细报错如下:
    2018/12/19 18:23:59 [error] 5032#5032: *1 "/home/index.html" is forbidden (13: Permission denied), client: 192.168.1.32, server: _, request: "GET / HTTP/1.1", host: "192.168.1.160"
    

有以上差不多就是权限的问题不能访问

解决方案:

  1. vim /etc/nginx.config
    nginx.config的user改为和启动用户一致,我是root用户
    user nginx 修改成 user root
    
  2. vim /etc/selinux/config
    SELINUX=enforcing 修改成 SELINUX=disabled
    
  3. 重启系统生效
    reboot
    

SELinux解释说明:

  • SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统。
    一般我们不用它,安全防护可以用防火墙等

  • 通过命令 cat /etc/selinux/config 来查看状态 默认状态是enforcing

    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=disabled
    # SELINUXTYPE= can take one of three values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    

    通过配置发现SELinux有3种状态:enforcing (执行中)、permissive (不执行但产生警告)、disabled(关闭)
    修改后需要重启系统生效

  • 当然也可以通过 /usr/sbin/sestatus 查看SELinux状态

    SELinux status:                 disabled
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值