nginx一些常用模块
模块很多,学不完的,瞅一眼,有个基本印象即可
1.目录索引模块
# ngx_http_autoindex_module
ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求,并生成目录列表。
当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。
1)语法
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
2)配置
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
autoindex on;
}
}
3)访问网站正常,加download跳转目录页面
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
}
}
#创建站点目录
[root@web01 ~]# mkdir /code/autoindex/download -p
[root@web01 ~]# echo "测试autoindex模块" > /code/autoindex/index.html
#访问
http://www.autoindex.com/ 为主站
http://www.autoindex.com/download/ 为下载文件的目录
4)常用优化参数
#显示文件字节大小,默认是显示字节大小,配置为off之后,显示具体大小 M/G/K
Syntax: autoindex_exact_size on | off;
Default: autoindex_exact_size on;
Context: http, server, location
#显示文件的修改的具体时间,默认显示的时间与真实时间相差8小时,所以配置 on
Syntax: autoindex_localtime on | off;
Default: autoindex_localtime off;
Context: http, server, location
5)完整配置
[root@web01 ~]# cat /etc/nginx/conf.d/www.autoindex.com.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
2.Nginx访问控制模块
#ngx_http_access_module
1)语法
#允许访问的语法
Syntax: allow address | all;
Default: —
Context: http, server, location, limit_except
#拒绝访问的语法
Syntax: deny address | all;
Default: —
Context: http, server, location, limit_except
#如果配置允许,则也要配置拒绝;配置拒绝可以单独配置
2)配置访问控制示例
1>拒绝指定的IP,其他全部允许
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
deny 10.0.0.1;
allow all;
}
}
2>只允许指定IP能访问, 其它全部拒绝
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
#如果使用all,一定放在最后面
deny all;
}
}
3>只允许10.0.0.1访问,拒绝该网段其他IP
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
#如果使用all,一定放在最后面
deny 10.0.0.0/24;
}
}
3.Nginx访问认证模块
# ngx_http_auth_basic_module
1)语法
#开启的登录认证,没有卵用
Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except
#指定登录用的用户名密码文件
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
2)创建密码文件
#创建密码文件需要用到 htpasswd
[root@web01 ~]# htpasswd -c /etc/nginx/auth_basic lhd
New password:
Re-type new password:
Adding password for user lhd
#添加一个登录用户
[root@web01 ~]# htpasswd /etc/nginx/auth_basic egon
New password:
Re-type new password:
Adding password for user egon
#密码文件内容
[root@web01 ~]# cat /etc/nginx/auth_basic
lhd:$apr1$A7d4BWYe$HzlIA7pjdMHBDJPuLBkvd/
egon:$apr1$psp0M3A5$601t7Am1BG3uINvuBVbFV0
3)配置访问登录
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
access_log /var/log/nginx/www.autoindex.com.log main;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "性感荷官在线发牌!!!";
auth_basic_user_file /etc/nginx/auth_basic;
}
}
4.Nginx状态监控模块
# ngx_http_stub_status_module
ngx_http_stub_status_module模块提供对nginx基本状态信息的访问。
默认情况下不构建此模块,应使用--with-http_stub_status_module配置参数启用它
1)语法
Syntax: stub_status;
Default: —
Context: server, location
2)配置
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
access_log /var/log/nginx/www.autoindex.com.log main;
location / {
root /code/autoindex;
index index.html;
}
location /download {
root /code/autoindex;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "性感荷官在线发牌!!!";
auth_basic_user_file /etc/nginx/auth_basic;
}
location = /basic_status {
stub_status;
}
}
3)访问
#访问 http://www.autoindex.com/basic_status
#nginx七种状态
Active connections: 2
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 1
Active connections #活跃的连接数
accepts #TCP连接总数
handled #成功的TCP连接数
requests #成功的请求数
Reading #读取的请求头
Writing #响应
Waiting #等待的请求数,开启了keepalive
# 注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接
5.连接限制模块
# ngx_http_limit_conn_module
1)语法
#设置限制的空间
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
limit_conn_zone #设置空间的模块
key #指定空间存储的内容
zone #指定空间
=name #空间名字
:size; #空间的大小
#调用限制的空间
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
limit_conn #调用空间的模块
zone #空间的名字
number; #指定可以同时连接的次数
2)配置
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf
limit_conn_zone $remote_addr zone=conn_zone:10m; #设置一个存储ip地址,空间名字为conn_zone,空间大小为10M的空间
server {
listen 80;
server_name www.autoindex.com;
charset utf8;;
limit_conn conn_zone 1; #调用conn_zone空间,限制每个ip同时只能连接一次
location / {
root /code/autoindex;
index index.html;
}
}
6.请求限制模块
1)语法
#设置空间的语法
Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default: —
Context: http
limit_req_zone #设置空间的模块
key #空间存储的内容
zone #指定空间
=name #空间的名字
:size #空间的大小
rate=rate [sync]; #读写速率
#调用的语法
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default: —
Context: http, server, location
limit_req #调用控件模块
zone=name #指定空间=空间的名字
[burst=number] #允许多请求几次
[nodelay | delay=number]; #延时
2)配置
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf
limit_conn_zone $remote_addr zone=conn_zone:10m;
limit_req_zone $remote_addr zone=req_zone:10m rate=1r/s; #设置一个储存ip地址,储存大小为10m,空间名字req_zone,一秒只能请求一次的空间。
server {
listen 80;
server_name www.autoindex.com;
charset utf8;
limit_conn conn_zone 1;
limit_req zone=req_zone; #调用空间名字是req_zone的空间
#limit_req zone=req_zone burst=5 nodelay; #调用空间名字是req_zone的空间,最大限度可以同时访问五次,没有延迟
location / {
root /code/autoindex;
index index.html;
}
}
3)测试
[root@web01 ~]# ab -n 20000 -c 20 http://www.autoindex.com/index.html
-n #请求的次数
-c #一次请求并发的次数