Nginx模块

Nginx模块
[TOC]
官网示例
https://nginx.org/(先去找官网)--->documentation(右侧的文档)--->Modules reference(往下翻,模块参考)

环境准备
# 为了我们后面的实验,我们需要准备一下我们的环境[root@web01 conf.d]#pwd/etc/nginx/conf.d# 来到子配置目录[root@web01 conf.d]#lltotal 4-rw-r--r-- 1 root root 133 Feb 4 10:22 xbw.conf# 重点,我们这里只有一个子配置文件[root@web01 conf.d]#cat xbw.confserver { listen 80; server_name xbw.kpyun.com;# 一个域名 location / { root /code/xbw; index index.html index.htm; }}[root@web01 conf.d]#tree /code/code├── index.html(默认内容hhhh)├── xbw├── ├── index.html(小霸王主页)├── zh│ ├── index.html(植物大战僵尸主页) # 我把zh的子配置文件删除了[root@web01 conf.d]#curl 127.0.0.1[root@web01 conf.d]#curl localhost[root@web01 conf.d]#curl xbw.kpyun.com'以上三个拉取的主页都是小霸王的主页???'提问:为什么不是/code── index.html(默认内容hhhh)# 因为我们只配置了一个子配置文件,这个子配置文件监控的目录是/code/xbw,那自然是显示小霸王的主页了,我们再来一个子配置文件就能解决问题了[root@web01 conf.d]#vim index.confserver{ listen 80 default_server; # 同为80端口,手动设置它为默认server块 server_name _; # _,它只是一个普通的无效域名;并不会让他成为默认的server 'server_name _; 你可以理解为“兜底”,但必须配合 default_server 才有效' root /code; index index.html index.htm; # 空格隔开 # 这里两个模块不是必须放在location模块中的 # 只是这样写不规范!!!}[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful# 检测一下语法[root@web01 conf.d]#systemctl restart nginx---------------------------当你访问 http://localhost(或直接用 IP 访问),Nginx 会:1)查找 localhost 对应的 server_name2)如果找不到完全匹配的 server 块3)就会使用第一个 server 块(即默认 server)ll /etc/nginx/conf.d/-rw-r--r-- 1 root root 317 Feb 9 14:26 php.conf-rw-r--r-- 1 root root 132 Feb 9 13:44 test.conf-rw-r--r-- 1 root root 325 Feb 6 19:21 wp.conf-rw-r--r-- 1 root root 318 Feb 7 04:13 zh.conf-rw-r--r-- 1 root root 116 Feb 9 14:31 zz.conf# 这个子配置的顺序很重要!'比如把 index.conf 改成 00-index.conf,这样在字母排序中它会最先被加载'# 或者声明一下listen 80 default_server;# 可以让他成为默认的server块---------------------------[root@web01 conf.d]#curl 127.0.0.1<h1>hhhh<h1>[root@web01 conf.d]#curl localhost<h1>hhhh<h1># 这次他们访问的就是/code── index.html(默认内容hhhh)[root@web01 conf.d]#vim index.confserver{ listen 80 default_server; server_name _;
location / { root /code; index index.html index.htm; }}# 我们还是规范一下自己的子配置文件吧!# -t检测,重启服务索引模块
[root@web01 conf.d]#cp index.conf test.conf[root@web01 conf.d]#vim test.confserver{ listen 80; server_name download.kpyun.com; # 这里域名又换了 location / { autoindex on; # 通常在location块中 # 开启索引,以列表的形式显示出来 root /code; # index index.html index.htm; '不能要这一行,同时也需要把/code下的index.html主页给删除了才行' }}# -t测试,在Windows中映射主机名,修改hosts表10.0.0.7 download.kpyun.com浏览器测试访问http://download.kpyun.com/
# 这里访问的是/code── index.html(默认内容hhhh)# 而我们想要它以列表的形式显示出来# 所以需要把默认的主页删除了才行!'子配置文件中也不能有index index.html;'[root@web01 conf.d]#rm -rf /code/index.html# 我们再刷新一下浏览器试一试!
'但是这样子看起来是不是太单调了一点,我们给它上传一点东西给它'[root@web01 code]# cd /code# 先切换到代码目录[root@web01 code]#lltotal 0drwxr-xr-x 6 root root 122 Feb 3 15:13 xbwdrwxr-xr-x 3 root root 52 Feb 3 16:03 zh# 原来目录里面的东西[root@web01 code]#rz[root@web01 code]#rz -Erz waiting to receive.[root@web01 code]#lltotal 34048-rw-r--r-- 1 root root 29718491 Jan 10 14:07 typora-theme-phycat.zipdrwxr-xr-x 6 root root 122 Feb 3 15:13 xbwdrwxr-xr-x 3 root root 52 Feb 3 16:03 zh-rw-r--r-- 1 root root 5141652 Dec 26 01:34 面试题.rar# 上传了一个主题,一个面试题
# 于是我们回去修改了一下自己的子配置文件[root@web01 code]#vim /etc/nginx/conf.d/test.confserver{ listen 80; server_name download.kpyun.com;
location / { autoindex on; root /code; charset utf-8; # 指定utf-8字符集 '这个字符集,推荐放在http{}模块;这样的话,对全局生效' autoindex_localtime on; # 开启本地时间 # 以当前本地时间为主,默认时间不对 autoindex_exact_size off; # 显示文件大小 M G # 默认以字节显示,我们把它关闭了 # Context: http, server, location # 这是以上选项可以包含的模块,可以去官网去详细查看 }}
[root@web01 code]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 code]#systemctl restart nginx# 这次就都把他们改回来了

reload VS restart
# 我们在重启Nginx服务的时候,尽量使用reload'平滑重载配置文件(reload)不中断服务,用户无感知''而不是“重启”整个 Nginx 服务(restart)'kill -1 == reload(重载配置)| 操作 | 是否中断服务 | 说明 |
|---|---|---|
| Reload(重载) | ❌ 不中断 | 只重新加载配置,旧连接继续处理 |
| Restart(重启) | ✅ 会中断 | 先 stop 再 start,所有连接断开 |
reload 重载后,主进程号
不会发生改变restart 重启后,主进程号
往往会发生改变systemctl restart nginx 会清空状态信息,但 reload不会清空
使用其他路径
[root@web01 code]#cd /etc/nginx/conf.d/[root@web01 conf.d]#lltotal 12-rw-r--r-- 1 root root 119 Feb 4 12:45 index.conf-rw-r--r-- 1 root root 201 Feb 4 15:55 test.conf-rw-r--r-- 1 root root 133 Feb 4 10:22 xbw.conf[root@web01 conf.d]#rm -rf xbw.conf# 把这个小霸王的子配置文件给删除了[root@web01 conf.d]#vim test.confserver{ listen 80; server_name kpyun.com;# 默认的域名 location / { root /code; index index.html index.htm; # 默认就能够访问到/code── index.html页面 }'这里是配置的两个location匹配规则''root是网站根目录,/ == /code'上面location是/,而root根是/code# 输入kpyun.com,实际访问的页面是/code/index.html下面location是/download,而root是/code# 输入kpyun.com/download,实际访问的页面是/code/download/index.html# 但是下面这个模块,开启了autoindex索引,它不需要默认index.html页面,也没有index index.html这个条件 location /download { autoindex on; root /code; charset utf-8; autoindex_localtime on; autoindex_exact_size off; # index ......没有这个 }}[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 conf.d]#systemctl reload nginx
10.0.0.7 kpyun.com# Windows修改hosts表,加上映射!浏览器上输入http://kpyun.com/# 报403错误,缺失主页[root@web01 conf.d]#echo 'hhhhh' > /code/index.html'这里我并没有改变配置文件,不需要重启服务'# 刷新浏览器页面,显示hhhhh浏览器上输入http://kpyun.com/download# 报404错误,找不到页面,因为我没有创建download目录[root@web01 conf.d]#mkdir /code/download/# 刷新页面就能够看到了

