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
    

猜你喜欢

转载自blog.csdn.net/liuxiaoming1109/article/details/85145993
今日推荐