ngx_http_access_module
- ngx_http_access_module模块
实现基于ip的访问控制功能
1、allow address | CIDR | unix: | all;
2、deny address | CIDR | unix: | all;
http,server,location,limit_expect
自上而下检查,一旦匹配,将生效,条件严格的置前
示例:
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
ngx_http_auth_basic_module
- ngx_http_auth_basic_module模块
实现基于用户的访问控制,使用basic机制进行用户认证
1、auth_basic string | off;
2、auth_basic_user_file file;
location /admin/ {
auth_basic "Admin Area";
auth_basic_user_file /etc/nginx/.ngxpasswd;
}
用户口令文件:
1、明文文本:格式name:password:comment
2、加密文本:由htppasswd命令实现
httpd-tools所提供
放在server那里就是整个网站都需要认证,放在location里面就是特定目录需要认证
ngx_http_stub_status_module
- ngx_http_stub_status_module模块
用于输出nginx的基本状态信息
输出信息示例:
Active connections:291
server accepts handled requests
16630948 16630948 31070465 ---3个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections:当前状态,活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已经处理完成的客户端请求的总数
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数
1、stub_status;
示例:
location /status {
stub_status;
allow 172.16.0.0/16;
deny all;
}
ngx_http_log_module
- ngx_http_log_module模块
指定日志格式记录请求
1、log_format name string ...;
string可以使用nginx核心模块及其它模块内嵌的变量
注意:log_format要在http那里定义
2、access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=conditino]];
access_log off;
访问日志文件路径,格式及相关的缓冲的配置
buffer=size
flush=time
示例:
log_format compression '$remote_addr-$remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /spool/logs/nginx-access.log compression buffer=32k; ---buffer在测试环境不要用,因为要放进缓存等一段时间才能看到
3、open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
缓存各日志文件相关的元数据信息
max:缓存的最大文件描述符数量
min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项
inactive:非活动时长
valid:验证缓存中各缓存项是否为活动项的时间间隔
ngx_http_gzip_module
- ngx_http_gzip_module
用gzip方法压缩响应数据,节约带宽
1、gzip on | off;
启用或禁用gzip压缩
2、gzip_comp_level level;
压缩比由低到高:1到9
默认:1
3、gzip_disable regex ...;
匹配到客户端浏览器不执行压缩
4、gzip_min_length length;
启用压缩功能的响应报文大小阈值
5、gzip_http_version 1.0 | 1.1;
设定启用压缩功能时,协议的最小版本
默认:1.1
6、gzip_buffers number size;
支持实现压缩功能时缓冲区数量及每个缓存区的大小
默认:32 4k 或 16 8k
7、gzip_types mime-type ...;
指明仅对哪些类型的资源执行压缩操作;即压缩过滤器
默认包含有text/html,不用显示指定,否则出错
8、gzip_vary on | off;
如果启用压缩,是否在响应报文首部插入“Vary:Accept-Encoding”
9、gzip_proxied off | expired | on-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
nginx充当代理服务器时,对于后端服务器的响应报文,在何种条件下启用压缩功能
off:不启用压缩
expired,no-cache,no-store,private:对后端服务器的响应报文首部Cache-Control值任何一个,启用压缩功能
示例:
gzip on;
gzip_comp_level 6;
gzip_min_length 64;
gzip_proxied any;
gzip_types text/xml text/css application/javascript;
curl -I compressed http://www.a.com ---不加compressed默认不压缩,压缩了的话有提示gzip
ngx_http_ssl_module
ngx_http_ssl_module模块:
1、ssl on | off;
为指定虚拟机启用HTTPS protocol,建议用listen指令代替
2、ssl_certificate file;
当前虚拟主机使用PEM格式的证书文件
3、ssl_certificate_key file;
当前虚拟主机上与其证书匹配的私钥文件
4、ssl_protocols [SSLv2] [SSLv3] [TLSv1.1] [TLSv1.2];支持ssl协议版本,默认为后三个
5、ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
none:通知客户端支持ssl session cache,但实际不支持
builtin[:size]:使用OpenSSL内建缓存,为每worker进程私有
[shared:name:size]:在各worker之间使用一个共享的缓存