一、Nginx目录索引
ngx_http_autoindex_module 模块处理以斜杠字符(‘/’)结尾的请求,并生成目录列表。 当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给模块。
1. 指令
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
Syntax: autoindex_exact_size on | off;
Default: autoindex_exact_size on;
Context: http, server, location
Syntax: autoindex_localtime on | off;
Default: autoindex_localtime off;
Context: http, server, location
2. 实操
[root@study ~]
server {
listen 80;
server_name study.com;
charset utf-8,gbk;
location / {
root /module;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
[root@study ~]
[root@study ~]
[root@study ~]
3. 效果显示
二、Nginx状态监控
ngx_http_stub_status_module 模块提供对应基本状态信息的访问。 默认情况下不构建此模块,使用–with-http_stub_status_module 配置参数启用它。
1. 指令
Syntax: stub_status;
Default: —
Context: server, location
2. 实操
[root@study ~]
server {
listen 80;
server_name ma.study.com;
charset utf-8,gbk;
location / {
root /module;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
stub_status;
}
}
3. 显示结果 4. 参数解释
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
Active connections
accepts
handled
requests
Reading
Writing
Waiting
keepalive_timeout 0;
keepalive_timeout 65;
三、Nginx访问控制
ngx_http_access_module 模块允许限制对某些客户端地址的访问。
1. 指令
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
2. 实操
[root@study ~]
server {
listen 80;
server_name module.study.com;
location /nginx_status {
stub_status;
deny 10.0.0.1/32;
allow all;
}
}
3. 基于来源的IP地址做限制
location /nginx_status {
stub_status;
deny 10.0.0.1/32;
allow all;
}
location /nginx_status {
stub_status;
allow 10.0.0.1/32;
deny all;
}
location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}
四、Nginx资源限制
ngx_http_auth_basic_module 模块允许使用HTTP基本身份验证,验证用户名和密码来限制对资源的访问。
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. 实操
[root@study ~]
[root@study ~]
[root@study ~]
Adding password for user study
[root@study ~]
study:$apr1$vBXH4VUp$FZ5gurt4yRodqCdxiXwRN/
[root@study ~]
server {
listen 80;
server_name test1.study.com;
location /download {
root /module;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "Please Passwrod!!!";
auth_basic_user_file /etc/nginx/auth_conf;
}
}
[root@study ~]
[root@study ~]
3. 最终结果
五、Nginx访问限制
经常会遇到这种情况,服务器流量异常,负载过大等等,对于大流量恶意的攻击访问,会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,请求数,进行限制; ngx_http_limit_conn_module 模块用于限制定义key的连接数,特别是来自单个IP地址的连接数。 并非使用连接都被计算在内,仅当连接已经读取了整个请求时才计算连接。
1. 指令
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
2. 实操
[root@study ~]
http {
limit_conn_zone $binary_remote_addr zone=addr:10m;
}
server {
listen 80;
server_name test1.study.com;
limit_conn conn_zone 1;
location / {
root /code;
index index.html;
}
}
ngx_http_limit_req_module 模块用于限制定义key的请求的处理速率,特别单一的IP地址的请求的处理速率
1. 指令
Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default: —
Context: http
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default: —
Context: http, server, location
2. 实操
[root@study conf.d]
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
server {
listen 80;
server_name test1.study.com;
limit_req zone=req_zone burst=5 nodelay;
limit_req_status 412;
location / {
root /code/test1;
index index.html;
}
}
[root@study conf.d]
六、Nginx location
使用nginx location可以控制访问网站的路径,但一个server可以有多个location配置,多个location的优先级改如何区分?
1. Location 语法示例
location [=|^~|~|~*|!~|~!*|/] /uri/ {...
}
2. location 语法优先级排序
|