学习b记 · 第二阶段
五、Nginx常用基础模块
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_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 ~]
server {
listen 80;
server_name module.awe.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;
}
}
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
1)访问控制配置示例,拒绝指定的IP,其他全部允许
server {
listen 80;
server_name module.awe.com;
access_log off;
location /nginx_status {
stub_status;
deny 10.0.0.1;
allow all;
}
}
2) 访问控制配置示例, 只允许谁能访问, 其它全部拒绝
server {
listen 80;
server_name module.awe.com;
access_log off;
location /nginx_status {
stub_status;
allow 10.0.0.0/24;
allow 127.0.0.1;
deny all;
}
}
Nginx基于用户登陆认证
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@web01 ~]
[root@web01 ~]
server {
listen 80;
server_name module.awe.com;
access_log off;
location /nginx_status {
stub_status;
auth_basic "Auth access Blog Input your Passwd!";
auth_basic_user_file auth_conf;
}
}
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.awe.com;
access_log off;
location /nginx_status {
stub_status;
}
}
server {
listen 80;
server_name module.awe.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.awe.com/nginx_status
Active connections
accepts
handled
requests
Reading
Writing
Waiting
keepalive_timeout 0;
keepalive_timeout 65;
Nginx访问限制
在企业中经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,请求数、进行限制。
ngx_http_limit_conn_module模块可以根据定义的key来限制每个键值的连接数,如同一个IP来源的连接数。
limit_conn_module 连接频率限制
limit_req_module 请求频率限制
Nginx连接限制配置实战
1)Nginx连接限制配置语法
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
2)Nginx连接限制配置实践
在一个公网Nginx中配置
http{
limit_conn_zone $remote_addr zone=conn_zone:10m;
server{
limit_conn conn_zone 1;
}
}
3)使用ab工具进行压力测试
[root@web01 ~]
[root@web01 ~]
4)nginx日志结果
2018/10/24 18:04:49 [error] 28656
2018/10/24 18:04:49 [error] 28656
2018/10/24 18:04:49 [error] 28656
Nginx请求限制配置实战
朋友公司Nginx配置文件:TP
企业真实案例:
1)Nginx请求限制配置语法
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 {
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
}
server {
listen 80;
server_name module.awe.com;
limit_req zone=req_zone burst=3 nodelay;
location / {
root /code;
index index.html;
}
}
3)使用ab工具进行压力测试
[root@aweedu ~]
[root@aweedu ~]
4)nginx日志结果
2018/10/24 07:38:53 [error] 81020
2018/10/24 07:38:53 [error] 81020
2018/10/24 07:38:53 [error] 81020
5)nginx请求限制重定向(扩展)
在nginx请求限制的过程中,我们可以自定义一个返回值,也就是错误页面的状态码。
默认情况下是503
1)修改默认返回状态码
server {
listen 80;
server_name module.awe.com;
charset utf-8,gbk;
location / {
root /code;
index index.html index.htm;
limit_req zone=req_zone burst=3 nodelay;
limit_req_status 478
}
}
2)页面太丑,重定向页面
server {
listen 80;
server_name module.awe.com;
charset utf-8,gbk;
location / {
root /code;
index index.html index.htm;
limit_req zone=req_zone burst=3 nodelay;
limit_req_status 478
error_page 478 /err.html;
}
}
vim /code/err.html
<img style='width:100%;height:100%;' src=https://www.linuxnc.com/478_page.png>
棒极了~
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 Location
使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置, 多个location的优先级该如何区分
Location语法示例
location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
}
Location语法优先级排列
–
匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 4
/ 通用匹配,任何请求都会匹配到 5
配置网站验证Location优先级
[root@conf.d]
server {
listen 80;
server_name test.awe.com;
default_type text/html;
location = / {
return 200 "configuration A";
}
location / {
return 200 "configuration B";
}
location /documents/ {
return 200 "configuration C";
}
location ^~ /images/ {
return 200 "configuration D";
}
location ~* \.(gif|jpg|jpeg)$ {
return 200 "configuration E";
}
}
测试Location效果
[root@Nginx conf.d]
location =/
[root@Nginx ~]
location ~/
[root@Nginx ~]
location /
Locaiton应用场景
location / {
...
}
location ~ \.php$ {
...
}
location ~ \.jsp$ {
...
}
location ~* .*\.(jpg|gif|png|js|css)$ {
...
}
location ~* \.(jpg|gif|png|js|css)$ {
...
}
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
...
}