zoukankan      html  css  js  c++  java
  • Nginx 403 forbidden多种原因及故障模拟重现

    访问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/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。

    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-1
    HTTP/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;
       }

    效果如下:

    wKiom1Uw1g6zDOf6AACTK7b6f9U697.jpg

    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-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等权限控制,导致客户端没有没权限访问。

    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-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
  • 相关阅读:
    [TimLinux] TCP全连接队列满
    [TimLinux] JavaScript 中循环执行和定时执行
    [TimLinux] JavaScript 事件
    [TimLinux] JavaScript 获取设置在CSS类中的属性值
    [TimLinux] JavaScript 面向对象程序设计
    [TimLinux] JavaScript 引用类型——Date
    [TimLinux] django html如何实现固定表头
    [TimLinux] Django 信号
    [TimLinux] Django 中间件
    安卓存储之文件存储方式j
  • 原文地址:https://www.cnblogs.com/luoahong/p/6444101.html
Copyright © 2011-2022 走看看