The following modules allow you to regulate access to the documents of your websites — require users to authenticate, match a set of rules, or simply restrict access to certain visitors.

Auth_basic Module

The auth_basic module enables the basic authentication functionality. With the two directives that it reveals, you can make it so that a specific location of your website (or your server) is restricted to users that authenticate using a username and password:

location /admin/ {
  auth_basic "Admin control panel";
  auth_basic_user_file access/password_file;
}

The first directive, auth_basic, can be set to either off or a text message usually referred to as authentication challenge or authentication realm. This message is displayed by web browsers in a username/password box when a client attempts to access the protected resource.

The second one, auth_basic_user_file, defines the path of the password file relative to the directory of the configuration file. A password file is formed of lines respecting the following syntax: username:password[:comment]. The password must be encrypted with the crypt(3) function, for example, using the htpasswd command-line utility from Apache.

If you aren't too keen on installing Apache on your system just for the sake of the htpasswd tool, you may resort to online tools as there are plenty of them available. Fire up your favorite search engine and type "online htpasswd".

Access

Two important directives are brought up by this module: allow and deny. They let you allow or deny access to a resource for a specific IP address or IP address range. Both directives have the same syntax: allow IP | CIDR | all, where IP is an IP address, CIDR is an IP address range (CIDR syntax), and all specifies that the directive applies to all clients:

location {
  allow 127.0.0.1; # allow local IP address
  deny all; # deny all other IP addresses
}

Note that rules are processed from top-down — if your first instruction is deny all, all possible allow exceptions that you place afterwards will have no effect. The opposite is also true — if you start with allow all, all possible deny directives that you place afterwards will have no effect, as you already allowed all IP addresses.

Limit Connections

The mechanism induced by this module is a little more complex than regular ones. It allows you to define the maximum amount of simultaneous connections to the server for a specific zone.

The first step is to define the zone using the limit_conn_zone directive:

  • Directive syntax: limit_conn_zone $variable zone=name:size;
  • $variable is the variable that will be used to differentiate one client from another, typically $binary_remote_addr — the IP address of the client in binary format (more efficient than ASCII)
  • name is an arbitrary name given to the zone
  • size is the maximum size you allocate to the table storing session states

The following example defines zones based on the client IP addresses:

limit_conn_zone $binary_remote_addr zone=myzone:10m;

Now that you have defined a zone, you may limit connections using limit_conn:

limit_conn zone_name connection_limit;

When applied to the previous example it becomes:

location /downloads/ {
  limit_conn myzone 1;
}

As a result, requests that share the same $binary_remote_addr are subject to the connection limit (one simultaneous connection). If the limit is reached, all additional concurrent requests will be answered with a 503 Service Unavailable HTTP response. If you wish to log client requests that are affected by the limits you have set, enable the limit_conn_log_level directive and specify the log level (info | notice | warn | error).

Limit Request

In a similar fashion, the Limit Request module allows you to limit the amount of requests for a defined zone.

Defining the zone is done via the limit_req_zone directive; its syntax differs from the Limit zone equivalent directive:

limit_req_zone $variable zone=name:max_memory_size rate=rate;

The directive parameters are identical, except for the trailing rate: expressed in requests per second (r/s) or requests per minute (r/m). It defines a request rate that will be applied to clients where the zone is enabled. To apply a zone to a location, use the limit_req directive:

limit_req zone=name burst=burst [nodelay];

The burst parameter defines the maximum possible bursts of requests — when the amount of requests received from a client exceeds the limit defined in the zone, the responses are delayed in a manner that respects the rate that you defined. To a certain extent, only a maximum of burst requests will be accepted simultaneously. Past this limit, Nginx returns a 503 Service Unavailable HTTP error response:

limit_req_zone $binary_remote_addr zone=myzone:10m rate=2r/s;
[…]
location /downloads/ {
  limit_req zone=myzone burst=10;
}

If you wish to log client requests that are affected by the limits you have set, enable the limit_req_log_level directive and specify the log level (info | notice | warn | error).

