Nginx 常用模块

Nginx 常用模块

1. ngx_http_autoindex_module
# ngx_http_autoindex_module模块处理以斜杠字符(' / ')结尾的请求,并生成一个目录列表。
# 通常,当ngx_http_index_module模块找不到索引文件时,请求被传递给ngx_http_autoindex_module模块。
`# Example 
location / {
    autoindex on;
}
`# 启用或禁用目录列表输出。
autoindex 								
# Syntax: 	autoindex on | off;
# Default: 	autoindex off;
# Context: 	http, server, location

`# 对于HTML格式,指定应该在目录清单中输出确切的文件大小
autoindex_exact_size			
# Syntax: 	autoindex_exact_size on | off;
# Default: 	autoindex_exact_size on;
# Context: 	http, server, location

`# 设置目录列表的格式。
autoindex_format					
# Syntax: 	autoindex_format html | xml | json | jsonp;
# Default: 	autoindex_format html;
# Context: 	http, server, location
# This directive appeared in version 1.7.9. 

`# 对于HTML格式,指定目录列表中的时间应该在本地时区还是UTC中输出。
autoindex_localtime				
# Syntax: 	autoindex_localtime on | off;
# Default: 	autoindex_localtime off;
# Context: 	http, server, location
2.ngx_http_access_module
# ngx_http_access_module模块允许限制对某些客户端地址的访问。
# 访问也可能受到密码、子请求结果或JWT的限制。地址和密码访问的同时限制由satisfy指令控制。
`# Example 
location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}
# 规则按顺序检查,直到找到第一个匹配项为止。
# 本例,仅允许访问IPv4网络10.1.1.0/16和192.168.1.0/24,不包括地址192.168.1.1,而对于IPv6网络2001:0db8::/32。
# 对于很多规则,最好使用ngx_http_geo_module模块变量。
`# 允许访问指定的网络或地址。如果指定了特殊值unix:(1.5.1),则允许访问所有unix域套接字。
allow			
# Syntax: 	allow address | CIDR | unix: | all;
# Default: 	—
# Context: 	http, server, location, limit_except

`# 拒绝访问指定的网络或地址。如果指定了特殊值unix:(1.5.1),则拒绝对所有unix域套接字的访问
deny			
# Syntax: 	deny address | CIDR | unix: | all;
# Default: 	—
# Context: 	http, server, location, limit_except
3.ngx_http_auth_basic_module
# ngx_http_auth_basic_module模块允许使用“HTTP基本身份验证”协议验证用户名和密码,从而限制对资源的访问。
# 访问也可以由地址、子请求结果或JWT来限制。地址和密码访问的同时限制由satisfy指令控制。
`# Example 
location / {
    auth_basic           "closed site";
    auth_basic_user_file conf/htpasswd;
}
`# 允许使用“HTTP基本身份验证”协议验证用户名和密码。
`# 指定的参数用作域。参数值可以包含变量(1.3.10,1.2.7)。特殊值off允许取消从以前配置级别继承的auth_basic指令的效果。
auth_basic 	
# Syntax: 	auth_basic string | off;
# Default: 	auth_basic off;
# Context: 	http, server, location, limit_except

`# 指定一个保存用户名和密码的文件;
auth_basic_user_file	
# Syntax: 	auth_basic_user_file file;
# Default: 	—
# Context: 	http, server, location, limit_except
`# Format password file
# comment
name1:password1
name2:password2:comment
name3:password3

`# Example
[root@web107 ~]# htpasswd -bc /etc/nginx/conf/htpasswd old 123
[root@web107 ~]# cat /etc/nginx/conf/htpasswd 
old:$apr1$Bse9UVLa$rLFQhUf7Fdi0EtBvldov9.
4.ngx_http_core_module –>limit_rate –>limit_rate_after
# 传输速率限制
# 设置初始数量,在此之后,将限制对客户端进一步传输响应的速率
`# Example 
map $slow $rate {
    1     4k;
    2     8k;
}
limit_rate $rate;
# 可以在$limit_rate变量中设置速率限制
server {
    if ($slow) {
        set $limit_rate 4k;
    }
    ...
}
# 速率限制也可以在代理服务器响应的“X-Accel-Limit-Rate”头字段中设置。
# 可以使用proxy_ignore_headers、fastcgi_ignore_headers、uwsgi_ignore_headers和scgi_ignore_headers指令禁用此功能。


`# Example:
    location /flv/ {
        flv;
        limit_rate_after 500k;
        limit_rate       50k;
    }
