avatar

11.Nginx常用模块

Nginx常用模块

第1章 目录索引

1.1 应用场景

可以使用nginx作为简易的文件下载服务器

1.2 参数说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
默认中文目录乱码,添加上解决乱码

1.3 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@web01 /etc/nginx/conf.d]# cat download.conf 
server {
listen 8080;
server_name download.wufei.com;
location / {
root /usr/share/nginx/html/download;
charset utf-8,gbk;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
}
}
[root@web01 /etc/nginx/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 /etc/nginx/conf.d]# systemctl reload nginx

1.4 创建测试数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@web01 /usr/share/nginx/html/download]# ll
total 948
-rw-r--r-- 1 root root 763172 Jun 4 11:07 01.jpg
-rw-r--r-- 1 root root 200027 Jun 4 00:29 02.jpg
-rw-r--r-- 1 root root 0 Jul 31 09:56 10.txt
-rw-r--r-- 1 root root 0 Jul 31 09:56 1.txt
-rw-r--r-- 1 root root 13 Jul 31 12:07 2.txt
-rw-r--r-- 1 root root 0 Jul 31 09:56 3.txt
-rw-r--r-- 1 root root 0 Jul 31 09:56 4.txt
-rw-r--r-- 1 root root 0 Jul 31 09:56 5.txt
-rw-r--r-- 1 root root 0 Jul 31 09:56 6.txt
-rw-r--r-- 1 root root 0 Jul 31 09:56 7.txt
-rw-r--r-- 1 root root 0 Jul 31 09:56 8.txt
-rw-r--r-- 1 root root 0 Jul 31 09:56 9.txt
drwxr-xr-x 2 root root 24 Jul 31 11:50 mdeia
-rw-r--r-- 1 root root 0 Jul 31 11:56 天天.txt

1.5 访问页面

第2章 状态监控

2.1 状态字段解释

1
2
3
4
5
6
7
8
9
10
Active connections # 当前活动的连接数
accepts # 当前的总连接数 TCP
handled # 成功的连接数 TCP
requests # 总的 http 请求数
Reading # 请求
Writing # 响应
Waiting # 等待的请求数,开启了 keepalive
# 注意, 一次 TCP 的连接,可以发起多次 http 的请求, 如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s 没有活动则断开连接

2.2 配置文件

1
2
3
4
5
6
7
[root@web01 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/status.conf 
server {
listen 80;
server_name status.oldboy.com;
stub_status on;
access_log off;
}

2.3 访问测试

1
2
3
4
5
[root@web01 /etc/nginx/conf.d]# curl status.oldboy.com
Active connections: 1
server accepts handled requests
32 32 31
Reading: 0 Writing: 1 Waiting: 0

第3章 访问控制

3.1 基于IP的访问控制

3.1.1 配置语法

1
2
3
4
5
6
7
8
9
#允许配置语法
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

3.1.2 案例一:拒绝windwos访问www域名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@web01 /etc/nginx/conf.d]# cat 01-www.conf 
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/www.access.log main;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
charset utf-8,gbk;
deny 10.0.1.1;
allow all;
}
}
[root@web01 /etc/nginx/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 /etc/nginx/conf.d]# systemctl reload nginx

3.1.3 windows访问测试403

3.1.4 使用curl访问测试ok

1
2
[root@web01 /etc/nginx/conf.d]# curl www.oldboy.com
www

3.1.5 案例二:只允许windows访问,其他全部拒绝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@web01 /etc/nginx/conf.d]# cat 01-www.conf 
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/www.access.log main;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
charset utf-8,gbk;
allow 10.0.1.1;
deny all;
}
}
[root@web01 /etc/nginx/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 /etc/nginx/conf.d]# systemctl reload nginx

3.1.6 windows访问测试ok

3.1.7 curl访问测试403

1
2
3
4
5
6
7
8
[root@web01 /etc/nginx/conf.d]# curl www.oldboy.com
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.16.0</center>
</body>
</html>

3.2 基于用户认证的访问控制

3.2.1 配置语法

1
2
3
4
5
6
7
8
9
#访问提示字符串
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

3.2.2 配置文件

安装httpd-tools,该包中携带了 htpasswd 命令

1
[root@web01 ~]# yum -y install httpd-tools

