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).
Nginx - Additional Modules, Limits and Restrictions的更多相关文章
- 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 ...
- Nginx - Additional Modules, Content and Encoding
The following set of modules provides functionalities having an effect on the contents served to the ...
- Nginx - Additional Modules, About Your Visitors
The following set of modules provides extra functionality that will help you find out more informati ...
- Nginx - Additional Modules, SSL and Security
Nginx provides secure HTTP functionalities through the SSL module but also offers an extra module ca ...
- nginx---reference
nginx (pronounced "engine x") is a free open source web server written by Igor Sysoev, a R ...
- 使用wordpress搭建自己的独立博客
最近想要搭建自己的私人博客, 各种百度,完整的搭建步骤如下! 首先得要有自己的vps或者云主机,我这里是自己的云主机,有自己的域名(我这边目前没有买域名)! 搭建步骤! 1,安装lnmp(linux+ ...
- 搭建 WordPress 博客教程
搭建 WordPress 博客教程(超详细) 在 2018年7月29日 上张贴 由 suncent一条评论 本文转自:静候那一米阳光 链接:https://www.jianshu.com/p/5675 ...
- 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 ...
- Table of Contents - Nginx
Downloading and Installing Nginx Nginx for Windows Basic Nginx Configuration Configuration File Syn ...
随机推荐
- PHP再学习1——cURL表单提交、HTTP请求和响应分析
1.前言 最近迷恋WEB方面的技术,虽然自己是一个嵌入式工程师,但是我深知若需要把传感器终端的数据推送至“平台”必然会和WEB技术打交道.在工作中发现嵌入式工程师喜欢 二进制形式的协议,例如MODBU ...
- Kicad使用经验谈
最近开始学习使用Linux上的开源软件KiCad来绘制电路图和PCB.学习这个还是比较快的,用了两天了,觉得还是蛮方便的. 在这两天的使用以及今后的使用过程中,一定会有很多想要谈的.所以,就写下这篇博 ...
- LINUX的一些常用操作
CentOs6.7关闭防火墙(SecureCRT连接不上) 解决方法:______________________________________一.开启SSH以root用户登录Linux,打开终端, ...
- [前端JS学习笔记]JavaScript function
一.函数的声明 1.1 function 命令 function methodName(params) { // code } 如下声明: function test_function(params) ...
- oracle安装界面中文乱码解决
在安装oracle时如果我们用的是英文安装没有任何问题,但是我要安装中文的,结果中文界面就出现了乱码了,后来网上找了原因是要安装中文包才可以,下面我来介绍一下. 在Linux的X window里安装o ...
- freeRadius 基础配置及测试
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- 解决VS2012新建MVC4等项目时,收到加载程序集“NuGet.VisualStudio.Interop…”的错误
初装V2012,新建MVC4新项目时出现以下错误: 解决方法为: 通过VS2012的“工具-扩展和更新-联机”安装“NuGet Package Manager”扩展包,可以顺利新建MVC4项目啦!
- HTML输出 二 控制行背景颜色
$Infors = Get-Content ports01.txt$Temp_PortStatustxt = "C:\Windows\Temp\PortStatustxt.txt" ...
- crm操作货币实体
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; /// <summary> ...
- SQL Server 数据导入Mysql详细教程