Nginx - Additional Modules, Limits and Restrictions的更多相关文章

  1. Nginx - Additional Modules, Website Access and Logging

    The following set of modules allows you to configure how visitors access your website and the way yo ...

  2. Nginx - Additional Modules, Content and Encoding

    The following set of modules provides functionalities having an effect on the contents served to the ...

  3. Nginx - Additional Modules, About Your Visitors

    The following set of modules provides extra functionality that will help you find out more informati ...

  4. Nginx - Additional Modules, SSL and Security

    Nginx provides secure HTTP functionalities through the SSL module but also offers an extra module ca ...

  5. nginx---reference

    nginx (pronounced "engine x") is a free open source web server written by Igor Sysoev, a R ...

  6. 使用wordpress搭建自己的独立博客

    最近想要搭建自己的私人博客, 各种百度,完整的搭建步骤如下! 首先得要有自己的vps或者云主机,我这里是自己的云主机,有自己的域名(我这边目前没有买域名)! 搭建步骤! 1,安装lnmp(linux+ ...

  7. 搭建 WordPress 博客教程

    搭建 WordPress 博客教程(超详细) 在 2018年7月29日 上张贴 由 suncent一条评论 本文转自:静候那一米阳光 链接:https://www.jianshu.com/p/5675 ...

  8. nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati

    root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx  --add-mod ...

  9. Table of Contents - Nginx

    Downloading and  Installing Nginx Nginx for Windows Basic Nginx Configuration Configuration File Syn ...

随机推荐

  1. Android流量监控 思路,想法

    1,开启一个服务,每5分钟跑动一次更新流量,用于能够准确记录流量         每一个小时,更新一次流量,用于清除非本月的流量 2,保存流量的时候,进行判断         a,若是数据库中保存的 ...

  2. HDU4570----Multi-bit Trie----简单的DP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4570 题目意思: 给你N个数 要你分成多段,每段长度不能超过20 是的sum(ai*(2^bi))最小 ...

  3. Python3学习

    要做一个儿童搜索引擎的项目(www.kidsearch.cn),所以开始接触各种新的语言,初步学了PHP爬虫,觉得要做大点的项目用PHP确实不太方便,中文兼容性就是一个比较棘手的问题.python的爬 ...

  4. cocos2d-x 仿真树叶飘落效果的实现

    转自:http://blog.csdn.net/ufolr/article/details/7624851 最近项目中需要一个落叶的效果,本来想用粒子特效来实现,但是几经调试,虽然调出了落叶的效果,但 ...

  5. 基础数据结构 之 栈(python实现)

    栈是编程开发中的两种较为简单的数据结构.栈和队可用于模拟函数的递归.栈的特点是后进先出.其常用操作包括:出栈,入栈等.在出栈前,需判断栈是否为空.在入栈时,需判断栈是否已满. 下面给出一个用pytho ...

  6. 数据字符集mysql主从数据库,分库分表等笔记

    文章结束给大家来个程序员笑话:[M] 1.mysql的目录:在rpm或者yum安装时:/var/lib/mysql  在编译安装时默许目录:/usr/local/mysql 2.用rpm包安装的MyS ...

  7. BZOJ 2748: [HAOI2012]音量调节 dp

    2748: [HAOI2012]音量调节 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  8. 使用自定义的BaseAdapter实现LIstView的展示

    http://stephen830.iteye.com/blog/1141394 使用自定义的BaseAdapter实现LIstView的展示 实现以下功能点: 1.通过自定义的BaseAdapter ...

  9. 关于更改apache和mysql的路径的问题..

    1.禁用selinux 系统管理->selinux管理->enforing模式..改为disable..然后重启 2.修改httpd.conf的各个路径 索引后发现指向欢迎页面则注释下面这 ...

  10. 在Linux里设置环境变量的方法(export PATH)

    一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量.例如我的mips-linux-gcc编译器在“/opt/au1200_rm/build_tools/bin”目录下,b ...