一、nginx模块是什么?有什么作用?
Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是Nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
Nginx的模块从结构上分为核心模块、基础模块和第三方模块:
核心模块:HTTP模块、EVENT模块和MAIL模块
基础模块:HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块,
第三方模块:HTTP Upstream Request Hash模块、Notice模块和HTTP Access Key模块。
用户根据自己的需要开发的模块都属于第三方模块。正是有了这么多模块的支撑,Nginx的功能才会如此强大。
二、nginx常用模块
autoindex
- autoindex模块作用
客户端在请求某一个路径下的index.html的时候,服务端无法找到这个index.html则会交给autoindex来处理;需要打开该功能;
#使用nginx配置yum仓库
[root@web02 conf.d]# cat yum.conf
server {
listen 80;
server_name mirrors.zhangzhonghao.com;
autoindex on; #开启autoindex
autoindex_exact_size off; #关闭默认显示文件大小单位
autoindex_localtime on; #开启本地时间
location / {
root /data/;
index index.html index.htm;
}
}
#验证:
[root@web02 conf.d]# ll /data/*.html
ls: cannot access /data/*.html: No such file or directory
访问控制
ngx_http_access_module #做接入控制
ngx_http_auth_basic_module #做接入认证
- 拒绝特定用户访问特定URI,其他允许;
[root@web02 conf.d]# cat yum.conf
server {
listen 80;
server_name mirrors.zhangzhonghao.com;
autoindex on; #开启autoindex
autoindex_exact_size off; #关闭默认显示文件大小单位
autoindex_localtime on; #开启本地时间
root /data/;
location / {
index index.html index.htm;
}
location /zabbix {
deny 10.0.0.252;
allow all;
}
}
#验证
- 在访问/nginx目录的时候需要进行认证(ngx_http_auth_basic_module)
yum install -y httpd-tools #安装httpd的工具包,用于加密密码
htpasswd -b -c /etc/nginx/auth_conf zhangzhonghao 123456 #设置用户名密码
[root@web02 conf.d]# cat yum.conf
server {
listen 80;
server_name mirrors.zhangzhonghao.com;
autoindex on; #开启autoindex
autoindex_exact_size off; #关闭默认显示文件大小单位
autoindex_localtime on; #开启本地时间
root /data/;
location / {
index index.html index.htm;
}
location /zabbix {
deny 10.0.0.252;
allow all;
}
location /nginx {
auth_basic "closed site";
auth_basic_user_file /etc/nginx/auth_conf;
}
}
#验证
nginx限流
为什么要限速、限流?
限流、限制连接数:在大流量大并发的情况下,服务器处理能力不够则会造成无法响应客户端;
限下载速率: 防止用户同时进行下载占用带宽过多;
如何实现
- 限制单位时间内所产生的http连接数
[root@web02 conf.d]# cat qos.conf
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
server {
listen 80;
server_name qos.zhangzhonghao.com;
limit_req zone=perip burst=5 nodelay;
location / {
root /qos/;
index index.html;
}
}
#验证
- 限制客户端同一时刻的并发连接数
http://qos.zhangzhonghao.com/bigdata
[root@web02 qos]# cat /etc/nginx/conf.d/qos.conf
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name qos.zhangzhonghao.com;
limit_conn addr 1;
location / {
root /qos/;
index index.html;
}
}
#验证,同时使用两个链接去下载bigdata这个文件;
#捕捉将503这个错误页面重定向到指定页面;
[root@web02 /]# cat /etc/nginx/conf.d/qos.conf
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name qos.zhangzhonghao.com;
limit_conn addr 1;
location / {
root /qos/;
index index.html;
}
error_page 503 @error_page;
location @error_page {
return 200 'Please recharge RMB';
default_type text/html;
}
}
#验证
- 限制下载速率
server {
listen 80;
server_name qos.zhangzhonghao.com;
limit_rate_after 300m;
limit_rate 50k;
location / {
root /qos/;
index index.html;
}
}
limit_rate_after 300m; 下载前300m不限速
limit_rate 50k; 300m以后限速50K
#验证
- 限制web请求数1秒1个,触发值为5,限制用户仅能够同时只能下载一个文件;当文件超过100M,限制下载速率为500K。如果同时下载超过两个文件,则返回"请充值会员"
nginx状态监控
- 模块Module ngx_http_stub_status_module
配置:
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name qos.zhangzhonghao.com;
limit_conn addr 1;
location / {
root /qos/;
index index.html;
}
error_page 503 @error_page;
location @error_page {
return 200 'Please recharge RMB';
default_type text/html;
}
location /basic_status {
stub_status;
}
}
#验证
参数:
总结
nginx通过模块的方式去实现相关特定的功能,对于使用者来说,需要什么样的功能使用什么样的模块;详细可参考nginx.org官方文档;