`# 限制向客户机传输响应的速率。速率以每秒字节数指定。零值禁用速率限制。
`# 这个限制是为每个请求设置的,因此如果一个客户机同时打开两个连接,那么总体速率将是指定限制的两倍。
limit_rate
# Syntax: 	limit_rate rate;
# Default: 	limit_rate 0;
# Context: 	http, server, location, if in location

`#设置初始数量,在此之后,将限制对客户端进一步传输响应的速率。
limit_rate_after 
# Syntax: 	limit_rate_after size;
# Default: 	limit_rate_after 0;
# Context: 	http, server, location, if in location
5.ngx_http_limit_req_module
# ngx_http_limit_req_module模块(0.7.21)用于限制每个已定义键的请求处理速率,特别是来自单个IP地址的请求处理速率。
# 这种限制是使用“漏桶”方法实现的。
`# Example
http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
    ...
    server {
        ...
        location /search/ {
            limit_req zone=one burst=5;
        }
`# 设置共享内存区域和请求的最大突发大小。如果请求速率超过了为区域配置的速率,它们的处理将被延迟,以便按照定义的速率处理请求。
`# 过多的请求会被延迟,直到它们的数量超过最大的突发大小,在这种情况下,请求会因错误而终止。默认情况下,最大突发大小等于零。
limit_req
# Syntax: 	limit_req zone=name [burst=number] [nodelay | delay=number];
# Default: 	—
# Context: 	http, server, location

`# 例如,指令
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
    location /search/ {
        limit_req zone=one burst=5;
    }
# 平均每秒允许不超过一个请求,突发请求不超过5个。
-------------------------------------------------------------------------------------------------------
$binary_remote_addr 	#表示通过remote_addr这个标识来做限制,“binary_”的目的是缩写内存占用量,是限制同一客户端ip地址
zone=one:10m					#表示生成一个大小为10M,名字为one的内存区域,用来存储访问的频次信息
rate=1r/s							#表示允许相同标识的客户端的访问频次,这里限制的是每秒1次,即每秒只处理一个请求

zone=one # 设置使用哪个配置区域来做限制,与上面limit_req_zone 里的name对应
burst=5  # 重点说明一下这个配置,burst爆发的意思,这个配置的意思是设置一个大小为5的缓冲区当有大量请求(爆发)过来时,
				 # 超过了访问频次限制的请求可以先放到这个缓冲区内等待,但是这个等待区里的位置只有5个,超过的请求会直接报503的错误然后返回。
-------------------------------------------------------------------------------------------------------
`# 如果不希望在请求有限的情况下延迟过多的请求,则应该使用参数nodelay:
limit_req zone=one burst=5 nodelay;
# delay参数(1.15.7)指定了延迟过多请求的限制。默认值为零,也就是说,所有多余的请求都会被延迟。
# 如果设置delay参数,会在瞬时提供处理(burst + rate)个请求的能力,请求超过(burst + rate)的时候就会直接返回503,
# 永远不存在请求需要等待的情况。(这里的rate的单位是:r/s)
# 如果没有设置,则所有请求会依次等待排队
-------------------------------------------------------------------------------------------------------
`# 可以有几个limit_req指令。例如,下面的配置将限制来自单个IP地址的请求的处理速率,同时限制虚拟服务器的请求处理速率:
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone $server_name zone=perserver:10m rate=10r/s;
server {
    ...
    limit_req zone=perip burst=5 nodelay;
    limit_req zone=perserver burst=10;
}

# 当且仅当当前级别上没有limit_req指令时,这些指令从上一级继承。
`# 启用试运行模式。在这种模式下,请求处理速率不受限制,但是,在共享内存区域中,过量请求的数量照常计算。
limit_req_dry_run
# Syntax: 	limit_req_dry_run on | off;
# Default: 	limit_req_dry_run off;
# Context: 	http, server, location

`# 为服务器由于速率超过或延迟请求处理而拒绝处理请求的情况设置所需的日志记录级别。
`# 延迟的日志记录级别比拒绝的日志记录级别低1分;例如,如果指定了“limit_req_log_level通知”,则使用info级别记录延迟。
limit_req_log_level
# Syntax: 	limit_req_log_level info | notice | warn | error;
# Default: 	limit_req_log_level error;
# Context: 	http, server, location

`# 设置状态代码以响应被拒绝的请求。
limit_req_status
# Syntax: 	limit_req_status code;
# Default: 	limit_req_status 503;
# Context: 	http, server, location

`# 设置用于保存各种键的状态的共享内存区域的参数。特别是,状态存储当前的过量请求数量。
`# 密钥可以包含文本、变量及其组合。不考虑键值为空的请求。
limit_req_zone
# Syntax: 	limit_req_zone key zone=name:size rate=rate [sync];
# Default: 	—
# Context: 	http
6.ngx_http_limit_conn_module
# ngx_http_limit_conn_module模块用于限制每个已定义键的连接数,特别是来自单个IP地址的连接数。
# 并不是所有的连接都被计算在内。只有当服务器正在处理一个请求并且整个请求头已经被读取时,连接才会被计算。
`# Example
http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    ...
    server {
        ...
        location /download/ {
            limit_conn addr 1;
        }
`# 设置共享内存区域和给定键值允许的最大连接数。当超过此限制时,服务器将返回错误以响应请求。
limit_conn
# Syntax: 	limit_conn zone number;
# Default: 	—
# Context: 	http, server, location

`# 例如,指令
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
    location /download/ {
        limit_conn addr 1;
    }
# 每次只允许一个IP地址有一个连接。
# 在HTTP/2和SPDY中,每个并发请求被视为一个单独的连接。

`# 可能有几个limit_conn指令。例如,下面的配置将限制每个客户机IP到服务器的连接数量,同时限制到虚拟服务器的连接总数:
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server {
    ...
    limit_conn perip 10;
    limit_conn perserver 100;
}
# 当且仅当当前级别上没有limit_conn指令时,这些指令从上一级继承。

`# 设置服务器限制连接数量时所需的日志记录级别。
limit_conn_log_level
# Syntax: 	limit_conn_log_level info | notice | warn | error;
# Default: 	limit_conn_log_level error;
# Context: 	http, server, location

`# 设置状态代码以响应被拒绝的请求。
limit_conn_status
# Syntax: 	limit_conn_status code;
# Default: 	limit_conn_status 503;
# Context: 	http, server, location
7.ngx_http_stub_status_module
# ngx_http_stub_status_module模块提供对基本状态信息的访问。
# 这个模块不是默认构建的,它应该使用——with-http_stub_status_module配置参数来启用。
`# Example
location = /basic_status {
    stub_status;
}

`# 这个配置创建了一个简单的web页面,其中包含基本的状态数据,如下所示:
Active connections: 291 
server accepts handled requests
 16630948 16630948 31070465 
Reading: 6 Writing: 179 Waiting: 106 
# Active connections 当前活动客户端连接的数量,包括正在等待的连接。

# accepts 接受的客户端连接的总数。
# handled 处理的连接总数。
# requests 客户机请求的总数。

# Reading 正在读取请求头的当前连接数。
# Writing 将响应写回客户端的当前连接数。
# Waiting 等待请求的当前空闲客户端连接数。
`# 基本状态信息可以从周围位置访问。
stub_status
# Syntax: 	stub_status;
# Default: 	—
# Context: 	server, location
原文地址:https://www.cnblogs.com/IMSCZ/p/12050470.html