创建新的密码文件, -c 创建新文件 -b 允许命令行输入密码

1
2
[root@web01 /etc/nginx/conf.d]# htpasswd -b -c /etc/nginx/auth_conf wufei 123456 
Adding password for user wufei

nginx 配置调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@web01 /etc/nginx/conf.d]# cat 01-www.conf 
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/www.access.log main;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
charset utf-8,gbk;
#allow 10.0.1.1;
#deny all;
auth_basic "Auth access Blog Input your Passwd!";
auth_basic_user_file auth_conf;
}
}
[root@web01 /etc/nginx/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 /etc/nginx/conf.d]# systemctl reload nginx3.2.3 访问测试

3.2.3 访问测试

第4章 访问限制

经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个 ip 的连接数,请求数、进行限制。
ngx_http_limit_conn_module 模块可以根据定义的 key 来限制每个键值的连接数,如同一个 IP 来源的连接数。
limit_conn_module 连接频率限制
limit_req_module 请求频率限制

4.1 连接限制

4.1.1 配置语法

1
2
3
4
5
6
7
#模块名 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

4.1.2 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# http 标签段定义连接限制
http{
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
}
# server标签里引用条件
[root@web01 /etc/nginx/conf.d]# cat 01-www.conf
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;

server {
listen 80;
server_name www.oldboy.com;
# 同一时刻只允许一个客户端连接
limit_conn conn_zone 1;

access_log /var/log/nginx/www.access.log main;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
charset utf-8,gbk;
#allow 10.0.1.1;
#deny all;
#auth_basic "Auth access Blog Input your Passwd!";
#auth_basic_user_file auth_conf;
}
}
[root@web01 /etc/nginx/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 /etc/nginx/conf.d]# systemctl reload nginx

4.1.3 访问测试

1
2
[root@web01 ~]# yum install httpd-tools -y
[root@web01 /etc/nginx/conf.d]# ab -n 50 -c 2 http://www.oldboy.com/

4.1.4 查看日志

1
2
[root@web01 ~]# tail -f /var/log/nginx/www.access.log 
10.0.1.7 - - [31/Jul/2019:16:15:55 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"

4.2请求限制

4.2.1 配置语法

1
2
3
4
5
6
7
#模块名 ngx_http_limit_req_module
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
Syntax: limit_conn zone number [burst=number] [nodelay];
Default: —
Context: http, server, location

4.2.2 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@web01 /etc/nginx/conf.d]# cat 01-www.conf 
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;

server {
listen 80;
server_name www.oldboy.com;
# 同一时刻只允许一个客户端连接
#limit_conn conn_zone 1;

limit_req zone=req_zone burst=3 nodelay;

access_log /var/log/nginx/www.access.log main;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
charset utf-8,gbk;
#allow 10.0.1.1;
#deny all;
#auth_basic "Auth access Blog Input your Passwd!";
#auth_basic_user_file auth_conf;
}
}
[root@web01 /etc/nginx/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 /etc/nginx/conf.d]# systemctl reload nginx

4.2.3 访问测试

1
2
[root@web01 ~]# yum install httpd-tools -y
[root@web01 /etc/nginx/conf.d]# ab -n 50 -c 2 http://www.oldboy.com/

4.2.4 查看访问日志