root VS alias
root 和 alias 指令在 location 块中的行为是不同的
原始配置(使用 root):
location /download { root /code;}- 当用户访问
http://kpyun.com/download时, - Nginx 会将请求映射到文件系统路径:
/code/download
因为
root是 拼接整个 URI 路径 到指定目录之后😻这里的
root就是网站根目录, / == /code
修改后配置(使用 alias):
location /download { alias /www;}- 当用户访问
http://kpyun.com/download时, - Nginx 会将
/download替换为/www,最终映射到:/www
因为
alias是 用指定路径替换匹配的 location 前缀✅ 当你把
root /code;改为alias /www;后用户访问
kpyun.com/download/xxx就会实际访问服务器上的/www/xxx文件
访问控制
ngx_http_access_module
限制客户端IP地址的访问
1)先拒绝、后允许[root@web01 conf.d]#vim test.confserver{ listen 80; server_name kpyun.com;
location / { root /code; index index.html index.htm; deny 10.0.0.41; # 拒绝10.0.0.41访问 '也可以是网段10.0.0.0/24' allow all; # 允许其他所有IP访问 '这里是allow,允许的意思' }}[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 conf.d]#systemctl reload nginx# 千万别忘了重启服务
# 我们来到backup服务器中[root@backup ~]#vim /etc/hosts127.0.0.1 localhost::1 localhost10.0.0.7 kpyun.com'注意这里填的不能是172.16.1.7'# 它是内网,我们拒绝的是10.0.0.41,并没有拒绝172.16.1.41如果这里填的是内网的映射,我们backup(172.16.1.41)可以通过内网访问到172.16.1.7[root@backup ~]#ping kpyun.comPING kpyun.com (10.0.0.7) 56(84) bytes of data.64 bytes from kpyun.com (10.0.0.7): icmp_seq=1 ttl=64 time=0.431 ms# 能够ping通就行[root@backup ~]#curl kpyun.com<head><title>403 Forbidden</title></head># 403,权限拒绝,或者主页缺失,这里是权限拒绝!
# 我们来到web01中[root@web01 conf.d]#cat /etc/hosts10.0.0.7 kpyun.com[root@web01 conf.d]#curl kpyun.comhhhhh
# 我们Windows的hosts表配置完地址映射后也是可以访问到的10.0.0.7 kpyun.com打开浏览器输入http://kpyun.com/# 他也是可以访问的
2)先允许、后拒绝(公司业务后台)'不能上来直接allow all; 不然后面的deny拒绝不生效'# 人间都进来了,你再拒绝有什么用呢?# 先允许一个小的地址,后拒绝一个大的地址;'我们用先拒绝,再允许多一点!!!'[root@web01 conf.d]#vim test.conf.... location / { root /code; index index.html index.htm; allow 10.0.0.41; # 只允许10.0.0.41访问 deny all; # 拒绝其他别的网络 }....[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 conf.d]#systemctl reload nginx# 千万别忘了重启服务打开浏览器输入http://kpyun.com/# 403,权限拒绝![root@web01 conf.d]#curl kpyun.com<head><title>403 Forbidden</title></head># web01也是403[root@backup ~]#curl kpyun.comhhhhh# backup能够成功访问
---------------------小插曲!!![root@web01 conf.d]#vim test.conf.... location / { root /code; index index.html index.htm; allow 10.0.0.0/24; # 修改成10.0.0.0/24网段都能够访问 deny all; }....'-t检查配置,reload重载服务'
[root@backup ~]#vim /etc/hosts172.16.1.7 kpyun.com# 修改backup的hosts表,把web01映射为172.16.1.7# 那么backup就是用内网172.16.1.41和它通信的# 可是web01的配置中只允许10.0.0.0/24网段能够访问[root@backup ~]#curl kpyun.com<head><title>403 Forbidden</title></head># 拉取一下,权限拒绝!!!403---------------------
'综合实例'# 先拒绝某个IP(小),再允许多个不同的网段(大)、然后拒绝所有(大)location / { deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; deny all;}用户认证
ngx_http_auth_basic_module
该模块允许通过验证 用户名 和 密码 来限制资源访问
语法: auth_basic string | off;默认: auth_basic off;# 描述信息,默认是off关闭状态;背景: http, server, location, limit_except# 可以用在以上模块'auth_basic 用户验证模块'
第一步: 生成密码文件[root@web01 nginx]#pwd/etc/nginx# 注意路径;子配置文件包含在主配置文件里面;主配置文件的路径也是/etc/nginx[root@web01 nginx]#htpasswd -b -c auth_pass oldboy 123Adding password for user oldboy-b # 允许在命令行直接输入密码 免交互-c # 创建密码文件[root@web01 nginx]#cat auth_passoldboy:$apr1$2W7hJNVR$zil5rZzGfNSt5EK1OEtYp.# 如果我们要增加新用户只用-b选项,不需要再次创建密码文件(-c)[root@web01 nginx]#htpasswd -b auth_pass heima 521Adding password for user heima[root@web01 nginx]#cat auth_passoldboy:$apr1$2W7hJNVR$zil5rZzGfNSt5EK1OEtYp.heima:$apr1$46xksJKo$5YDxFa1aN4dI3fP0GUePX1'这样里面就有了两个用户了'
第二步: 修改子配置文件[root@web01 nginx]#vim ./conf.d/test.confserver{ listen 80; server_name kpyun.com;
location / { root /code; index index.html index.htm; allow 10.0.0.0/24; deny all; }
location /download { auth_basic 'kpyun'; # 描述信息,必须有 auth_basic_user_file auth_pass; # 指定密码文件的位置 # 密码文件在/etc/nginx/目录下,而子配置包含在主配置文件里面,并且目录一致,这里采用的是相对路径 #我们也可以采用绝对路径,/etc/nginx/auth_pass autoindex on; root /code; charset utf-8; autoindex_localtime on; autoindex_exact_size off; }}[root@web01 nginx]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 nginx]#systemctl restart nginx
# 浏览器测试访问http://kpyun.com/download/# 输入用户名oldboy和密码123
[root@web01 nginx]#cat /var/log/nginx/access.log | tail -1# 查看最新的日志信息10.0.0.1 - oldboy [05/Feb/2026:09:54:01 +0800] .....'这里就显示出来了登录的用户信息'$remote_user # 客户端登录用户名状态模块
ngx_http_stub_status_module
提供基本状态信息的访问
背景: server, location# 只能在server块,或者location块中进行访问实例:location /nginx_status { stub_status; }如果有人访问 kpyun.com/nginx_status则直接访问模块信息就可以了1)修改子配置文件[root@web01 nginx]#vim ./conf.d/test.confserver{ listen 80; server_name kpyun.com;
location / { root /code; index index.html index.htm; }
location /nginx_status { stub_status; }.....}[root@web01 nginx]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 nginx]#systemctl reload nginx
# 浏览器访问http://kpyun.com/nginx_status# 显示以下状态信息Active connections: 2server accepts handled requests 5 5 7Reading: 0 Writing: 1 Waiting: 17种状态
Nginx 连接与请求状态详解(餐厅模型)
| Nginx 指标 | 餐厅比喻 | 技术含义 | 是否属于 active |
|---|---|---|---|
| accepts | 开业以来走进门的总人次 | 成功完成 TCP 三次握手的连接数 | ❌(历史累计) |
| handled | 被服务员成功接待的人次;服务员上前打招呼、安排座位,才算“接待” | Nginx 成功处理的连接数(通常 ≈ accepts,若远小于说明有连接被拒绝或超时) | ❌(历史累计) |
| requests | 所有点单次数总和 | HTTP 请求总数(一个连接可发起多个请求) | ❌(历史累计) |
| active | 正在吃饭的客人总数 | 当前活跃的连接数(正在处理请求) | ✅ |
| reading | 正在看菜单 / 喊服务员 | Nginx 正在读取客户端请求头/体 | ✅(是 active 的子集) |
| writing | 正在上菜 / 送账单 | Nginx 正在向客户端发送响应 | ✅(是 active 的子集) |
| waiting | 吃完饭但占座休息(等下一轮点单) | 处于 keep-alive 状态的空闲连接(等待新请求) | ❌(不属于 active) |
✅ 总结口诀
- 进店看 accepts,接待看 handled 点单总数是 requests
- 吃饭 active 分三态:reading、writing、和其他 吃完占座不算 active,那是 waiting 在等待
systemctl restart nginx 会清空状态信息,但 reload不会清空
💡 注意: active = reading + writing + (极少数其他中间状态)
而 waiting 是独立于 active 的
健康指标:正常情况下,accepts ≈ handled
如果 handled << accepts,说明很多连接进来了但 Nginx 没处理
HTTP 请求总数(非连接数!)
一个客人可以点多次菜(比如刷新页面、加载图片)
requests >> handled是正常的总连接数:
active + waiting = 当前总 TCP 连接数waiting 不算在 active 里
⚠️ 常见问题排查
| 现象 | 可能原因 |
|---|---|
accepts >> handled | 连接被拒绝 |
waiting 很高(如 >10k) | 客户端 keep-alive 时间长,或连接泄漏 |
writing 持续很高 | 大量慢速下载或网络瓶颈 |
reading 很高 | 大量上传或慢速 POST |
连接数限制
ngx_http_limit_conn_module
限制客户端并发连接数,特别是 单个IP地址 的并发连接数量
http { limit_conn_zone $remote_addr zone=conn_zone:10m; ... }----------------------------------- # ⚠️ 注意:必须配置在HTTP层、不能配置到server层limit_conn_zone:定义一个共享内存区域(zone)$remote_addr:表示客户端的 IP 地址zone=conn_zone:10m 1)conn_zone 是这个 共享内存区域(zone) 的名字,后续在server层 limit_conn 中会被引用 2)10m 表示分配 10MB 内存给这个 zone,大约可以存储 约 16 万个 IP 地址-----------------------------------server { '连接限制,限制每个IP最多5个并发连接' limit_conn conn_zone 5; limit_conn_status 429; # 并发连接数量限制被触发时,Nginx 返回给客户端的 HTTP 状态码 # 默认 503 '使用 429 能让客户端更清晰地知道:不是服务器故障,而是你请求太频繁/连接太多,请稍后再试'}
# 修改配置文件[root@web01 conf.d]#vim xbw.conflimit_conn_zone $remote_addr zone=conn_zone:10m;# 这里写在子配置文件中,它在server外面,也就是在http里面server { listen 80; server_name xbw.kpyun.com; limit_conn conn_zone 5; limit_conn_status 429; root /code/xbw; index index.html;}[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 conf.d]#systemctl reload nginx⚠️ 注意事项
不要设为
1,某些浏览器在加载图片时会 并行请求 ,1会导致页面加载卡顿甚至失败
| 场景 | 推荐值 |
|---|---|
| 普通网站 | 5(最常用) |
| 高交互应用 | 8~10 |
| 严格防护 | 2~3 |
✅ 如果你不确定,就从
5开始,观察日志和用户体验,再微调
请求数限制
ngx_http_limit_req_module
限制客户端(同一IP) 在单位时间内 的请求数量
#出现请求攻击行文1.可以通过deny模块限制IP2.配合连接数(同一个IP地址并发连接数)和请求数(同一IP在单位时间内请求的资源数)limit_conn_zone $remote_addr zone=req_zone:10m rate=50r/s;'通常为 $binary_remote_addr(客户端 IP 的二进制形式,节省内存)'# zone=req_zone:10m 定义共享内存区域名称和大小# rate:请求速率,单位为 r/s(每秒请求数)或 r/m(每分钟)# 位于http层limit_req zone=req_zone burst=10 nodelay;# zone=req_zone 引用前面定义的 zone# burst=10 允许突发的请求数# nodelay:不延迟处理突发请求(立即处理突发请求,但超过 burst 则拒绝)'允许每个 IP 每秒 50 个请求,突发最多 10 个(共 60 个),且突发请求不延迟。'limit_req_status 429;# 自定义限流触发时返回的 HTTP 状态码(默认是 503)# 429 Too Many Requests
[root@web01 conf.d]#vim xbw.conflimit_conn_zone $remote_addr zone=conn_zone:10m;limit_req_zone $remote_addr zone=req_zone:10m rate=50r/s;server { listen 80; server_name xbw.kpyun.com; limit_conn conn_zone 5; limit_conn_status 429; limit_req zone=req_zone burst=10 nodelay; limit_req_status 429; root /code/xbw; index index.html;}[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 conf.d]#systemctl reload nginx
这里是,限制客户端(同一IP)
在单位时间内的请求数量,一般都是1秒内🐹但是,客户端,如果一秒内点击了多次刷新,也就是1秒内有了多次请求,就会出现卡顿,返回429,请求太频繁/连接太多,请稍后再试!
error_page
是 Nginx 中用于自定义错误页面的指令,可以将特定的 HTTP 错误状态码(如 404、500 等)重定向到指定的 URI 或返回特定内容
'可以参考Nginx06中的4)案例,他也是错误页面进行跳转'# 只不过上面的是集中都进行跳转[root@web01 conf.d]#vim test.confserver{limit_req_zone $remote_addr zone=test_zone:10m rate=1r/s;# 这里改了一下zone的名字,test_zone# 1秒给一个请求server{ listen 80; server_name kpyun.com;
location / { root /code; index index.html index.htm; limit_req zone=test_zone burst=1 nodelay; # 对应修改名字test_zone,同时burst=1 limit_req_status 429; error_page 429 /429.html; error_page 403 /403.html; error_page 404 /404.html; # 这个/是root指定的路径/code # 所以我们需要再/code路径下创建这些文件 }}[root@web01 conf.d]#vim /code/429.html<img style='width:100%;height:100%;' src=/image/429.png># 这个路径也是一样的,/根目录是/code,所以我们需要把图片放在/code/image/.png# 403,404页面类似![root@web01 code]#lltotal 34064-rw-r--r-- 1 root root 57 Feb 5 15:53 403.html-rw-r--r-- 1 root root 57 Feb 5 15:54 404.html-rw-r--r-- 1 root root 57 Feb 5 15:53 429.htmldrwxr-xr-x 2 root root 6 Feb 5 15:54 image[root@web01 code]#cd image/# 进入到图片目录[root@web01 image]#rz -Erz waiting to receive......# 上传图片[root@web01 image]#lltotal 3092-rw-r--r-- 1 root root 153023 Feb 5 15:34 403.png-rw-r--r-- 1 root root 62776 Feb 5 15:33 404.png-rw-r--r-- 1 root root 2941243 Feb 5 15:30 429.png# 把图片提前准备好[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful# 检测时候不要有错误出现,别忘记以;结尾[root@web01 conf.d]#systemctl reload nginx
浏览器输入http://kpyun.com/
# 我们把默认的主页给删除了试一试'主页缺失,403错误'[root@web01 conf.d]#rm -f /code/index.html
# 我们输入一个错误的路径,再试试!'找不到文件,404错误'
error_log
[root@web01 nginx]#cat nginx.confuser nginx;worker_processes auto;# 在nginx的核心层它是有错误日志的,这个是总的错误日志# 但是如果我们想要实现,每个站点设置独立的错误日志呢?'访问日志也是一个道理!也可以独立出来!'error_log /var/log/nginx/error.log notice;.....
1)先注释掉主配置文件的错误日志[root@web01 nginx]#grep error_log /etc/nginx/nginx.conf# error_log /var/log/nginx/error.log notice;'成功把它注释掉'-----------------------语法: error_log 文件名 级别;-----------------------| 级别(Level) | 说明 |
|---|---|
| debug | 调试信息(需编译时启用 --with-debug) |
| info | 一般信息 |
| notice | 通知信息(正常但值得注意) |
| warn | 警告信息(可能有问题) |
| error | 错误信息(请求处理出错) |
| crit | 严重错误 |
| alert | 必须立即处理的问题 |
| emerg | 系统不可用 |
debug:未来调试使用,短时间开启,网站访问量较大不要开启!
2)编辑子配置文件,把它放在server层中[root@web01 conf.d]#vim test.confserver{ listen 80; server_name kpyun.com; error_log /var/log/nginx/kpyun.error.log notice;.....[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 conf.d]#systemctl reload nginx[root@web01 conf.d]#ll /var/log/nginx/kpyun.error.log-rw-r--r-- 1 root root 0 Feb 5 19:19 /var/log/nginx/kpyun.error.log
浏览器访问http://kpyun.com/no# 这是一个不存在的路径,404报错![root@web01 conf.d]#cat /var/log/nginx/kpyun.error.log | tail -12026/02/05 19:24:10 [error] 4339#4339: *69 open() "/etc/nginx/html/no" failed (2: No such file or directory), client: 10.0.0.1, server: kpyun.com, request: "GET /no HTTP/1.1", host: "kpyun.com"location匹配优先级
| 匹配符 | 匹配规则说明 | 是否区分大小写 | 是否使用正则 | 优先级(数字越小优先级越高) |
|---|---|---|---|---|
= | 精确匹配 URI(完全相等) | 否 | 否 | 1(最高) |
^~ | 前缀匹配,URI 以指定字符串开头 | 否 | 否 | 2 |
~ | 使用正则表达式匹配 | ==是== | ==是== | 3 |
~* | 使用正则表达式匹配(不区分大小写) | 否 | ==是== | 4 |
/ | 通用前缀匹配(最长前缀匹配) | 否 | 否 | 5(最低) |
注意事项:
=和^~虽然都是前缀类,但=要求完全一致,而^~只需前缀匹配/是默认匹配,常用于反向代理或返回首页
# 官网有实例的documentation(官方文档)--->ngx_http_core_module(核心模块)--->location(路径匹配)# 实例如下!
1)修改子配置文件[root@web01 conf.d]#vim index.confserver{ listen 80; server_name 10.0.0.7; # 用ip地址进行访问 default_type text/html; # 返回的类型 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"; }}[root@web01 conf.d]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 conf.d]#systemctl reload nginx
2)测试优先级[root@web01 conf.d]#curl 10.0.0.7configuration A# 默认会给你带上根/[root@web01 conf.d]#curl 10.0.0.7/configuration A'上面两个完全等价'[root@web01 conf.d]#curl 10.0.0.7/index.htmlconfiguration B# 当访问根/下某一个具体的资源页面时# = / 没有精准匹配到/,故继续往下找# /documents/ 也并非是它目录下的文件# 最后只能匹配到 /;常用于放回首页[root@web01 conf.d]#curl 10.0.0.7/documents/index.htmlconfiguration C# 是/documents/下的资源页面,自然匹配它[root@web01 conf.d]#curl 10.0.0.7/documents/1.gifconfiguration E# 虽然也是访问/documents/下的资源,但是优先匹配 ~* \.(gif|jpg|jpeg)[root@web01 conf.d]#curl 10.0.0.7/images/1.gifconfiguration D限速模块
[root@web01 code]#vim /etc/nginx/conf.d/test.conf..... location /download { root /code; autoindex on; charset utf-8; autoindex_localtime on; autoindex_exact_size off; limit_rate_after 50M; # 下载50M不限速 limit_rate 50k; # 然后以50k的速度下载 }# nginx -t systemctl reload nginx浏览器输入http://kpyun.com/download/# 实际访问的是/code/download# 不要/code/download/index.html,不要index index.html indx.htm选项文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!



