Linux-核心配置拓展
检测文件是否存在
try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如 果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一 个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内 部500错误。
语法格式
Syntax: try_files file ... uri; try_files file ... =code; Default: — Context: server, location
location / { root /data/nginx/html/pc; index index.html; try_files $uri $uri.html $uri/index.html /about/default.html; #try_files $uri $uri/index.html $uri.html =489; } [root@centos8 ~]# echo "default page" >> /data/nginx/html/pc/about/default.html
#重启nginx并测试,当访问到http://www.magedu.org/about/xx.html等不存在的uri会显示 default.html,如果是自定义的状态码则会显示在返回数据的状态码中 #注释default.html行,启用上面489响应码的那一行,在其生效后再观察结果
location / { root /data/nginx/html/pc; index index.html; #try_files $uri $uri.html $uri/index.html /about/default.html; try_files $uri $uri/index.html $uri.html =489; } [root@centos8 ~]# curl -I http://www.magedu.org/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 timeout [header_timeout]; #设定保持连接超时时长,0表示禁止长连接,默认为75s,通常配置在http字段作为站点全局配置
keepalive_requests number; #在一次长连接上所允许请求的资源的最大数量,默认为100次,建议适当调大,比如:500
keepalive_requests 3; keepalive_timeout 65 60; #开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,第二个数字60为发送给客户端应答报文头部中显示的超时时间设置为60s:如不设置客户端将不显示超时 时间。 Keep-Alive:timeout=60 #浏览器收到的服务器返回的报文 #如果设置为0表示关闭会话保持功能,将如下显示: Connection:close #浏览器收到的服务器返回的报文
#使用命令测试: [root@centos8 ~]# telnet www.magedu.org 80 Trying 10.0.0.8... Connected to www.magedu.org. Escape character is '^]'. GET / HTTP/1.1 HOST: www.magedu.org #Response Headers(响应头信息): HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Thu, 24 Sep 2020 04:35:35 GMT Content-Type: text/html Content-Length: 7 Last-Modified: Wed, 23 Sep 2020 14:39:21 GMT Connection: keep-alive Keep-Alive: timeout=60 ETag: "5c8a6b3a-7" Accept-Ranges: bytes #页面内容 pc web
作为下载服务器配置
ngx_http_autoindex_module 模块处理以斜杠字符 "/" 结尾的请求,并生成目录列表,可以做为下载服务 配置使用
autoindex on | off;#自动文件索引功能,默为off autoindex_exact_size on | off; #计算文件确切大小(单位bytes),off 显示大概大小(单位K、M),默认on autoindex_localtime on | off ; #显示本机时间而非GMT(格林威治)时间,默认off autoindex_format html | xml | json | jsonp; #显示索引的页面文件风格,默认html limit_rate rate; #限制响应客户端传输速率(除GET和HEAD以外的所有方法),单位B/s,即 bytes/second,默认值0,表示无限制,此指令由ngx_http_core_module提供 set $limit_rate 4k; #也可以通变量限速,单位B/s,同时设置,此项优级高.Rate limit can also be set in the $limit_rate variable, however, since version 1.17.0, this method is not recommended:
#注意:download不需要index.html文件 [root@centos8 ~]# mkdir -p /data/nginx/html/pc/download [root@centos8 ~]# vim /apps/nginx/conf/conf.d/pc.conf location /download { autoindex on; #自动索引功能 autoindex_exact_size on; #计算文件确切大小(单位bytes),此为默认值,off只显示大概大 小(单位kb、mb、gb) autoindex_localtime on; #on表示显示本机时间而非GMT(格林威治)时间,默为为off显示GMT 时间 limit_rate 1024k; #限速,默认不限速 root /data/nginx/html/pc; } [root@centos8 ~]# cp /root/anaconda-ks.cfg /data/nginx/html/pc/download/ #重启Nginx并访问测试下载页面
作为上传服务器
以下指令控制上传数据
client_max_body_size 1m; #设置允许客户端上传单个文件的最大值,默认值为1m,上传文件超过此值会 出413错误 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@centos8 ~]# 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 100m; #如果太大,上传时会出现下图的413错误,注意:如果php上传,还需要修 改php.ini的相关配置 client_body_buffer_size 1024k; client_body_temp_path /apps/nginx/client_body_temp/ 1 2 2; #上传时,Nginx会自动创 建相关目录 #上传超过nginx指定client_max_body_size值会出413错误 [root@wang-liyun-pc ~]# tail /apps/nginx/logs/nginx.access.log 125.41.184.117 - - [27/Sep/2020:00:09:00 +0800] "POST /wp-admin/async-upload.php HTTP/1.1" 413 578 "http://www.wangxiaochun.com/wp-admin/post-new.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.63" "-"
#上传文件后,会自动生成相关目录
[root@wang-liyun-pc ~]# tree /apps/nginx/client_body_temp/
/apps/nginx/client_body_temp/
├── 5
│ └── 00
│ └── 00
└── 6
└── 00
└── 00
其他配置
keepalive_disable none | browser ...; #对哪种浏览器禁用长连接 limit_except method ... { ... },仅用于location #禁止客户端使用除了指定的请求方法之外的其它方法,如果使用会出现403错误 method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH limit_except GET { allow 192.168.0.0/24; allow 10.0.0.1; deny all; } #除了GET和HEAD 之外其它方法仅允许192.168.1.0/24网段主机使用 [root@centos8 ~]# mkdir /data/nginx/html/pc/upload [root@centos8 ~]# echo "upload" > /data/nginx/html/pc/upload/index.html [root@centos8 ~]# vim /apps/nginx/conf/conf.d/pc.conf location /upload { root /data/nginx/html/pc; index index.html; limit_except GET { allow 10.0.0.6; deny all; } } #重启Nginx并进行测试上传文件 [root@centos8 ~]# systemctl restart nginx [root@centos8 ~]# [root@centos6 ~]# curl -XPUT /etc/issue http://www.magedu.org/about curl: (3) <url> malformed <html> <head><title>405 Not Allowed</title></head> #Nginx已经允许,但是程序未支持上传功能 <body bgcolor="white"> <center><h1>405 Not Allowed</h1></center> <hr><center>nginx</center> </body> </html> [root@centos8 ~]# curl -XPUT /etc/issue http://www.magedu.org/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 on | off #是否启用asynchronous file I/O(AIO)功能,需要编译开启 --with-file-aio #linux 2.6以上内核提供以下几个系统调用来支持aio: 1、SYS_io_setup:建立aio 的context 2、SYS_io_submit: 提交I/O操作请求 3、SYS_io_getevents:获取已完成的I/O事件 4、SYS_io_cancel:取消I/O操作请求 5、SYS_io_destroy:毁销aio的context directio size | off; #操作完全和aio相反,aio是读取文件而directio是写文件到磁盘,启用直接 I/O,默认为关闭,当文件大于等于给定大小时,例如:directio 4m,同步(直接)写磁盘,而非写缓存。 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_valid time; #缓存项有效性的检查验证频率,默认值为60s open_file_cache_errors on | off; #是否缓存查找时发生错误的文件一类的信息,默认值为off open_file_cache_min_uses number; #open_file_cache指令的inactive参数指定的时长内,至少 被命中此处指定的次数方可被归类为活动项,默认值为1 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 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)