我是在在本地用虚拟机中通过yum安装nginx的,安装一切正常,但是访问时报403

414d24224c74fb41b975bb017658121c.png

于是查看nginx日志,路径为/var/log/nginx/error.log。打开日志发现报错Permission denied,详细报错如下:2018/11/28 11:39:40 [error] 41772#41772: *130 "/home/hc/dists/autoAweme/dist/index.html" is forbidden (13: Permission denied), client: 192.168.3.139, server: 192.168.3.139, request: "GET / HTTP/1.1", host: "192.168.3.139"

一:由于启动用户和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=disabledvi /etc/selinux/config#SELINUX=enforcing

SELINUX=disabled

重启生效。reboot

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