Nginx 403 forbidden多种原因及故障模拟重现
原文地址:http://blog.51cto.com/oldboy/1633952
访问Nginx出现状态码为403 forbidden原因及故障模拟
1) nginx配置文件里不配置默认首页参数或者首页文件在站点目录下没有
|
1
|
index index.php index.html index.htm; |
问题模拟示例:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[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/hosts10.0.0.8 www.etiantian.orgbbs.etiantian.org blog.etiantian.org etiantian.org[root@www extra]# ll ../../html/www/ 总用量 12drwxr-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-1HTTP/1.1 403 Forbidden #<==问题是,Nginx没有指定首页文件的参数,因此访问Nginx时不会把index.html当首页,所以报403错误。 |
2)站点目录下没有配置文件里指定的首页文件index.php index.html index.htm。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[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-1HTTP/1.1 403 Forbidden |
提示:以上1)和2)有一个参数可以解决这个问题就是:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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用户访问权限。
|
1
2
3
4
5
6
7
8
9
|
[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-1HTTP/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-1HTTP/1.1 200 OK #<==200 OK了 |
4)Nginx配置文件中设置allow、deny等权限控制,导致客户端没有没权限访问。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[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-1HTTP/1.1 200 OK #<==设置755让nginx用户有权读取[root@www extra]# ../../sbin/nginx -sreload[root@www extra]# curl -I -s 10.0.0.8|head-1HTTP/1.1 403 Forbidden |
提示:上述403问题并不是nginx才有,apache服务的Forbidden 403问题同样也是这几个问题导致的,只是参数细节略有区别而已,见http://oldboy.blog.51cto.com/2561410/581383

浙公网安备 33010602011771号