解决Nginx常见的问题 403 forbidden错误

配置Nginx,然后生成一键生成静态页面,然后就没有然后了,所有栏目页面都显示nginx 403 forbidden。

一般来说nginx 的 403 Forbidden errors 表示你在请求一个资源文件但是nginx不允许你查看。
403 Forbidden 只是一个HTTP状态码,像404,200一样不是技术上的错误。
哪些场景需要返回403状态码的场景?
1.网站禁止特定的用户访问所有内容,例:网站屏蔽某个ip访问。
2.访问禁止目录浏览的目录,例:设置autoindex off后访问目录。
3.用户访问只能被内网访问的文件。
以上几种常见的需要返回 403 Forbidden 的场景

一:由于启动用户和nginx工作用户不一致所致

1. 查看nginx的启动用户

命令:

ps aux | grep "nginx: worker process" | awk  '{print $1}' 
[root@localhost hc]# ps aux | grep "nginx: worker process" | awk  '{print $1}'
nginx
root

发现是nginx,而不是用root启动的

2. 将nginx.conf的user改为和启动用户一致

将nginx.conf文件中的 user 对应的nginx 改为 root ,改完后重启

[root@localhost hc]# vim /etc/nginx/nginx.conf
[root@localhost hc]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost hc]# nginx -s reload

  

二、缺少index.html或者index.php文件,就是配置文件中index index.html index.htm这行中的指定的文件

  

server {
listen 80;
server_name localhost;
index index.php index.html;
root / var/www;
}

  如果在/ var/www下面没有index.php,index.html的时候,直接访问域名,找不到文件,会报403 forbidden。

三、权限问题,如果nginx没有web目录的操作权限,也会出现403错误

解决办法:修改web目录的读写权限,或者是把nginx的启动用户改成目录的所属用户,重启Nginx即可解决

chmod -R 755 / var/www

  

四、SELinux设置为开启状态(enabled)的原因

首先查看本机SELinux的开启状态,如果SELinux status参数为enabled即为开启状态

/usr/sbin/ sestatus -v

  

或者使用getenforce命令检查

如何关闭 SELinux 呢

1. 临时关闭(不用重启)

setenforce  0

  

2. 永久关闭(需要重启)

修改配置文件 /etc/selinux/config,将SELINUX=enforcing改为SELINUX=disabled

vi /etc/selinux/config


#SELINUX=enforcing

SELINUX=disabled

  重启生效。

猜你喜欢

转载自www.cnblogs.com/henrylinux/p/10777698.html
今日推荐