目录索引模块
ngx_http_autoindex_module 模块处理以斜杠字符('/')结尾的请求,并生成目录列表。 当 ngx_http_index_module 模块找不到索引文件时,通常会将请求传递给 ngx_http_autoindex_module 模块。
Syntax: autoindex on | off; ###目录索引模块
Default: autoindex off; ### 默认开启
Context: http, server, location ### autoindex能写在这些层
server{
## 监听端口
listen 80;
## 域名(ip,localhost,_,域名)
server_name 10.0.0.7;
## 站点目录(代码存放目录)
root /game;
## 密码访问提示
auth_basic "one two";
## 密码访问的密码所在路径
auth_basic_user_file /etc/nginx/auth/yjt_auth;
## URI
location /{
##目录索引模块 开启;
autoindex on;
## 显示带单位的大小
autoindex_exact_size off;
## 显示本地时间
autoindex_localtime on;
## 允许Ip10.0.0.8访问
allow 10.0.0.8;
## 禁止所有Ip访问
deny all;
}
## 查看状态页
location = /yyy {
stub_status;
}
}
windows启用telnet命令
控制面板
Nginx状态模块
[root@web01 conf.d]# cat yjt.conf
server{
listen 80;
server_name 10.0.0.7;
root /game;
location /{
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
location = /yyy {
stub_status;
}
}
Nginx访问控制模板
基于用户密码(auth_basic)
## 安装htpasswd命令
[root@web01 conf.d]# yum install -y httpd
## 创建存放认证文件目录
[root@web01 conf.d]# mkdir /etc/nginx/auth
## 创建认证文件
[root@web01 conf.d]# htpasswd -b -c /etc/nginx/auth/yjt_auth yjt 123
Adding password for user yjt
## 查看认证文件内容
[root@web01 conf.d]# cat /etc/nginx/auth/yjt_auth
yjt:$apr1$41v41SAo$j0z4GAi1Wo6OhrKI.EldP1
## 修改nginx配置文件,添加认证
[root@web01 conf.d]# cat yjt.conf
server{
listen 80;
server_name 10.0.0.7;
root /game;
auth_basic "one two";
auth_basic_user_file /etc/nginx/auth/yjt_auth;
location /{
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
location = /yyy {
stub_status;
}
}
-b:允许命令行中输入密码
-c:创建一个新文件,将用户名和密码保存到指定文件中
[root@web01 conf.d]# htpasswd -b /etc/nginx/auth/yjt_auth yyy 456
Adding password for user yyy
[root@web01 conf.d]# cat /etc/nginx/auth/yjt_auth
yjt:$apr1$41v41SAo$j0z4GAi1Wo6OhrKI.EldP1
yyy:$apr1$UeJUdj/C$MpM5KPQwrQBYZpN9obKUq/
######可以设置多个账号密码,任取其中一个就可以登录
####配置文件中的 nginx默认路径:/etc/nginx
原本(auth_basic_user_file /etc/nginx/auth/yjt_auth)
基于IP访问控制(access)
[root@web01 ~]# !vim (上一次vim编辑)
server{
listen 80;
server_name 10.0.0.7;
root /game;
auth_basic "one two";
auth_basic_user_file /etc/nginx/auth/yjt_auth;
location /{
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.8;
deny all;
}
location = /yyy {
stub_status;
}
}
## 注意:默认nginx是allow all;如果只允许某一个IP需要配合deny all使用,deny all;要放到最下面,因为系统读取配置文件是从上到下,先后顺序很重要
## 此时IP为10.0.0.8的虚拟机:curl http://用户名:密码@10.0.0.7 便可以访问到10.0.0.7
访问频率限制
连接频率限制(limit_conn)
http{
limit_conn_zone $remote_addr zone=自己随意起一个内存空间的名字:10m;
server{
limit_conn 上面自己的取名 1;
}
}
conn_zone:内存空间的名字
1:连接次数
请求频率限制(limit_req)
# http标签段定义请求限制, rate限制速率,限制一秒钟最多一个IP请求
http{
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
}
server{
listen 80;
server_name module.oldboy.com;
# 1r/s只接收一个请求,其余请求拒绝处理并返回错误码给客户端
#limit_req zone=req_zone;
# 请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量, 多余的请求返回503
limit_req zone=req_zone burst=3 nodelay;
location / {
root /code;
index index.html;
}
}
## 请求频率限制错误页面优化
[root@web01 test]# cat /etc/nginx/conf.d/xxx.conf
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
server{
listen 80;
server_name _;
auth_basic "password is 123";
auth_basic_user_file auth/zls_auth;
limit_req zone=req_zone burst=3 nodelay;
limit_req_status 508; #(400 - 599之间)
error_page 508 /508.html;
location /{
root /test;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
location /yyy{
stub_status;
}
}
location优先级
匹配符 | 匹配规则 | 优先级 |
= | 精确匹配 | 1 |
^~ | 以某个字符串开头 | 2 |
~ | 区分大小写的正则匹配 | 3 |
~* | 不区分大小写的正则匹配 | 4 |
!~ | 区分大小写不匹配的正则 | 5 |
!~* | 不区分大小写不匹配的正则 | 6 |
/ | 通用匹配,任何请求都会匹配到 | 7 |
应用场景
# 通用匹配,任何请求都会匹配到
location / {
...
}
# 严格区分大小写,匹配以.php结尾的都走这个location
location ~ \.php$ {
...
}
# 严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
...
}
# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
location ~* .*\.(jpg|gif|png|js|css)$ {
....
}
location ~* \.(jpg|gif|png|js|css)$ {
...
}
# 不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
...
}