我是在在本地用虛擬機中通過yum安裝nginx的,安裝一切正常,但是訪問時報403,
於是查看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=disabled
vi /etc/selinux/config
#SELINUX=enforcing
SELINUX=disabled
重啟生效。
reboot
參考:https://blog.csdn.net/onlysunnyboy/article/details/75270533