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_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] 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: 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]
|
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] 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 handled requests Reading Writing Waiting
keepalive_timeout 0; keepalive_timeout 65;
|
2.2 配置文件
1 2 3 4 5 6 7
| [root@web01 /etc/nginx/conf.d] 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] 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] 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: 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]
|
3.1.3 windows访问测试403

3.1.4 使用curl访问测试ok
1 2
| [root@web01 /etc/nginx/conf.d] 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] 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: 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]
|
3.1.6 windows访问测试ok

3.1.7 curl访问测试403
1 2 3 4 5 6 7 8
| [root@web01 /etc/nginx/conf.d] <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 命令
创建新的密码文件, -c 创建新文件 -b 允许命令行输入密码
1 2
| [root@web01 /etc/nginx/conf.d] 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] 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; auth_basic "Auth access Blog Input your Passwd!"; auth_basic_user_file auth_conf; } } [root@web01 /etc/nginx/conf.d] 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]
|
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
| 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{ limit_conn_zone $binary_remote_addr zone=conn_zone:10m; }
[root@web01 /etc/nginx/conf.d] 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; } } [root@web01 /etc/nginx/conf.d] 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]
|
4.1.3 访问测试
1 2
| [root@web01 ~] [root@web01 /etc/nginx/conf.d]
|
4.1.4 查看日志
1 2
| [root@web01 ~] 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
| 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] limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
server { listen 80; server_name www.oldboy.com;
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; } } [root@web01 /etc/nginx/conf.d] 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]
|
4.2.3 访问测试
1 2
| [root@web01 ~] [root@web01 /etc/nginx/conf.d]
|
4.2.4 查看访问日志
1 2 3 4 5 6 7 8
| [root@web01 ~] 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 ~] 2019/07/31 16:23:01 [error] 2808 2019/07/31 16:23:01 [error] 2808 2019/07/31 16:23:01 [error] 2808 2019/07/31 16:23:01 [error] 2808 2019/07/31 16:23:01 [error] 2808
|
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] 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: 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]
|
5.4 测试location匹配规则
#精确匹配=/
1 2
| [root@web01 /etc/nginx/conf.d] location =
|
#没有满足的请求,所以匹配了/
1 2
| [root@web01 /etc/nginx/conf.d] location /
|
#匹配了/documents
1 2
| [root@web01 /etc/nginx/conf.d] location /documents/
|
#没有满足的条件,匹配/
1 2
| [root@web01 /etc/nginx/conf.d] location /
|
#正则匹配了jpg文件名
1 2
| [root@web01 /etc/nginx/conf.d] location ~* \.(gif|jpg|jpeg)
|
#~*匹配正则不区分大小写优先于/documents
1 2
| [root@web01 /etc/nginx/conf.d] location ~* \.(gif|jpg|jpeg)
|
#^优先匹配于*
1 2
| [root@web01 /etc/nginx/conf.d] location ^~ /images/
|