Home  >  Article  >  Database  >  nginx访问限制

nginx访问限制

WBOY
WBOYOriginal
2016-06-07 15:31:14963browse

nginx中通过 访问 控制来允许或拒绝某些IP或用户 访问 。常用的方法如下: 一:通过ngx_http_access_module模块来允许某些IP的客户端 访问 ,通过关键字allow和deny来实现 allow :允许指定的网络地址 访问 。 deny :拒绝指定的网络地址 访问 。 location /

  nginx中通过访问控制来允许或拒绝某些IP或用户访问。常用的方法如下:


一:通过ngx_http_access_module模块来允许某些IP的客户端访问,通过关键字allow和deny来实现

  allow:允许指定的网络地址访问

   deny:拒绝指定的网络地址访问

   

location / {
    deny    192.168.2.6;
    allow   192.168.2.0/24;
    deny    all;
}

   上面的例子中,规则从上到下依次检测,允许192.168.2.0/24网络访问,但192.168.2.6除外,其余的都拒绝。

二:基于ngx_http_auth_basic_module模块允许使用“HTTP基本认证”协议验证用户名和密码来限制对资源的访问

   auth_basic STRING|off;    //默认为off

  auth_basic_user_file FIEL;    指定保存用户名和密码的文件。

   

location / {
    auth_basic "private";
    auth_basic_user_file /usr/nginx/.htpasswd;
}

其中.htpasswd文件可以手动创建,也可以使用Apache发行包中的htpasswd命令来创建此文件。

  htpasswd的使用方法:

  htpasswd -cm /usr/nginx/.htpasswd user1

  第一次使用时要加"-c"参数,以后不需要了。

三:satisfy实现同时通过IP地址和密码来限制访问

    satisfy all|any;   //默认为all

   上面说到两个模块ngx_http_access_module和ngx_http_auth_basic_module,当satisfy使用参数all时,表示只有当两个模块的所有限制条件都授权访问时,才允许请求访问;当使用参数any时,表示当任意模块的任意限制条件授权访问时,允许请求访问


location / {
    satisfy any;
    allow 192.168.2.0/24;
    deny all;
    auth_basic "private";
    auth_basic_user_file /usr/nginx/.htpasswd;
}

   上面例子表示只要满足一组限制,即可访问。    


阿三哥

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn