http://s1.cb.aiwaly.com/M00/docs/nginx/nginx%20allow%20%26%20deny%20%E6%8C%87%E4%BB%A4%E8%A7%A3%E6%9E%90%20-%20%E7%9F%A5%E4%B9%8E.mhtml
https://zhuanlan.zhihu.com/p/378026632

1、allow 和 deny 指令在 ngx_http_access_module 模块中。

2、两个指令分别表示允许或禁止源 IP 访问,用于对源 IP 做访问控制。

3、nginx 是按照自上而下的顺序进行匹配,匹配到一个就不往下继续了。

4、遇到 return 指令时 return 指令还是会生效;

allow 指令

1、允许哪些 IP 访问,all 表示允许所有;

2、作用域 http / server / location / limit_except ;

deny 指令

1、禁止哪些 IP 访问,all 表示禁止所有;

2、作用域 http / server / location / limit_except ;

实验

【实验一、deny 在前】

# ngx_http_access_module
server {
    listen 8080;
    server_name _;

    charset "utf-8";
    location / {
        deny all;
        allow 192.168.135.1;
        #default_type text/plain;
        #return 200 "$remote_addr 正常访问 3";
        #echo "$remote_addr 正常访问 5";
    }
}

【实验二、deny 和 allow 指令调换位置】

# ngx_http_access_module
server {
    listen 8080;
    server_name _;

    charset "utf-8";
    location / {
        allow 192.168.135.1;
        deny all;
        #default_type text/plain;
        #return 200 "$remote_addr 正常访问 3";
        #echo "$remote_addr 正常访问 5";
    }
}

【实验三、deny 和 allow 指令再调换位置,加上 return 指令】

# ngx_http_access_module
server {
    listen 8080;
    server_name _;

    charset "utf-8";
    location / {
        deny all;
        allow 192.168.135.1;
        default_type text/plain;
        return 200 "$remote_addr 正常访问 3";
        #echo "$remote_addr 正常访问 5";
    }
}

【实验四、把 return 指令换成 echo 指令】

# ngx_http_access_module
server {
    listen 8080;
    server_name _;

    charset "utf-8";
    location / {
        deny all;
        allow 192.168.135.1;
        default_type text/plain;
        #return 200 "$remote_addr 正常访问 3";
        echo "$remote_addr 正常访问 5";
    }
}

【实验五、deny 和 allow 指令再次调换位置】

# ngx_http_access_module
server {
    listen 8080;
    server_name _;

    charset "utf-8";
    location / {
        allow 192.168.135.1;
        deny all;
        default_type text/plain;
        #return 200 "$remote_addr 正常访问 3";
        echo "$remote_addr 正常访问 5";
    }
}

【实验六、在 deny 和 allow 指令之后使用 root 指令】

# ngx_http_access_module
server {
    listen 8080;
    server_name _;

    charset "utf-8";
    location / {
        allow 192.168.135.1;
        deny all;
        root /opt/data/images;
        #default_type text/plain;
        #return 200 "$remote_addr 正常访问 3";
        #echo "$remote_addr 正常访问 5";
    }
}

【实验七、调换 deny 和 allow 指令位置】

# ngx_http_access_module
server {
    listen 8080;
    server_name _;

    charset "utf-8";
    location / {
        deny all;
        allow 192.168.135.1;

        root /opt/data/images;
        #default_type text/plain;
        #return 200 "$remote_addr 正常访问 3";
        #echo "$remote_addr 正常访问 5";
    }
}

【实验八、在 deny 和 allow 指令之前使用 root 指令】

# ngx_http_access_module
server {
    listen 8080;
    server_name _;

    charset "utf-8";
    location / {
        root /opt/data/images;
        allow 192.168.135.1;
        deny all;

        #default_type text/plain;
        #return 200 "$remote_addr 正常访问 3";
        #echo "$remote_addr 正常访问 5";
    }
}

【实验九、调换 deny 和 allow 指令位置】

# ngx_http_access_module
server {
    listen 8080;
    server_name _;

    charset "utf-8";
    location / {
        root /opt/data/images;
        deny all;
        allow 192.168.135.1;

        #default_type text/plain;
        #return 200 "$remote_addr 正常访问 3";
        #echo "$remote_addr 正常访问 5";
    }
}
文档更新时间: 2022-10-14 06:44   作者:月影鹏鹏
IT运维支持-AIWALY-月影工作室