1
2
3
4
5
6
7
8
[root@web01 ~]# tail -f /var/log/nginx/www.access.log 
10.0.1.7 - - [31/Jul/2019:16:23:01 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [31/Jul/2019:16:23:01 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [31/Jul/2019:16:23:01 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [31/Jul/2019:16:23:01 +0800] "GET / HTTP/1.0" 200 4 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [31/Jul/2019:16:23:01 +0800] "GET / HTTP/1.0" 503 197 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [31/Jul/2019:16:23:01 +0800] "GET / HTTP/1.0" 503 197 "-" "ApacheBench/2.3" "-"
10.0.1.7 - - [31/Jul/2019:16:23:01 +0800] "GET / HTTP/1.0" 503 197 "-" "ApacheBench/2.3" "-"
4.2.5 查看错误日志
1
2
3
4
5
6
[root@web01 ~]# tail -5 /var/log/nginx/error.log
2019/07/31 16:23:01 [error] 2808#2808: *191 limiting requests, excess: 3.995 by zone "req_zone", client: 10.0.1.7, server: www.oldboy.com, request: "GET / HTTP/1.0", host: "www.oldboy.com"
2019/07/31 16:23:01 [error] 2808#2808: *192 limiting requests, excess: 3.995 by zone "req_zone", client: 10.0.1.7, server: www.oldboy.com, request: "GET / HTTP/1.0", host: "www.oldboy.com"
2019/07/31 16:23:01 [error] 2808#2808: *193 limiting requests, excess: 3.995 by zone "req_zone", client: 10.0.1.7, server: www.oldboy.com, request: "GET / HTTP/1.0", host: "www.oldboy.com"
2019/07/31 16:23:01 [error] 2808#2808: *194 limiting requests, excess: 3.995 by zone "req_zone", client: 10.0.1.7, server: www.oldboy.com, request: "GET / HTTP/1.0", host: "www.oldboy.com"
2019/07/31 16:23:01 [error] 2808#2808: *195 limiting requests, excess: 3.995 by zone "req_zone", client: 10.0.1.7, server: www.oldboy.com, request: "GET / HTTP/1.0", host: "www.oldboy.com"

4.3 为什么限制请求的效果更好

我们先来回顾一下 http 协议的连接与请求

首先 HTTP 是建立在 TCP 基础之上, 在完成 HTTP 请求需要先建立TCP 三次握手(称为 TCP 连接) ,在连接的基础上在完成 HTTP 的请求。
所以多个 HTTP 请求可以建立在一次 TCP 连接之上, 那么我们对请求的精度限制,当然比对一个连接的限制会更加的有效,因为同一时刻只允许一个 TCP 连接进入, 但是同一时刻多个 HTTP 请求可以通过一个 TCP 连接进入。所以针对 HTTP 的请求限制才是比较优的解决方案。

第5章 location

使用 Nginx Location 可以控制访问网站的路径, 但一个 server 可以有多个 location 配置, 多个 location 的优先级该如何区分

5.1 location语法介绍

1
2
location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
}

5.2 location语法优先级

匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 4
!~ 区分大小写不匹配的正则 5
!~* 不区分大小写不匹配的正则 6
/ 通用匹配,任何请求都会匹配到 7

5.3 配置location匹配规则实战

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@web01 /etc/nginx/conf.d]# cat 03-bbs.conf
server {
listen 80;
server_name bbs.oldboy.com;
access_log /var/log/nginx/bbs.access.log main;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
return 200 "location / \n";
}
location = / {
return 200 "location = \n";
}

location /documents/ {
return 200 "location /documents/ \n";
}
location ^~ /images/ {
return 200 "location ^~ /images/ \n";

}
location ~* \.(gif|jpg|jpeg)$ {
return 200 "location ~* \.(gif|jpg|jpeg) \n";
}
access_log off;
}
[root@web01 /etc/nginx/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 /etc/nginx/conf.d]# systemctl reload nginx

5.4 测试location匹配规则

#精确匹配=/

1
2
[root@web01 /etc/nginx/conf.d]# curl bbs.oldboy.com
location =

#没有满足的请求,所以匹配了/

1
2
[root@web01 /etc/nginx/conf.d]# curl bbs.oldboy.com/oldboy.html
location /

#匹配了/documents

1
2
[root@web01 /etc/nginx/conf.d]# curl bbs.oldboy.com/documents/oldboy.html
location /documents/

#没有满足的条件,匹配/

1
2
[root@web01 /etc/nginx/conf.d]# curl bbs.oldboy.com/oldboy/documents/oldboy.html
location /

#正则匹配了jpg文件名

1
2
[root@web01 /etc/nginx/conf.d]# curl bbs.oldboy.com/oldboy.jpg
location ~* \.(gif|jpg|jpeg)

#~*匹配正则不区分大小写优先于/documents

1
2
[root@web01 /etc/nginx/conf.d]# curl bbs.oldboy.com/documents/oldboy.jpg
location ~* \.(gif|jpg|jpeg)

#^优先匹配于*

1
2
[root@web01 /etc/nginx/conf.d]# curl bbs.oldboy.com/images/oldboy.jpg
location ^~ /images/
文章作者: Wu Fei
文章链接: http://linuxwf.com/2020/04/13/11-Nginx%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 WF's Blog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论