首页 > 技术文章 > Nginx 403 forbidden原因及故障模拟重现(转载)

shangzekai 2014-06-21 10:16 原文

这篇文章是转载过来的一篇文章,觉得不错,因此做个记录。

 

访问Nginx出现状态码为403 forbidden原因及故障模拟

1) nginx配置文件里不配置默认首页参数或者首页文件在站点目录下没有

1 index index.php index.html index.htm;
 1 [root@www extra]# cat www.conf
 2 #www virtualhost by oldboy
 3    server {
 4        listen       80;
 5        server_name  www.etiantian.org;
 6        location / {
 7            root   html/www;
 8            #index  index.html index.htm;#<==注释首页文件配置
 9        }
10        access_log off;
11    }
12 [root@www extra]# ../../sbin/nginx -sreload
13 [root@www extra]# tail -1 /etc/hosts
14 10.0.0.8 www.etiantian.orgbbs.etiantian.org blog.etiantian.org etiantian.org
15 [root@www extra]# ll ../../html/www/                   
16 总用量 12
17 drwxr-xr-x 2 root root 4096 4月  15 14:20 blog
18 -rw-r--r-- 1 root root    4 4月  17 17:11index.html #<==存在首页文件
19 drwxr-xr-x 2 root root 4096 4月  15 14:19 oldboy
20 [root@www extra]# curl -I -s 10.0.0.8|head -1
21 HTTP/1.1 403 Forbidden #<==问题是,Nginx没有指定首页文件的参数,因此访问Nginx时不会把index.html当首页,所以报403错误。

2)站点目录下没有配置文件里指定的首页文件index.php index.html index.htm。

 1 [root@www extra]# cat www.conf
 2 #www virtualhost by oldboy
 3    server {
 4        listen       80;
 5        server_name www.etiantian.org;
 6        location / {
 7            root   html/www;
 8            index  index.htmlindex.htm; #<==配置首页文件配置
 9        }
10        access_log off;
11    }
12 [root@www extra]# ../../sbin/nginx -sreload
13 [root@www extra]# rm -f ../../html/www/index.html#<==删除物理首页文件
14 [root@www extra]# curl -I -s 10.0.0.8|head -1
15 HTTP/1.1 403 Forbidden

提示:以上1)和2)有一个参数可以解决这个问题就是:

 1 autoindex on;
 2 [root@www extra]# cat www.conf
 3 #www virtualhost by oldboy
 4    server {
 5        listen       80;
 6        server_name  www.etiantian.org;
 7        location / {
 8            root   html/www;
 9            autoindex on; #<==当找不到首页文件时,会展示目录结构,这个功能一般不要用除非有需求。
10        }
11        access_log off;
12    }

3)站点目录或内部的程序文件没有Nginx用户访问权限。

1 [root@www extra]# echo test >../../html/www/index.html
2 [root@www extra]# chmod 700../../html/www/index.html #<==设置700让nginx用户无权读取
3 [root@www extra]# ls -l ../../html/www/index.html
4 -rwx------ 1 root root 5 4月  17 17:15../../html/www/index.html
5 [root@www extra]# curl -I -s 10.0.0.8|head-1
6 HTTP/1.1 403 Forbidden #<==403错误
7 [root@www extra]# chmod 755../../html/www/index.html #<==设置755让nginx用户有权读取
8 [root@www extra]# curl -I -s 10.0.0.8|head -1
9 HTTP/1.1 200 OK #<==200 OK了

4)Nginx配置文件中设置allow、deny等权限控制,导致客户端没有没权限访问。

 1 [root@www extra]# cat www.conf
 2 #www virtualhost by oldboy
 3    server {
 4        listen       80;
 5        server_name  www.etiantian.org;
 6        location / {
 7            root   html/www;
 8            index  index.html index.htm;
 9            allow 192.168.1.0/24;
10            deny all;
11        }
12        access_log off;
13    }
14 [root@www extra]# curl -I -s 10.0.0.8|head-1
15 HTTP/1.1 200 OK #<==设置755让nginx用户有权读取
16 [root@www extra]# ../../sbin/nginx -sreload
17 [root@www extra]# curl -I -s 10.0.0.8|head -1
18 HTTP/1.1 403 Forbidden

提示:上述403问题并不是nginx才有,apache服务的Forbidden 403问题同样也是这几个问题导致的,只是参数细节略有区别而已,见http://oldboy.blog.51cto.com/2561410/581383    感谢oldboy同学的分享。

推荐阅读