nginx出現403錯誤的解決方法

emanlee發表於2024-08-31

nginx出現403錯誤的解決方法

2024/08/31 17:06:52 [error] 26005#26005: *11 "/root//frontend/dist/index.html" is forbidden (13: Permission denied), client: 220.196.160.53, server
: 81.70.112.191, request: "GET / HTTP/1.1", host: "81.70.112.191"

測試的結果:

1)用 我們自己的檔案 替換 nginx的預設示例資料夾html 中的內容,可以。

2) 把user nginx 改為 user root ,也可以

一、由於啟動使用者和nginx工作使用者不一致所致

1.1 檢視nginx的啟動使用者

[root@VM-8-12-centos ~]# ps aux | grep "nginx: worker process"
root 3920 0.0 0.0 112812 976 pts/2 S+ 17:08 0:00 grep --color=auto nginx: worker process
nginx 26004 0.0 0.1 41952 2488 ? S 16:48 0:00 nginx: worker process
nginx 26005 0.0 0.1 41952 2496 ? S 16:48 0:00 nginx: worker process

[root@VM-8-12-centos ~]# cat /etc/passwd | grep nginx
nginx:x:994:991:Nginx web server:/var/lib/nginx:/sbin/nologin


1.2 將 nginx.config 的 user 改為和啟動使用者一致,

命令:vi conf/nginx.conf

二、缺少index.html 檔案

1. server {
2. listen 80;
3. server_name localhost;
4. index index.php index.html;
5. root /XXX/www/;
6. }

三、許可權問題,如果nginx沒有web目錄的操作許可權,也會出現403錯誤。

解決辦法:修改web目錄的讀寫許可權,或者是把nginx的啟動使用者改成目錄的所屬使用者,重啟Nginx即可解決

1. chmod -R 777 /data
2. chmod -R 777 /data/www/

chown nginx:nginx -R /root/nginxlog

chown nginx:nginx -R /usr/local/logs/error.log


四、SELinux設定為開啟狀態(enabled)的原因。

4.1、檢視當前selinux的狀態。

/usr/sbin/sestatus

# /usr/sbin/sestatus
SELinux status: disabled

4.2、將SELINUX=enforcing 修改為 SELINUX=disabled 狀態。

1. vi /etc/selinux/config
2.
3. #SELINUX=enforcing
4. SELINUX=disabled
4.3、重啟生效。reboot。

reboot
重啟 nginx

nginx -s reload

參考:

https://www.sohu.com/a/260081807_639793

相關文章