Nginx常用模块介绍
Nginx常用模块
Nginx官方文档:
https://nginx.org/en/docs/
1、目录索引模块
ngx_http_autoindex_module (适合建yum仓库用)
# Example Configuration
location / {
autoindex on;
}
# Directives
Syntax(语法):autoindex on | off;
Default(默认):autoindex off;
Context(环境):http, server, location
如何配置:例如
0 ✓ 10:16:14 root@web01,172.16.1.7:~ # cd /etc/nginx/conf.d
0 ✓ 10:18:20 root@web01,172.16.1.7:/etc/nginx/conf.d # ll
total 4
-rw-r--r-- 1 root root 1072 May 24 23:33 default.conf
0 ✓ 10:18:21 root@web01,172.16.1.7:/etc/nginx/conf.d # vim xxx.conf 创个配置文件
server {
listen 80;
server_name blog.xxx.com;
location / {
autoindex on; # <<----放进去
root /xxx1;
index index.html;
}
}
# ps:nginx默认会去xxx1目录下去找index.html,如果找到了会交给下面index处理。没找到的话走autoindex模块
mkdir /xxx1 && cd /xxx1
cd xxx1
mkdir 1
mkdir 2
mkdir abc
systemctl start nginx ,打开网页:
优化1:文件大小展示,下点东西进xxx1
Syntax:autoindex_exact_size on | off;
Default:autoindex_exact_size on; (把这行放进去)
Context:http, server, location
location / {
autoindex on;
autoindex_exact_size off <<---放进去
root /xxx1;
index index.html;
}
优化2 显示格式
Syntax: autoindex_format html | xml | json | jsonp; html有时显示中文是乱码,通过修改json形式可以看到中文
Default:autoindex_format html;
Context:http, server, location
例如:
server {
listen 80;
server_name blog.xxx.com;
location / {
autoindex on;
autoindex_exact_size off;
autoindex_format json; <<----换成json
root /xxx1;
}
}
优化3 显示本地时间
Syntax:autoindex_localtime on | off;
Default:autoindex_localtime off;
Context:http, server, location
2、状态监控模块
ngx_http_stub_status_module
https://nginx.org/en/docs/http/ngx\_http\_stub\_status\_module.html
# Example Configuration
location = /basic_status {
stub_status;
}
# 例如:
server {
listen 80;
server_name blog.xxx.com;
location / {
autoindex on;
autoindex_exact_size off;
root /xxx1;
}
location = /basic_status { #<<------ 另起一层location
stub_status;
}
}
##### 各参数的解释
Active connections # 当前活动的连接数
accepts # 当前的总连接数 of TCP
handled # 成功的连接数TCP
requests # 通过各终端访问的的http总请求数(每刷新一次就+1)
Reading # 请求
Writing # 响应
Waiting # 等待的请求数,开启了keepalive
# ps: 长格式:一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout 65; # 65s没有活动则断开连接
keepalive_timeout 0; # 类似于关闭长连接
实验:用linux服务器访问nginx页面(web02用curl访问页面)
# 先把配置改一下
server {
listen 80;
server_name 10.0.0.7; # <<-----改成10.0.0.7,因为web02里没有做域名解析
location / {
autoindex on;
autoindex_exact_size off;
root /xxx1;
}
location = /basic_status {
stub_status;
}
}
# 连接
0 ✓ 11:11:15 root@web02,172.16.1.8:~ # curl -s 10.0.0.7/basic_status (-s 显示静态)
Active connections: 3
server accepts handled requests
23 23 38
Reading: 0 Writing: 1 Waiting: 2
0 ✓ 11:12:18 root@web02,172.16.1.8:~ # curl -s 10.0.0.7/basic_status | awk 'NR==3 {print $1}'
24
0 ✓ 11:14:29 root@web02,172.16.1.8:~ # curl -s 10.0.0.7/basic_status | awk 'NR==3 {print $1}'
25
3、访问控制模块
ngx_http_access_module
# Example Configuration
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
# 允许配置语法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
# 拒绝配置语法
Syntax:deny address | CIDR | unix: | all;
Default:—
Context:http, server, location, limit_except
默认是allow all
4、访问认证模块
ngx_http_auth_basic_module
https://nginx.org/en/docs/http/ngx\_http\_auth\_basic\_module.html
# 配置示例格式
location / {
auth_basic "closed site"; (密码输错会显示的信息,比如密码提示)
auth_basic_user_file conf/htpasswd;
}
# 说明
1、配到哪一层,进哪一层就要输密码
2、通常和auth_basic配合使用的工具是htpasswd,该工具来源于httpd-tools包,主要用于生成用户及其密码加密文件
实操举例:
# 安装htpasswd
yum install -y httpd
# 在/etc/nginx下创建auth目录,把密码都放这
root@web01,172.16.1.7:/etc/nginx # mkdir auth
# 执行命令生成秘钥文件
htpasswd -bc /etc/nginx/auth/xxx_auth xxx 123
-b 允许命令输入密码
-c 创建一个新文件,将用户名和密码保存到文件中
0 ✓ 21:00:11 root@web01,172.16.1.7:/etc/nginx/auth # cat xxx_auth
xxx:$apr1$0PjJZR1K$ZYD9kukcrNzMIfTAa9y3x.
# 修改xxx.conf配置文件
0 ✓ 21:08:08 root@web01,172.16.1.7:/etc/nginx/conf.d # vim xxx.conf
server {
listen 80;
server_name blog.xxx.com;
location / {
autoindex on;
autoindex_exact_size off;
root /xxx1;
}
location = /basic_status {
stub_status;
auth_basic "closed site"; # <<--------配在这一层,进监控模块时便需密码
auth_basic_user_file /etc/nginx/auth/xxx_auth;
}
}
# 保存退出 重载Nginx,再打开网页便需输入密码,输入xxx和123可进入
http://blog.xxx.com/basic_status
还没有评论,来说两句吧...