Nginx常用模块
Nginx目录索引
目录索引模块简述
ngx_http_autoindex_module模块处理以斜杠字符(\'/\')结尾的请求,并生成目录列表。
当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。
配置
# 打包原始index.html
gzip /test/index.html
# 写入配置文件
vim /etc/nginx/conf.d/xxx.conf
server{
# 监听端口
listen 80;
# 域名(IP localhost_域名)
server_name niubi2.com;
# url
location / {
# 站点目录
root /test;
index index.html;
# 目录索引文件
autoindex on;
}
}
# 格式
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
# autoindex常用参数
autoindex_exact_size off;
默认为on, 显示出文件的确切大小,单位是bytes。
修改为off,显示出文件的大概大小,单位是kB或者MB或者GB。
autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
修改为on, 显示的文件时间为文件的服务器时间。
charset utf-8,gbk;
默认中文目录乱码,添加上解决乱码。
显示文件大小模块
server{
# 监听端口
listen 80;
# 域名(IP localhost_域名)
server_name niubi2.com;
# url
location / {
# 站点目录
root /test;
index index.html;
# 目录索引文件
autoindex on;
# 显示出文件的大概大小
autoindex_exact_size off;
# 显示出文件的准确大小
autoindex_exact_size on;
}
}
# autoindex_exact_size
默认为on, 显示出文件的确切大小,单位是bytes。
修改为off,显示出文件的大概大小,单位是kB或者MB或者GB。
- 文件准确大小
- 文件大概大小
页面展示模块
server{
# 监听端口
listen 80;
# 域名(IP localhost_域名)
server_name niubi2.com;
# url
location / {
# 站点目录
root /test;
index index.html;
# 目录索引文件
autoindex on;
# 显示出文件的大概大小
autoindex_exact_size off;
# 页面展示格式模块 用|分隔
autoindex_format jsonp | json | xml | html;
}
}
# 模式
html(默认)
jsonp
json
xml
- html模式展示
- jsonp模式展示
- json模式展示
- xml模式展示
显示文件本地时间
server{
# 监听端口
listen 80;
# 域名(IP localhost_域名)
server_name niubi2.com;
# url
location / {
# 站点目录
root /test;
index index.html;
# 目录索引文件
autoindex on;
# 显示出文件的大概大小
autoindex_exact_size off;
# 显示本地时间
autoindex_localtime on;
}
}
- 未设置本地时间展示
- 设置本地时间展示
Nginx状态监控
server{
listen 80;
server_name niubi2.com;
location / {
root /test;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
# nginx状态监控
location /ljy {
stub_status;
}
}
# 重启服务
systemctl restart nginx
# 浏览niubi.com/ljy
Active connections # 当前活动的连接数
accepts # 当前的总连接数TCP
handled # 成功的连接数TCP
requests # 总的http请求数
Reading # 请求
Writing # 响应
Waiting # 等待的请求数,开启了 keepalive
# 注意, 一次TCP的连接,可以发起多次http的请求,如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接
Nginx访问控制
server{
listen 80;
server_name niubi2.com;
location / {
root /test;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
# nginx状态监控
location /ljy {
stub_status;
# 允许 10.0.0.102访问
allow 10.0.0.102;
# 拒接所有
deny all;
}
}
# 切换到10.0.0.102
# 写入域名解析
vim /etc/hosts
10.0.0.101 niubi2.com
# 访问niubi2.com/ljy
[root@kehu01 ~]# curl niubi2.com/ljy
Active connections: 1
server accepts handled requests
5 5 5
Reading: 0 Writing: 1 Waiting: 0
# 其他用户访问
[root@web01 nginx]# curl niubi2.com/ljy
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
Nginx基于用户密码访问
# 安装http命令
yum install -y httpd
# 创建存放认证文件的目录
mkdir /etc/nginx/auth
# 创建密码文字文件
[root@fuwu conf.d]# htpasswd -b -c /etc/nginx/auth/ljy_auth ljy 111
Adding password for user ljy
# 查看文件内容
[root@fuwu conf.d]# cat /etc/nginx/auth/ljy_auth
ljy:$apr1$wi4QJ1c4$FOFlDgGe1GlGZKrQHsJhW0
# 编辑配置文件
server{
listen 80;
server_name niubi2.com;
location / {
root /test;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic input username and passwd;
auth_basic_user_file /etc/nginx/auth/ljy_auth;
}
location /ljy {
stub_status;
allow 10.0.0.102;
deny all;
}
}
# 重启nginx
systemctl restart nginx
# 浏览器访问
niubi2.com
- 填写内容展示
- 用户密码访问
在使用xshell时vim使用小键盘数字(拓展)
每个虚拟机用户都有独立的属性(切换用户就需要重新设置)