Nginx目录索引
目录索引模块简述
ngx_http_autoindex_module模块处理以斜杠字符(’/’)结尾的请求,并生成目录列表。
当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。
配置
Nginx默认是不允许列出整个目录浏览下载。
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;
默认中文目录乱码,添加上解决乱码。
对下载资源进行限速
Syntax: limit_rate rate;
Default:
limit_rate 0;
Context: http, server, location, if in location
Syntax: limit_rate_after size;
Default:
limit_rate_after 0;
Context: http, server, location, if in location
配置示例:
[root@web01 ~]# vim /etc/nginx/conf.d/module.conf
server {
listen 80;
server_name module.ahui.com;
charset utf-8,gbk; #添加utf-8和gbk编码
localtion / {
root /code;
index index.html index.htm;
}
location /download {
alias /module;
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示例
server {
listen 80;
server_name module.ahui.com;
access_log off;
location /nginx_status {
stub_status;
}
}
server {
listen 80;
server_name module.ahui.com;
charset utf-8,gbk;
localtion / {
root /code;
index index.html index.htm;
}
location /download {
alias /module;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
location /nginx_status {
stub_status;
}
}
打开浏览器访问:http://module.ahui.com/nginx_status(买不起)
Active connections # 当前活动的连接数
accepts # 已接收T的总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
基于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
举例1:
[root@web01 conf.d]# cat download.conf
server {
listen 80;
server_name download.ahui.com;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
allow 116.63.0.10; # 只允许116.63.0.10访问download.ahui.com 只允许公司公网IP地址
deny all; # 其他地址全部拒绝
location / {
root /code/download/;
index index.htm index.html;
}
}
举例2:
[root@web01 conf.d]# cat download.conf
server {
listen 80;
server_name download.ahui.com;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
deny 116.63.0.10; # 只拒绝116.63.0.10 如果有IP恶意攻击我们
allow all; # 其他地址全部可以访问download.ahui.com
location / {
root /code/download/;
index index.htm index.html;
}
}
案例: 允许windows的10.0.0.1访问downalod 其他拒绝
开启41服务器做hosts解析
vim /etc/hosts
[root@backup ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.7 download.ahui.com
使用curl命令测试网站是否正常
[root@backup ~]# curl -I download.ahui.com
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Thu, 16 Jun 2022 04:20:03 GMT
Content-Type: text/html; charset=utf-8,gbk
Connection: keep-alive
修改配置文件:
[root@web01 conf.d]# cat download.conf
server {
listen 80;
server_name download.ahui.com;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
deny all;
location / {
root /code/download/;
index index.htm index.html;
}
}
然后在41再次测试 403拒绝访问
[root@backup ~]# curl -I download.ahui.com
HTTP/1.1 403 Forbidden
Server: nginx/1.22.0
Date: Thu, 16 Jun 2022 04:21:54 GMT
Content-Type: text/html; charset=utf-8,gbk
Content-Length: 153
Connection: keep-alive
基于用户登陆认证配置实践
需要安装httpd-tools,该包中携带了htpasswd命令
[root@web01 ~]# yum install httpd-tools
创建新的密码文件, -c创建新文件 -b允许命令行输入密码
[root@web01 ~]# htpasswd -b -c /etc/nginx/passwd ahui 1
nginx配置调用
[root@web01 conf.d]# cat download.conf
server {
listen 80;
server_name download.ahui.com;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "Hello Linux82"; # 描述信息 auth_basic_user_file /etc/nginx/passwd; # 密码的认证文件
allow 10.0.0.0/24;
deny all;
location / {
root /code/download/;
index index.htm index.html;
}
}
语法检测并重启或者重新加载配置文件
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
通过浏览器输入域名进行检测是否提示用户名和密码
Nginx访问限制
在企业中经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,请求数、进行限制。
ngx_http_limit_conn_module模块可以根据定义的key来限制每个键值的连接数,如同一个IP来源的连接数。
limit_conn_module 连接频率限制
limit_req_module 请求频率限制
Nginx TCP连接限制
连接限制: limit_conn_zone $binary_remote_addr zone=addr:10m
limit_conn_zone 模块信息
$binary_remote_addr # 客户端IP地址
zone=addr # 存储客户端IP地址的名称叫addr 自定义
:10m # 在内存中这个空间的大小
在server区块调用
limit_conn addr 1; # 对addr这个空间里的客户端IP进行限制 最大连接数为1
修改配置文件:
[root@web01 conf.d]# cat download.conf
limit_conn_zone $binary_remote_addr zone=addr:10m; #连接限制模块
server {
listen 80;
server_name download.ahui.com;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
limit_conn addr 1; # 限制连接数为1
location / {
root /code/download/;
index index.htm index.html;
}
}
测试并重启
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
Nginx http请求限制
请求限制:limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
limit_req_zone 模块
$binary_remote_addr # 客户端IP地址
zone=one # 空间名称 one
:10m # 空间大小 10m
rate=1r/s # 每秒发送一个http请求
调用: 在server区块 对one空间内客户端IP地址进行限制 每秒1个请求 burst延时处理5个请求
limit_req zone=one burst=5;
修改配置文件:
[root@web01 conf.d]# cat xq.conf
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; # 对请求数进行限制 每秒1个
server {
listen 80;
server_name www.ahui.com;
limit_req zone=one; # 限制请求
location / {
root /code/download/;
index index.htm index.html;
}
}
测试并重启
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
Nginx访问限制自定义状态码
限制过程中默认给用户返回503 可以指定返回的状态码:
limit_conn_status 501; # 连接限制状态码为501
limit_req_status 502; # 请求限制状态码为502
error_page 502 /1.png; # 在代码目录下的1.png
error_page 502 /error.html; # 在代码目录下 error.html
案例1:请求限制状态码为502
修改配置文件:
[root@web01 conf.d]# cat xq.conf
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; # 对请求数进行限制 每秒1个
server {
listen 80;
server_name www.ahui.com;
limit_req zone=one; # 限制请求
limit_req_status 502; # 请求限制状态码为502
location / {
root /code/download/;
index index.htm index.html;
}
}
效果
案例2:连接限制状态码为501并指定error页面
修改配置文件:
[root@web01 conf.d]# cat download.conf
limit_conn_zone $binary_remote_addr zone=addr:10m; #连接限制模块
server {
listen 80;
server_name download.ahui.com;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
limit_conn addr 1; # 限制连接数为1
limit_conn_status 501; # 连接限制状态码为501
error_page 501 /error.html; # 指定501错误页面
location / {
root /code/download/;
index index.htm index.html;
}
}
编辑错误页面
[root@web01 conf.d]# cat /code/download/error.html
<img style='width:100%;height:100%;' src=https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201901%2F23%2F20190123155309_HdCen.thumb.400_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658042159&t=ac8664b4900d30b4b6f93fdbacccb8d8>
效果
Nginx连接限制没有请求限制有效?
我们先来回顾一下http协议的连接与请求,首先HTTP是建立在TCP基础之上,在完成HTTP请求需要先建立TCP三次握手(称为TCP连接),在连接的基础上在完成HTTP的请求。
所以多个HTTP请求可以建立在一次TCP连接之上, 那么我们对请求的精度限制,当然比对一个连接的限制会更加的有效,因为同一时刻只允许一个TCP连接进入, 但是同一时刻多个HTTP请求可以通过一个TCP连接进入。所以针对HTTP的请求限制才是比较优的解决方案。
如果作为代理服务器,我们需要限制每个用户的请求速度和链接数量,但是,由于一个页面有多个子资源,如果毫无选择的都进行限制,那就会出现很多不必要的麻烦,如:一个页面有40个子资源,那么如果想让一个页面完整的显示,就需要将请求速度和连接数都调整到40,以此达到不阻塞用户正常请求,而这个限制,对服务器性能影响很大,几百用户就能把一台nginx的处理性能拉下来。
所以我们需要制定哪些请求是需要进行限制的,如html页面;哪些是不需要限制的,如css、js、图片等,这样就需要通过配置对应的location进一步细化。
我们不对css、js、gif、png,jpg等进行连接限制,而对除此之外的链接进行限制
Nginx流量限速
limit_rate_after 100m; 初始下载速度100m
limit_rate 2m; 在一段时间后稳定到2m
直接限速
修改配置文件:
[root@web01 conf.d]# cat download.conf
server {
listen 80;
server_name download.ahui.com;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
limit_rate 2m; #下载速度限制为2MB/s
location / {
root /code/download/;
index index.htm index.html;
}
}
速度对比
由快到慢
修改配置文件:
[root@web01 conf.d]# cat download.conf
server {
listen 80;
server_name download.ahui.com;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
limit_rate_after 100m; #初始下载速度100m
limit_rate 2m; #在一段时间后稳定到2m
location / {
root /code/download/;
index index.htm index.html;
}
}
效果
Nginx Location
使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置, 多个location的优先级该如何区分
官网链接:http://nginx.org/en/docs/http/ngx_http_core_module.html#location
Location语法示例
location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
}
Location语法优先级排列
匹配符 | 匹配规则 | 优先级 |
= | 精确匹配 | 1 |
^~ | 以某个字符串开头 | 2 |
~ | 区分大小写的正则匹配 | 3 |
~* | 区分大小写的正则匹配 | 4 |
/ | 通用匹配,任何请求都会匹配到 | 5 |
配置网站验证Location优先级
[root@Nginx conf.d]# cat testserver.conf
server {
listen 80;
server_name www.ahui.com;
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@Nginx conf.d]# curl www.ahui.com
location =/
# 注释掉精确匹配=, 重启Nginx
[root@Nginx ~]# curl www.ahui.com
location ~/
# 注释掉~, 重启Nginx
[root@Nginx ~]# curl www.ahui.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)$" {
...
}