訪問Nginx出現狀態碼爲403 forbidden原因及故障模擬
1) nginx配置文件裏不配置默認首頁參數或者首頁文件在站點目錄下沒有
index index.php index.html index.htm;
問題模擬示例:
[root@www extra]# cat www.conf #www virtualhost by oldboy server { listen 80; server_name www.etiantian.org; location / { root html/www; #index index.html index.htm;#<==註釋首頁文件配置 } access_log off; } [root@www extra]# ../../sbin/nginx -sreload [root@www extra]# tail -1 /etc/hosts 10.0.0.8 www.etiantian.orgbbs.etiantian.org blog.etiantian.org etiantian.org [root@www extra]# ll ../../html/www/ 總用量 12 drwxr-xr-x 2 root root 4096 4月 15 14:20 blog -rw-r--r-- 1 root root 4 4月 17 17:11index.html #<==存在首頁文件 drwxr-xr-x 2 root root 4096 4月 15 14:19 oldboy [root@www extra]# curl -I -s 10.0.0.8|head-1 HTTP/1.1 403 Forbidden #<==問題是,Nginx沒有指定首頁文件的參數,因此訪問Nginx時不會把index.html當首頁,所以報403錯誤。
2)站點目錄下沒有配置文件裏指定的首頁文件index.php index.html index.htm。
[root@www extra]# cat www.conf #www virtualhost by oldboy server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.htmlindex.htm; #<==配置首頁文件配置 } access_log off; } [root@www extra]# ../../sbin/nginx -sreload [root@www extra]# rm -f ../../html/www/index.html#<==刪除物理首頁文件 [root@www extra]# curl -I -s 10.0.0.8|head-1 HTTP/1.1 403 Forbidden
提示:以上1)和2)有一個參數可以解決這個問題就是:
autoindex on; [root@www extra]# cat www.conf #www virtualhost by oldboy server { listen 80; server_name www.etiantian.org; location / { root html/www; autoindex on; #<==當找不到首頁文件時,會展示目錄結構,這個功能一般不要用除非有需求。 } access_log off; }
效果如下:
3)站點目錄或內部的程序文件沒有Nginx用戶訪問權限。
[root@www extra]# echo test >../../html/www/index.html [root@www extra]# chmod 700../../html/www/index.html #<==設置700讓nginx用戶無權讀取 [root@www extra]# ls -l ../../html/www/index.html -rwx------ 1 root root 5 4月 17 17:15../../html/www/index.html [root@www extra]# curl -I -s 10.0.0.8|head-1 HTTP/1.1 403 Forbidden #<==403錯誤 [root@www extra]# chmod 755../../html/www/index.html #<==設置755讓nginx用戶有權讀取 [root@www extra]# curl -I -s 10.0.0.8|head-1 HTTP/1.1 200 OK #<==200 OK了
4)Nginx配置文件中設置allow、deny等權限控制,導致客戶端沒有沒權限訪問。
[root@www extra]# cat www.conf #www virtualhost by oldboy server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; allow 192.168.1.0/24; deny all; } access_log off; } [root@www extra]# curl -I -s 10.0.0.8|head-1 HTTP/1.1 200 OK #<==設置755讓nginx用戶有權讀取 [root@www extra]# ../../sbin/nginx -sreload [root@www extra]# curl -I -s 10.0.0.8|head-1 HTTP/1.1 403 Forbidden
提示:上述403問題並不是nginx纔有,apache服務的Forbidden 403問題同樣也是這幾個問題導致的,只是參數細節略有區別而已,見http://oldboy.blog.51cto.com/2561410/581383
本文來自老男孩老師的新書內容預告,<跟老男孩學linux運維> 老男孩出書交流Q羣 339128815 226199808