服务器之光:Nginx--核心配置详解及演练
3.Nginx 核心配置详解
3.1 配置文件
nginx 官方帮助文档:http://nginx.org/en/docs/
3.1.1 Nginx的配置文件的组成部分
- 主配置文件:nginx.conf
- 子配置文件: include conf.d/*.conf
- fastcgi, uwsgi,scgi 等协议相关的配置文件
- mime.types:支持的mime类型,MIME(Multipurpose Internet Mail Extensions)多用途互联网邮
- 件扩展类型,MIME消息能包含文本、图像、音频、视频以及其他应用程序专用的数据,是设定某
- 种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动
- 使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。
1.nginx 配置文件格式说明
配置文件由指令与指令块构成
每条指令以;分号结尾,指令与值之间以空格符号分隔
可以将多条指令放在同一行,用分号分隔即可,但可读性差,不推荐
指令块以{ }大括号将多条指令组织在一起,且可以嵌套指令块
include语句允许组合多个配置文件以提升可维护性
使用#符号添加注释,提高可读性
使用$符号使用变量
部分指令的参数支持正则表达式
2.Nginx主配置文件的配置指令方式
directive value [value2 ...];注意
(1) 指令必须以分号结尾
(2) 支持使用配置变量内建变量:由Nginx模块引入,可直接引用自定义变量:由用户使用set命令定义,格式: set variable_name value;引用变量:$variable_name
主配置文件结构:四部分
main block:主配置段,即全局配置段,对http,mail都有效#事件驱动相关的配置
event {...
} #http/https 协议相关配置段
http {...
} #默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {...
}#stream 服务器相关配置段
stream {...
}
默认的nginx.conf 配置文件格式说明
#全局配置端,对全局生效,主要设置nginx的启动用户/组,启动的工作进程数量,工作模式,Nginx的PID路径,日志路径等。
user nginx nginx;
worker_processes 1; #启动工作进程数数量
events { #events #设置快,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多个网络连接,使用哪种事件驱动模型 #处理请求,每个工作进程可以同时支持的最大连接数,是否开启对多工作进程下的网络连接进行序列化等。worker_connections 1024; #设置单个nginx工作进程可以接受的最大并发,作为web服务器的时候最大并发数为 #worker_connections * worker_processes,作为反向代理的时候为#(worker_connections * worker_processes)/2
}http { #http块是Nginx服务器配置中的重要部分,缓存、代理和日志格式定义等绝大多数功能和第三方模块都 #可以在这设置,http块可以包含多个server块,而一个server块中又可以包含多个location块,#server块可以配置文件引入、MIME-Type定义、日志自定义、是否启用sendfile、连接超时时间和 #单个链接的请求上限等。include mime.types;default_type application/octet-stream;sendfile on; #作为web服务器的时候打开sendfile加快静态文件传输,指定是否使用#sendfile系统调用来传输文件#sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作)#从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝,#硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈。keepalive_timeout 65; #长连接超时时间,单位是秒server { #设置一个虚拟机主机,可以包含自己的全局快,同时也可以包含多个location模块#比如本虚拟机监听的端口、本虚拟机的名称和IP配置,多个server 可以使用一个端口比如都使用 #80端口提供web服务listen 80; #配置server监听的端口server_name localhost; #本server的名称,当访问此名称的时候nginx会调用当前serevr内部的配置进程匹配。location / { #location其实是server的一个指令,为nginx服务器提供比较多而且灵活的指令#都是在location中体现的,主要是基于nginx接受到的请求字符串#对用户请求的UIL进行匹配,并对特定的指令进行处理#包括地址重定向、数据缓存和应答控制等功能都是在这部分实现#另外很多第三方模块的配置也是在location模块中配置。root html; #相当于默认页面的目录名称,默认是安装目录的相对路径,可以使用绝对路径配置。index index.html index.htm; #默认的页面文件名称}error_page 500 502 503 504 /50x.html; #错误页面的文件名称location = /50x.html { #location处理对应的不同错误码的页面定义到/50x.html#这个跟对应其server中定义的目录下。root html; #定义默认页面所在的目录}}#和邮件相关的配置
#mail {
# ...
# } mail 协议相关配置段#tcp代理配置,1.9版本以上支持
#stream {
# ...
# } stream 服务器相关配置段
#导入其他路径的配置文件
#include /apps/nginx/conf.d/*.conf
}
3.2 全局配置
Main 全局配置段常见的配置指令分类
- 正常运行必备的配置
- 优化性能相关的配置
- 用于调试及定位问题相关的配置
- 事件驱动相关的配置
3.2.1 全局配置说明
user nginx nginx; #启动Nginx工作进程的用户和组
worker_processes [number | auto]; #启动Nginx工作进程的数量,一般设为和CPU核心数相同worker_cpu_affinity 00000001 00000010 00000100 00001000 | auto ;
#将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。CPU MASK: 00000001:0号CPU00000010:1号CPU10000000:7号CPU#示例
worker_cpu_affinity 0001 0010 0100 1000;第0号---第3号CPU
worker_cpu_affinity 0101 1010; #示例
worker_processes 4;
worker_cpu_affinity 00000010 00001000 00100000 10000000;[root@centos8 ~]# ps axo pid,cmd,psr | grep nginx
31093 nginx: master process /apps 1
34474 nginx: worker process 1
34475 nginx: worker process 3
34476 nginx: worker process 5
34477 nginx: worker process 7#错误日志记录配置,语法:error_log file [debug | info | notice | warn | error | crit | alert | emerg]#error_log logs/error.log;
#error_log logs/error.log notice;
error_log /usr/local/nginx/logs/error.log error; #pid文件保存路径
pid /usr/local/nginx/logs/nginx.pid;
worker_priority 0; #工作进程优先级,-20~20(19)
worker_rlimit_nofile 65536; #所有worker进程能打开的文件数量上限,#包括:Nginx的所有连接(例如与代理服务器的连接等)#而不仅仅是与客户端的连接#另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制#最好与ulimit -n 或者limits.conf的值保持一致,#修改pam限制
[root@Nginx ~]# sudo -u nginx ulimit -n
1024[root@Nginx ~]# vim /etc/security/limits.conf
* - nofile 100000
[root@Nginx ~]# sudo -u nginx ulimit -n
100000daemon off; #前台运行Nginx服务用于测试、docker等环境。
master_process off|on; #是否开启Nginx的master-worker工作模式,仅用于开发调试场景,默认为onevents {worker_connections 65535; #设置单个工作进程的最大并发连接数use epoll; #使用epoll事件驱动,#Nginx支持众多的事件驱动,#比如:select、poll、epoll,只能设置在events模块中设置accept_mutex on; #on为同一时刻一个请求轮流由work进程处理,#而防止被同时唤醒所有worker#避免多个睡眠进程被唤醒的设置,默认为off#新请求会唤醒所有worker进程,此过程也称为"惊群"#因此nginx刚安装完以后要进行适当的优化。建议设置为onmulti_accept on; #on时Nginx服务器的每个工作进程可以同时接受多个新的网络连接#此指令默认为off,#即默认为一个工作进程只能一次接受一个新的网络连接#打开后几个同接受多个。建议设置为on}
1.示例: 实现 nginx 的高并发配置
[root@Nginx ~]# ulimit -n100000
[root@Nginx ~]# ab -n10000 -c5000 http://172.25.254.10/index.html# 默认配置不支持高并发,会出现以下错误日志
[root@Nginx ~]# tail /apps/nginx/logs/error.log
2020/09/24 21:19:33 [crit] 41006#0: *1105860 open() "/apps/nginx/html/50x.html"
failed (24: Too many open files), client: 10.0.0.7, server: localhost, request:
"GET / HTTP/1.0", host: "10.0.0.8"
2020/09/24 21:19:33 [crit] 41006#0: accept4() failed (24: Too many open files)
2020/09/24 21:19:33 [crit] 41006#0: *1114177 open()
"/apps/nginx/html/index.html" failed (24: Too many open files), client: 10.0.0.7,
server: localhost, request: "GET / HTTP/1.0", host: "10.0.0.8"# 修改配置
[root@Nginx ~]# vim /etc/security/limits.conf
* - nofile 100000
* - nproc 100000[root@Nginx ~]# vim /apps/nginx/conf/nginx.conf
worker_rlimit_nofile 100000;
events{worker_connections 100000;use epoll;
}[root@Nginx ~]# nginx -s reload[root@Nginx ~]# ulimit -n100000
[root@Nginx ~]# ab -n10000 -c5000 http://172.25.254.10/index.html



3.3 http配置块
#在响应报文中将指定的文件扩展名映射至MIME对应的类型
include /etc/nginx/mime.types;
default_type application/octet-stream; #除mime.types中的类型外#指定其它文件的默认MIME类型,浏览器一般会提示下载types {text/html html;image/gif gif;image/jpeg jpg;
}
1.示例:识别php文件为text/html
[root@Nginx ~]# vim /usr/local/nginx/html/lee.php
<?phpphpinfo();
?>[root@Nginx ~]# curl -I 172.25.254.100/lee.php
HTTP/1.1 200 OK
Server: nginx/1.26.1Content-Type: application/octet-stream
Content-Length: 24
Last-Modified: Fri, 19 Jul 2024 09:38:52 GMT
Connection: keep-alive
ETag: "669a342c-18"
Accept-Ranges: bytes[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
default_type text/html;
[root@Nginx ~]# nginx -s reload[root@Nginx ~]# curl -I 172.25.254.100/lee.php
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Fri, 19 Jul 2024 09:49:49 GMT
Content-Type: text/html
Content-Length: 24
Last-Modified: Fri, 19 Jul 2024 09:38:52 GMT
Connection: keep-alive
ETag: "669a342c-18"
Accept-Ranges: bytes
3.4 核心配置示例
基于不同的IP、不同的端口以及不用得域名实现不同的虚拟主机,依赖于核心模块ngx_http_core_module实现。
3.4.1 新建一个 PC web 站点
[root@web-a ~]# mkdir -p /usr/local/nginx/conf.d/[root@web-a ~]# vim /usr/local/nginx/conf/nginx.conf
......include /apps/nginx/conf/conf.d/*.conf; #在配置文件的最后面添加此行#注意不要放在最前面,会导致前面的命令无法生效# 创建虚拟主机网站配置
[root@web-a ~]# vim /usr/local/nginx/conf.d/vhost.conf[root@web-a ~]# mkdir -p /web/html[root@web-a ~]# echo web_html > /web/html/index.html[root@web-a ~]# nginx -s reload[root@web-a ~]# vim /etc/hosts
172.25.254.10 www.dhj.org# 进行访问测试
[root@web-a ~]# curl 172.25.254.10
web-a:172.25.254.10[root@web-a ~]# curl www.dhj.org
web_html
3.4.2 root 与 alias
1.root示例
root:指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location
server {listen 80;server_name lee.timinglee.org;location / {root /webdata/nginx/timinglee.org/lee/html;}location /dirtest { #必须建立/mnt/dirtest才能访问root /mnt;}
}[root@Nginx ~]# mkdir /mnt/dirtest/
[root@Nginx ~]# echo dirtest page > /mnt/dirtest/index.html
[root@Nginx ~]# nginx -s reload#重启Nginx并访问测试
[root@node100 ~]# curl lee.timinglee.org/dirtest/
dirtest page
2.alias示例
alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制;仅能用于location上下文,此指令使用较少
server {listen 80;server_name lee.timinglee.org;location / {root /webdata/nginx/timinglee.org/lee/html;}location /dirtest {root /mnt;}location /alias { #注意about后不要加/#使用alias的时候uri后面如果加了斜杠,则下面的路径配置必须加斜杠,否则403alias /mnt/dirtest; #当访问alias的时候,会显示alias定义的/mnt/dirtest里面的内容 }
}#重启Nginx并访问测试
[root@node100 ~]# curl lee.timinglee.org/alias/
dirtest page
[!TIP]
root
- 最终文件路径 = root(/var/www/my_site) + 请求路径(/images/logo.png) = (/var/www/my_site/images/logo.png)
alias
- 最终文件路径 = alias(/data/shared_assets) + 请求路径(/static/css/style.css)去掉匹配部分后的剩余部分(css/style.css) = /data/shared_assets/css/style.css
# 对比如下,示例如下
server {listen 80;server_name example.com;root /var/www/my_site; # 根目录设置在这里# 请求 http://example.com/about/index.html# Nginx 会找:/var/www/my_site/about/index.html
}server {listen 80;server_name example.com;root /var/www/my_site;# 处理以 /static/ 开头的请求location /static/ { # 注意这个 location 匹配路径结尾有 /alias /data/shared_assets/; # 注意 alias 路径结尾有 /}# 请求 http://example.com/static/css/style.css# 1. 匹配到 location /static/# 2. 去掉匹配的 "/static/", 剩下 "css/style.css"# 3. 附加到 alias 路径: /data/shared_assets/css/style.css
}
3.4.3 location 的详细使用
-
在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;
-
ngnix会根据用户请求的URI来检查定义的所有location,按一定的优先级找出一个最佳匹配,
-
而后应用其配置在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri
-
uri是用户请求的字符串,即域名后面的web文件路径
-
然后使用该location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理此请求。
#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }= #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立即处理请求^~ #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头#对uri的最左边部分做匹配检查,不区分字符大小写~ #用于标准uri前,表示包含正则表达式,并且区分大小写~* #用于标准uri前,表示包含正则表达式,并且不区分大写不带符号 #匹配起始于此uri的所有的uri\ #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号#匹配优先级从高到低:
=, ^~, ~/~*, 不带符号
1.匹配案例-精确匹配
在server部分使用location配置一个web界面,例如:当访问nginx 服务器的/logo.jpg的时候要显示指定html文件的内容,精确匹配一般用于匹配组织的logo等相对固定的URL,匹配优先级最高
- 精确匹配 logo
[root@Nginx ~]# mkdir /webdata/nginx/timinglee.org/lee/images -p
[root@Nginx ~]# ls /webdata/nginx/timinglee.org/lee/images
[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.confserver {listen 80;server_name lee.timinglee.org;location / {root /webdata/nginx/timinglee.org/lee/html;}location = /logo.png {root /webdata/nginx/timinglee.org/lee/images;}
}#上传logo.jpg图片到/webdata/nginx/timinglee.org/lee/images,重启Nginx并访问测试
#访问测试:http://www.timinglee.org/logo.png
2.匹配案例-区分大小写
实现区分大小写的模糊匹配. 以下范例中,
如果访问uri中包含大写字母的logo.PNG,则以下location匹配logo.png条件不成功
因为 ~ 区分大小写,当用户的请求被执行匹配时发现location中定义的是小写的png,
本次访问的uri匹配失败,后续要么继续往下匹配其他的location(如果有),要么报错给客户端
server {listen 80;server_name lee.timinglee.org;location / {root /webdata/nginx/timinglee.org/lee/html;}location ~ /logo.PNG {root /webdata/nginx/timinglee.org/lee/images;}}#重启Nginx并访问测试
#http://www.timinglee.org/logo.PNG #访问失败,系统中没有logo.PNG文件
3.匹配案例-不区分大小写
~* 用来对用户请求的uri做模糊匹配,uri中无论都是大写、都是小写或者大小写混合,此模式也都会匹配,通常使用此模式匹配用户request中的静态资源并继续做下一步操作,此方式使用较多
注意: 此方式中,对于Linux文件系统上的文件仍然是区分大小写的,如果磁盘文件不存在,仍会提示404
server {listen 80;server_name lee.timinglee.org;location / {root /webdata/nginx/timinglee.org/lee/html;}location ~* /logo.PNG {root /webdata/nginx/timinglee.org/lee/images;}}#重启Nginx并访问测试
#http://www.timinglee.org/logo.png
4.匹配案例-URI开始
[root@Nginx ~]# mkdir /webdata/nginx/timinglee.org/lee/images/images{1,2}
[root@Nginx ~]# echo image1 > /webdata/nginx/timinglee.org/lee/images/images1/index.html
[root@Nginx ~]# echo image1 > /webdata/nginx/timinglee.org/lee/images/images2/index.htmlserver {listen 80;server_name lee.timinglee.org;location / {root /webdata/nginx/timinglee.org/lee/html;}location ^~ /images {root /webdata/nginx/timinglee.org/lee/images;index index.html;}location /images1 {root /webdata/nginx/timinglee.org/lee/images;}
}#重启Nginx并访问测试,实现效果是访问/images1和/images2返回内容一样
[root@node100 ~]# curl 172.25.254.200/images1/
image1
[root@node100 ~]# curl 172.25.254.200/images2/
image1
5.匹配案例-文件名后缀
[root@Nginx ~]# mkdir /webdata/nginx/timinglee.org/lee/images
#上传一个图片到/webdata/nginx/timinglee.org/lee/images
server {listen 80;server_name lee.timinglee.org;location / {root /webdata/nginx/timinglee.org/lee/html;}location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js|css)$ {root /webdata/nginx/timinglee.org/lee/images;index index.html;}
}#重启Nginx并访问测试
172.25.254.200/logo.png
6.匹配案例-优先级
bashserver {listen 80;server_name lee.timinglee.org;location / {root /webdata/nginx/timinglee.org/lee/html;}location ^~ /images {root /webdata/nginx/timinglee.org/lee/images;index index.html;}location /images1 {root /webdata/nginx/timinglee.org/lee/images;}location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {root /data/nginx/static3;index index.html;}}
#匹配优先级:=, ^~, ~/~*,/
location优先级:(location =) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 完整路径) > (location 部分起始路径) > (/)
7.生产使用案例
#直接匹配网站根会加速Nginx访问处理
location = /index.html {......;
}location / {......;
}#静态资源配置方法1
location ^~ /static/ {......;
}#静态资源配置方法2,应用较多
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {......;
}#多应用配置
location ~* /app1 {......;
}location ~* /app2 {......;
}
3.4.4 Nginx 账户认证功能
由 ngx_http_auth_basic_module 模块提供此功能
# 创建Nginx认证文件
# -c:当认证文件不存在,则会新建认证文件;存在时,则会清空认证文件;
[root@web-a ~]# htpasswd -cm /usr/local/nginx/conf/.htpasswd admin
New password:
Re-type new password:[root@web-a ~]# htpasswd -mb /usr/local/nginx/conf/.htpasswd cehsi ceshi # -b表示非交互建立用户认证.(第二次建立时记得不要加-c,由于-c可以将其清空)[root@web-a ~]# cat /usr/local/nginx/conf/.htpasswd
admin:$apr1$QnO0g3O5$8uLKBEksYaCumjoUyX1E71
cehsi:$apr1$LpojKcFD$ewDOeBwauK3cptiXsGF1e0[root@web-a ~]# mkdir -p /web/login/[root@web-a ~]# echo login--cehsi_page > /web/login/index.html[root@web-a ~]# vim /usr/local/nginx/conf.d/vhost.conflocation /login/ {root /web/;index index.html;auth_basic "please input username and passwd!!!";auth_basic_user_file /usr/local/nginx/.htpasswd;}[root@web-a ~]# nginx -t
[root@web-a ~]# nginx -s reload[root@web-a ~]# curl www.dhj.org
web_html
[root@web-a ~]# curl www.dhj.org/login[root@web-a ~]# curl -uadmin:admin www.dhj.org/login/

3.4.5 自定义错误页面
自定义错误页,同时也可以用指定的响应状态码进行响应, 可用位置:http, server, location, if in location
error_page code ... [=[response]] uri;
1.示例
listen 80;
server_name www.timinglee.org;
error_page 500 502 503 504 /error.html; # 注意格式(只能用空格来分割;且相邻空格个数为1)
location = /error.html {root /data/nginx/html;
}#重启nginx并访问不存在的页面进行测试
2.示例:自定义错误页面
[root@Nginx ~]# mkdir /webdata/nginx/timinglee/lee/errors -p
[root@Nginx ~]# echo error page > /webdata/nginx/timinglee/lee/errors/40x.htmlserver {listen 80;server_name lee.timinglee.org;error_page 404 /40x.htmllocation = /40x.html {root /webdata/nginx/timinglee/lee/errors;}}[root@web-a ~]# vim /usr/local/nginx/conf.d/vhost.conf
server{listen 80;server_name www.dhj.org;root /web/html;index index.html;error_page 500 502 503 504 404 403 402 401 /errorpage/error.html;location /login/ {root /web/;index index.html;auth_basic "please input username and passwd!!!";auth_basic_user_file /usr/local/nginx/.htpasswd;}location /errorpage/ {root /web/;}
}[root@web-a ~]# nginx -t[root@web-a ~]# mkdir /web/errorpage -p[root@web-a ~]# nginx -s reload[root@web-a ~]# echo "错误啦" > /web/errorpage/error.html[root@web-a ~]# curl www.dhj.org/asdada
错误啦
3.4.6 自定义错误日志
可以自定义错误日志
Syntax: error_log file [level];
Default:
error_log logs/error.log error;
Context: main, http, mail, stream, server, location
level: debug, info, notice, warn, error, crit, alert, emerg
1.示例
[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
server{listen 80;server_name www.dhj.org;root /web/html;index index.html;error_page 500 502 503 504 404 403 402 401 /errorpage/error.html;error_log /usr/local/nginx/logs/err.log; # 111access_log /usr/local/nginx/logs/access.log; # 222location /login/ {root /web/;index index.html;auth_basic "please input username and passwd!!!";auth_basic_user_file /usr/local/nginx/.htpasswd;}location /errorpage/ {root /web/;}
}# 重启nginx并访问不存在的页面进行测试并验证是在指定目录生成新的日志文件
[root@web-a ~]# nginx -s reload[root@web-a ~]# systemctl restart nginx[root@web-a ~]# cd /usr/local/nginx/logs/
[root@web-a logs]# ls
access.log err.log error.log nginx.pid
3.4.7 检测文件是否存在
try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误。
1.语法格式
Syntax: try_files file ... uri;
try_files file ... =code;
Default: —
Context: server, location
2.示例: 如果不存在页面, 就转到default.html页面
[root@web-a ~]# vim /usr/local/nginx/conf.d/vhost.conf
server{listen 80;server_name www.dhj.org;root /web/html;index index.html;error_page 500 502 503 504 404 403 402 401 /errorpage/error.html;error_log /usr/local/nginx/logs/err.log;access_log /usr/local/nginx/logs/access.log;try_files $uri $uri.html $uri/index.html /errorpage/default.html; # 当检测的所有指定文件不存在时,此时访问default.htmllocation /login/ {root /web/;index index.html;auth_basic "please input username and passwd!!!";auth_basic_user_file /usr/local/nginx/.htpasswd;}location /errorpage/ {root /web/;}
}
[root@web-a ~]# nginx -s reload[root@web-a ~]# echo ceshi--default_page > /web/errorpage/default.html[root@web-a ~]# curl www.dhj.org
web_html
[root@web-a ~]# curl www.dhj.org/asd
ceshi--default_page
3.4.8 长连接配置
keepalive_timeout timeout [header_timeout]; # 设定保持连接超时时长,0表示禁止长连接,默认为75s# 通常配置在http字段作为站点全局配置keepalive_requests 数字; # 在一次长连接上所允许请求的资源的最大数量# 默认为100次,建议适当调大,比如:500
1.示例
keepalive_requests 3;
keepalive_timeout 65 60;
#开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,第二个数字60为发送给客户端应答报文头部中显示的超时时间设置为60s:如不设置客户端将不显示超时时间。Keep-Alive:timeout=60 #浏览器收到的服务器返回的报文#如果设置为0表示关闭会话保持功能,将如下显示:#Connection:close 浏览器收到的服务器返回的报文#使用命令测试:
[root@web-a ~]# telnet www.dhj.org 80
Trying 172.25.254.10...
Connected to www.dhj.org.
Escape character is '^]'.
GET / HTTP/1.1
HOST: www.dhj.orgHTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sat, 26 Jul 2025 03:39:41 GMT
Content-Type: text/html
Content-Length: 9
Last-Modified: Thu, 24 Jul 2025 08:07:45 GMT
Connection: keep-alive
ETag: "6881e9d1-9"
Accept-Ranges: bytesweb_html


3.4.9 作为下载服务器配置
ngx_http_autoindex_module 模块处理以斜杠字符 “/” 结尾的请求,并生成目录列表,可以做为下载服务配置使用
1.相关指令
autoindex on | off; #自动文件索引功能,默为offautoindex_exact_size on | off; #计算文件确切大小(单位bytes),off 显示大概大小(单位K、M),默认onautoindex_localtime on | off ; #显示本机时间而非GMT(格林威治)时间,默认offautoindex_format html | xml | json | jsonp; #显示索引的页面文件风格,默认htmllimit_rate rate; #限制响应客户端传输速率(除GET和HEAD以外的所有方法),单位B/s,bytes/second, #默认值0,表示无限制,此指令由ngx_http_core_module提供
set $limit_rate 4k; #也可以通变量限速,单位B/s,同时设置,此项优级高.
2.示例:实现下载站点
#注意:download不需要index.html文件
[root@web-a ~]# vim /usr/local/nginx/conf.d/vhost.conf
server{listen 80;server_name www.dhj.org;root /web/html;index index.html;error_page 500 502 503 504 404 403 402 401 /errorpage/error.html;error_log /usr/local/nginx/logs/err.log;access_log /usr/local/nginx/logs/access.log;try_files $uri $uri.html $uri/index.html /errorpage/default.html;location /login/ {root /web/;index index.html;auth_basic "please input username and passwd!!!";auth_basic_user_file /usr/local/nginx/.htpasswd;}location /errorpage/ {root /web/;}location /download/ {root /web/;autoindex on; # 自动索引功能autoindex_exact_size on; # 计算文件确切大小(单位bytes),此为默认值,off只显示大概大小(单位kb、mb、gb)autoindex_localtime on; # on表示显示本机时间而非GMT(格林威治)时间,默为为off显示GMT时间limit_rate 1024k; # 限速,默认不限速}
}[root@web-a ~]# nginx -s reload
[root@web-a ~]# curl www.dhj.org/download/
# 重启Nginx并访问测试下载页面