Nginx - Additional Modules, Limits and Restrictions

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).

时间: 2024-08-06 08:28:36

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

Nginx - Additional Modules, Content and Encoding

The following set of modules provides functionalities having an effect on the contents served to the client, either by modifying the way the response is encoded, by affecting the headers, or by generating a response from scratch. Empty GIF The purpos

Nginx - Additional Modules, About Your Visitors

The following set of modules provides extra functionality that will help you find out more information about the visitors, such as by parsing client request headers for browser name and version, assigning an identifier to requests presenting similari

Nginx - Additional Modules, Website Access and Logging

The following set of modules allows you to configure how visitors access your website and the way your server logs requests. Index The Index module provides a simple directive named index, which lets you define the page that Nginx will serve by defau

nginx open files limits 导致大量错误信息

nginx  error.log 中出现大量如下错误信息: [[email protected] nginx]# grep -aP '^20.* \[crit\]' error.log 2017/03/14 12:06:31 [crit] 3549#0: accept4() failed (24: Too many open files) [[email protected] nginx]# grep -aP '^20.* \[alert\]' error.log 2017/03/14 16:0

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

[email protected]:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx  --add-module=/usr/local/src/nginx-rtmp-module  --with-htchecking for OS + Linux 4.4.0-116-generic x86_64checking for C compiler ... found + using GNU C compiler + gc

Nginx - HTTP Configuration, Module Directives

Socket and Host Configuration This set of directives will allow you to configure your virtual hosts. In practice, this materializes by creating server blocks that you identify either by a hostname or by an IP address and port combination. In addition

Nginx Upload Module 上传模块

传统站点在处理文件上传请求时,普遍使用后端编程语言处理,如:Java.PHP.Python.Ruby等.今天给大家介绍Nginx的一个模块,Upload Module上传模块,此模块的原理是先把用户上传的文件保存到临时文件,然后在交由后台页面处理,并且把文件的原名,上传后的名称,文件类型,文件大小set到页面. GitHub: https://github.com/vkholodkov/nginx-upload-module/tree/2.2 Site: http://wiki.nginx.or

[Angular2 Router] Lazy Load Angular 2 Modules with the Router

Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start up faster because it only needs to use the main App Module when the page initially loads. As you navigate between routes, it will load the additional

SaltStack安装Nginx

1. 思路整理 五步走: 整个base环境规划 工作当中,我们在使用SaltStack的时候,环境目录的规划尽量做到标准化,自己要严格要求好!这也是作为一个运维工程师的基本技能要求. 安装Nginx(依赖包) 在本文章中,我的Example使用的是源码安装,也是当前主流的安装方式!(这里会用到pkg.installed,cmd.run等多种状态管理模块) 配置管理 在安装好服务之后,我们需要对服务进行基本的配置管理,通过模板文件来进行统一管理!(会用到"file.managed",&q