Nginx目录索引
ngx_http_autoindex_module
模块处理以斜杠字符(’/’)结尾的请求,并生成目录列表
当ngx_http_index_module
模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module
模块
配置
Nginx
默认是不允许列出整个目录浏览下载
# autoindex常用参数 autoindex_exact_size off;
# 默认为on, 显示出文件的确切大小,单位是bytes
# 修改为off,显示出文件的大概大小,单位是kB或者MB或者GB
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
# 修改为on, 显示的文件时间为文件的服务器时间
# 默认为off,显示的文件时间为GMT时间
autoindex_localtime on;
# 默认中文目录乱码,添加上解决乱码
charset utf‐8,gbk;
配置示例:
[root@test conf.d]# cat test.conf
server{
listen 80;
server_name www.test1.com;
#charset utf-8,gbk;
location / {
root /code/test1;
index index.html;
}
location /download {
alias /code/download;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
Nginx状态监控
ngx_http_stub_status_module
模块提供对基本状态信息的访问。
默认情况下不构建此模块,应使用‐‐with‐http_stub_status_module
配置参数启用它
配置
Syntax: stub_status;
Default: —
Context: server, location
配置Nginx status
示例
[root@test conf.d]# cat test.conf
server {
listen 80;
server_name www.test1.com;
access_log off;
location /nginx_status {
stub_status;
}
}
server{
listen 80;
server_name www.test1.com;
charset utf-8,gbk;
location / {
root /code/test1;
index index.html;
}
location /download {
alias /code/download;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
location /nginx_status {
stub_status;
}
}
访问浏览器,显示信息
Active connections: 2
server accepts handled requests
32 32 80
Reading: 0 Writing: 1 Waiting: 1
参数信息
Active connections # 当前活动的连接数
accepts # 当前的总连接数TCP
handled # 成功的连接数TCP
requests # 总的http请求数
Reading # 请求
Writing # 响应
Waiting # 等待的请求数,开启了keepalive
# 注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接
Nginx访问控制
基于IP的访问控制 http_access_module
基于用户登陆认证 http_auth_basic_module
nginx基于IP的访问控制
#允许配置语法
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
- 访问控制配置示例,拒绝指定的IP,其他全部允许
server {
listen 80;
server_name www.test1.com;
access_log off;
location /nginx_status {
stub_status;
deny 192.168.100.101;
allow all;
}
}
# 重启nginx
nginx -s stop
nginx
测试示例
[root@192.168.100.101 ~]# curl www.test1.com/nginx_status
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
- 访问控制配置示例, 只允许谁能访问, 其它全部拒绝
server {
listen 80;
server_name www.test1.com;
access_log off;
location /nginx_status {
stub_status;
allow 192.168.100.103/32; #只允许公司内网IP访问。同是只能本机进行查看nginx状态,使用Zabbix监控
allow 127.0.0.1;
deny all;
}
}
# 重启nginx
nginx -s stop
nginx
测试示列
[root@192.168.100.103 conf.d]# curl www.test1.com/nginx_status
Active connections: 1
server accepts handled requests
5 5 5
Reading: 0 Writing: 1 Waiting: 0
Nginx基于用户登陆认证
- 基于用户登陆认证配置语法
# 访问提示字符串
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
- 基于用户登陆认证配置实践
#1.需要安装httpd‐tools,该包中携带了htpasswd命令
[root@test ~]# yum install httpd‐tools
#2.创建新的密码文件
# ‐c创建新文件
# ‐b允许命令行输入密码
# 生成加密的 用户和密码 文件
[root@test nginx]# htpasswd -cb /etc/nginx/auto_conf user01 user01
Adding password for user user01
[root@test nginx]# cat auto_conf
user01:$apr1$ZmG3c/zn$2dryAJsOdx8h4D8T08q.i.
#3.nginx配置调用
location /download {
root /code/download;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "Please input user and passwd";
auth_basic_user_file /etc/nginx/auto_conf;
}
# 访问然后 www.test1.com/download 会让您登录用户和密码
Nginx访问限制
在企业中经常会遇到这种情况,服务器流量异常,负载过大等等。
对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,请求数、进行限制。
ngx_http_limit_conn_module
模块可以根据定义的key来限制每个键值的连接数,如同一个IP来源的连接数。
limit_conn_module
连接频率限制
limit_req_module
请求频率限制
Nginx连接限制配置实战
1、Nginx连接限制配置语法
#模块名ngx_http_limit_conn_module
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
2、Nginx连接限制配置实践
# 在一个公网Nginx中配置
cat /etc/nginx/nginx.conf
http{ # http层,设置
......
# Limit settings
limit_conn_zone $remote_addr zone=conn_zone:10m;
}
cat /etc/nginx/conf.d/test.conf
server{ # server层调用
#连接限制,限制同时最高1个连接
limit_conn conn_zone 1;
}
3、使用ab工具进行压力测试
ab -n 20 -c 1 http://127.0.0.1/index.html
Nginx请求限制配置实战
1、Nginx请求限制配置语法
#模块名ngx_http_limit_req_module
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
Syntax: limit_req zone number [burst=number] [nodelay];
Default: —
Context: http, server, location
2、Nginx请求限制配置实战
# http标签段定义请求限制, rate限制速率,限制一秒钟最多一个IP请求
cat /etc/nginx/nginx.conf
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
}
cat /etc/nginx/conf.d/test.conf
# 也可以写在到server.conf文件里面
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server{
listen 80;
server_name www.test1.com;
# 1r/s只接收一个请求,其余请求拒绝处理并返回错误码给客户端
# limit_req zone=one;
# 请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量, 多余的请求返回503
limit_req zone=one burst=3 nodelay;
location / {
root /code/test1;
index index.html;
}
}
3、使用ab工具进行压力测试
[root@test nginx]# ab -n50 -c2 http://www.test1.com/download
Nginx日志结果:
[root@test nginx]# tailf -10 /var/log/nginx/error.log
202X/XX/XX 23:20:11 [error] 8462#8462: *327 limiting requests, excess: 3.991 by zone "one", client: 192.168.100.103, server: www.test1.com, request: "GET /download HTTP/1.0", host: "www.test1.com"
Nginx请求限制重定向(扩展)
在Nginx请求限制的过程中,我们可以自定义一个返回值,也就是错误页面的状态码。一般默认都是503状态码
1)修改默认返回状态码
server{
listen 80;
server_name www.test1.com;
access_log off;
charset utf-8,gbk;
location /download {
alias /code/download;
limit_req zone=one burst=3 nodelay;
# 修改返回状态码为:478
limit_req_status 478;
}
}
2)重定向页面
echo "错误页面" >>/code/download/error.html
server{
listen 80;
server_name www.test1.com;
access_log off;
charset utf-8,gbk;
location /download {
alias /code/download;
limit_req zone=one burst=3 nodelay;
# 修改返回状态码为:478
limit_req_status 478;
error_page 478 ./error.htmll; # 如果存放的图片话,是需要存放在当前目录下
}
}
Nginx连接限制没有请求限制有效?
http协议的连接与请求,首先HTTP是建立在TCP基础之上,在完成HTTP请求需要先建立TCP三次握手(称为TCP连接),在连接的基础上在完成HTTP的请求。所以多个HTTP请求可以建立在一次TCP连接之上, 那么我们对请求的精度限制,当然比对一个连接的限制会更加的有效,因为同一时刻只允许一个TCP连接进入, 但是同一时刻多个HTTP请求可以通过一个TCP连接进入。所以针对HTTP的请求限制才是比较优的解决方案。
Nginx Location
使用Nginx Location
可以控制访问网站的路径,但一个server
可以有多个location
配置, 多个location
的优先级该如何区分
Location语法示例
![](https://img.haomeiwen.com/i18118090/5e9e09efc021695d.png)
配置网站验证Location优先级
[root@test conf.d]# cat test.conf
server {
listen 80;
server_name www.test1.com;
access_log off;
charset utf-8,gbk;
location / {
default_type /text/html;
return 200 "location /";
}
location =/ {
default_type /text/html;
return 200 "location =/";
}
location ~ / {
default_type /text/html;
return 200 "location ~/";
}
#location ^~ / {
# default_type /text/html;
# return 200 "location ^~";
#}
}
测试Location效果
# 优先级最高符号=
[root@test nginx]# curl www.test1.com
location =/
# 注释掉精确匹配=, 重启Nginx
[root@test nginx]# curl www.test1.com
location ~/
# 注释掉~, 重启Nginx
[root@test nginx]# curl www.test1.com
location /
Locaiton应用场景
# 通用匹配,任何请求都会匹配到
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)$" {
...
}
网友评论