Nginx-检测文件是否存在(try_files),长连接配置,上传与下载服务器,限制位置内允许的 HTTP 方法,AIO与sendfile,缓存
检测文件是否存在
try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误。
官网文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
location /about { root /data/nginx/html/pc; #alias /data/nginx/html/pc; index index.html; #try_files $uri /about/default.html; try_files $uri $uri/index.html $uri.html /default.html; //将用户访问的uri最后一个参数名称作为新的变量名称匹配服务器内部的资源,依次匹配,如果匹配成功则返回,如果未匹配成功,则返回default.html页面。
#try_files $uri $uri/index.html $uri.html =489; //如果前面都没有匹配成功,则返回自定义错误码 }
]# echo "default" >> /data/nginx/html/pc/about/default.html
]# mkdir /data/nginx/html/pc/about/index1
]# echo "index v2" > /data/nginx/html/pc/about/index1/index.html
]# echo "index v1" > /data/nginx/html/pc/about/index1.html
重启nginx并测试
conf.d]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful conf.d]# nginx -s reload
about]# curl http://www.magedu.net/about/index.html about index about]# curl http://www.magedu.net/about/index1.html index1 v1 about]# rm -rf index1.html //删除about路径下的文件,使$uri.html不能匹配,测试$uri/功能 about]# curl http://www.magedu.net/about/index1.html //会将index.html作为文件夹名称匹配about路径下的/abuot/index.html/index.html,可以看到前面未匹配成功,匹配到了default。 default
about]# curl http://www.magedu.net/about/index1 //$uri/ 将index1作为文件夹名称匹配到了about/index1文件 index1 v2
修改配置文件
location /about { root /data/nginx/html/pc; index index.html; #try_files $uri /about/default.html; try_files $uri $uri/index1.html $uri.html /default.html; //将$uri/ 默认访问文件改为index1.html #try_files $uri $uri/index.html $uri.html =489; }
重新加载访问
about]# curl http://www.magedu.net/about/index1 default [root@nginx-master about]# ll index1/ total 4 -rw-r--r--. 1 nginx nginx 11 Jan 9 00:13 index.html [root@nginx-master about]# echo "index1 v3" > index1/index1.html [root@nginx-master about]# curl http://www.magedu.net/about/index1 //可以看到配置文件中$uri/ 后面指定访问的文件名称后,这个文件必须存在,不然匹配不到。 index1 v3
如果是自定义的状态码则会显示在返回数据的状态码中
[root@s2 about]# curl --head http://www.magedu.net/about/xx.html HTTP/1.1 489 #489就是自定义的状态返回码 Server: nginx Date: Thu, 21 Feb 2019 00:11:40 GMT Content-Length: 0 Connection: keep-alive Keep-Alive: timeout=65
长连接
keepalive_timeout number; #设定保持连接超时时长,0表示禁止长连接,默认为75s,通常配置在http字段作为站点全局配置 keepalive_requests number; #在一次长连接上所允许请求的资源的最大数量,默认为100次
也可以设置在location,server
http { keepalive_timeout 65 60; //65为一次客户端和服务器长连接超时时间,60为浏览器端显示时间 keepalive_requests 200; //200为一次TCP连接客户端与服务器可以交互的请求次数 ... }
开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,后面的60为发送给客户端应答报文头部中显示的超时时间设置为60s:如不设置客户端将不显示超时时间。
作为下载服务器配置
[root@s2 about]# mkdir /data/nginx/html/pc/download #download不需要index.html文件
[root@s2 about]# vim /apps/nginx/conf/conf.d/pc.conf location /download {
auth_basic "Restricted";
auth_basic_user_file /usr/local/nginx/conf/passwords;
autoindex off; #关闭自动索引功能 autoindex_exact_size on; #计算文件确切大小(单位bytes),off只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; #显示本机时间而非GMT(格林威治)时间
root /data/nginx/html/pc; }
limit_rate 10k; #限制响应给客户端的传输速率,单位是bytes/second,默认值0表示无限制
生成用户密码
htpasswd -c passwords user
[root@s2 pc]# cp /root/anaconda-ks.cfg /data/nginx/html/pc/download/
重启Nginx并访问测试下载页面
限速与不限速的对比:
作为上传服务器
配置路径:location,server,http
client_max_body_size 1m; #设置允许客户端上传单个文件的最大值,默认值为1m client_body_buffer_size size; #用于接收每个客户端请求报文的body部分的缓冲区大小;默认16k;超出此大小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置
client_body_temp_path path [level1 [level2 [level3]]]; #设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量,目录名为16进制的数字,使用hash之后的 值从后往前截取1位、2位、2位作为文件名:
[root@s3 ~]# md5sum /data/nginx/html/pc/index.html
95f6f65f498c74938064851b1bb 96 3d 4 /data/nginx/html/pc/index.html
1级目录占1位16进制,即2^4=16个目录 0-f
2级目录占2位16进制,即2^8=256个目录 00-ff
3级目录占2位16进制,即2^8=256个目录 00-ff
配置示例:
client_max_body_size 10m;
client_body_buffer_size 16k;
client_body_temp_path /apps/nginx/temp 1 2 2; #reload Nginx会自动创建temp目录,注意目录权限
对哪种浏览器禁用长连接
Default:
keepalive_disable msie6; //IE6浏览器不支持长连接
Context: http, server, location
限制位置内允许的 HTTP 方法
Syntax: limit_except method ... { ... }
Default: —
Context: location
限制位置内允许的 HTTP 方法。该method
参数可以是下列之一: GET
, HEAD
, POST
, PUT
, DELETE
, MKCOL
, COPY
, MOVE
, OPTIONS
, PROPFIND
, PROPPATCH
, LOCK
, UNLOCK
,或 PATCH
。允许该GET
方法使得该 HEAD
方法也被允许。可以使用ngx_http_access_module、 ngx_http_auth_basic_module和 ngx_http_auth_jwt_module (1.13.10) 模块指令来限制对其他方法的访问 :
limit_except GET { allow 192.168.1.0/32; deny all; }
请注意,这将限制对除GET 和 HEAD之外的所有方法的访问 。
测试访问
]# curl -XPUT /etc/issue http://www.magedu.net/upload curl: (3) <url> malformed <html> <head><title>403 Forbidden</title></head> #Nginx拒绝上传 <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx</center> </body> </html>
AIO与sendfile
需要编译时加载模块 --with-file-aio
Syntax: aio on | off | threads[=pool]; Default: aio off; Context: http, server, location This directive appeared in version 0.8.11.
在 Linux 上,可以从内核版本 2.6.22 开始使用 AIO。此外,必须启用 directio,否则读取将被阻塞:
location /video/ { aio on; directio 512; output_buffers 1 128k; }
在 Linux 上同时启用 AIO 和sendfile 时,AIO 用于大于或等于directio指令中指定大小的文件,而sendfile用于较小大小的文件或禁用directio时。
location /video/ {
sendfile on;
aio on;
directio 8m; #directio是直接读写文件到磁盘,启用直接I/O,默认为关闭,当文件大于等于给定大小时,例如directio 4m,可以和sebdfile结合使用,比如当大于此值使用AIO,当小于此值使用sendfile
}
缓存
Context: http, server, location
open_file_cache off; #是否缓存打开过的文件信息 open_file_cache max=N [inactive=time];
nginx可以缓存以下三种信息: (1) 文件元数据:文件的描述符、文件大小和最近一次的修改时间 (2) 打开的目录结构 (3) 没有找到的或者没有权限访问的文件的相关信息 max=N:可缓存的缓存项上限数量;达到上限后会使用LRU(Least recently used,最近最少使用)算法实现管理 inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项,将被删除
open_file_cache_errors on | off; 是否缓存查找时发生错误的文件一类的信息 默认值为off
open_file_cache_min_uses number; open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项 默认值为1
open_file_cache_valid time; 缓存项有效性的检查验证频率,默认值为60s
例子: open_file_cache max=10000 inactive=60s; #最大缓存10000个文件,非活动数据超时时长60s open_file_cache_valid 60s; #每间隔60s检查一下缓存数据有效性 open_file_cache_min_uses 5; #60秒内至少被命中访问5次才被标记为活动数据 open_file_cache_errors on; #缓存错误信息
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~