网站首页 > 技术教程 正文
- 上一篇:ailx10:Nginx基础入门001 ;
- 下一篇:ailx10:Nginx基础入门003 ;
ailx10
网络安全优秀回答者
网络安全硕士
Nginx为配置一个完整的静态Web服务器提供了8类配置项,对应ngx_http_core_module处理模块:
- 虚拟主机与请求的分发
- 文件路径的定义
- 内存及磁盘资源的分配
- 网络连接的设置
- MIME类型的设置
- 对客户端请求的限制
- 文件操作的优化
- 对客户端请求的特殊处理
1 虚拟主机与请求的分发
每个server块就是一个虚拟主机
1.1 监听端口
- 语法:listen address:port;(默认:listen 80;)
- 配置块:server
1.2 主机名称
- 语法:server_name name [...];(默认:server_name "";)
- 配置块:server
server_name与HTTP报文中HOST字段的匹配优先级:
- 首先选择所有字符串完全匹配的server_name,比如www.ailx10.com
- 其次选择通配符在前面的server_name,比如*.ailx10.com
- 再其次选择通配符在后面的server_name,比如www.aixl10.*
- 最后选择使用正则表达式才匹配的server_name,比如~^\.ailx10\.com$
- 如果都不匹配,走listen配置项中加入default的server块
- 如果还不匹配,走匹配到listen端口的第一个server块
1.3 server_names_hash_bucket_size
- 语法:server_names_hash_bucket_size size;(默认:32|64|128)
- 配置项:http、server、location
为了提高快速寻找到相应server name的能力,Nginx使用散列表来存储server name,server_names_hash_bucket_size设置每个散列桶占用的内存大小。
1.4 server_names_hash_max_size
- 语法:server_names_hash_max_size size;(默认:512)
- 配置项:http、server、location
server_names_hash_max_size会影响散列表的冲突率,server_names_hash_max_size越大消耗的内存就越大,但散列key的冲突率会降低,检索速度更快。
1.5 重定向主机名称的处理
- 语法:server_name_in_redirect on|off;(默认:on)
- 配置块:http、server或location
需要配合server_name使用,表示在重定向请求时会使用server_name里配置的第一个主机名代替原先请求中的Host头部,而使用off时,表示在重定向请求中是哟经请求本身的Host头部。
1.6 location
- 语法:location [=|~|~*|^~|@] /uri/ {...}
- 配置块:server
location会尝试根据用户请求中的URI来匹配上面的/uri表达式,如果可以匹配,就选择location{}块中的配置来处理用户请求。
- =表示把URI作为字符串,以便于参数中的uri做完全匹配
- ~表示匹配URI时是字母大小写敏感的
- ~*表示匹配URI时忽略字母大小写问题
- ^~表示匹配URI时只需要其前半部分与uri参数匹配即可
- @表示仅用于Nginx服务内部请求之间的重定向,不直接处理用户请求
- 支持正则表达式
当一个请求匹配多个location时,实际上这个请求会被第一个location处理。一般把location /作为最后一条,用于匹配所有的请求。
location / {
# /可以匹配所有请求
}
2 文件路径的定义
2.1 以root方式设置资源路径
- 语法:root path;
- 默认:root html;
- 配置块:http、server、location、if
location /download/ {
root /opt/web/html/;
}
如果有一个请求的URI是/download/index/ailx10.html,那么服务器将返回/opt/web/html/download/index/ailx10.html文件中的内容。
2.2 以alias方式设置资源路径
- 语法:alias path;
- 配置块:location
location /conf {
alias /usr/local/nginx/conf/;
}
如果一个请求的URI是/conf/ailx10.html,那么服务器将返回/usr/local/nginx/conf/ailx10.html文件中的内容。
推荐使用root方式
2.3 访问首页
- 语法:index file ...;
- 默认:indedx index.html;
- 配置块:http、server、location
location / {
root path;
index /index.html /html/index.php /index.php;
}
如果一个请求的URI是/,Nginx首先会尝试访问path/index.php文件,如果可以访问,就直接返回文件内容结束请求,否则再尝试返回path/html/index.php文件中的内容,以此类推。
2.4 根据HTTP返回码重定向页面
- 语法:error_page code [code...][=|=answer-code] uri | @named_location
- 配置块:http、server、location、if
error_page 404 /404.html;
error_page 502 503 504 /50x.html;
error_page 403 http://ailx10.com/forbidden.html;
error_page 404 =@fetch;
虽然重定向了URI,但返回的HTTP错误码还是原来的,用户可以通过=来更改返回的错误码。
error_page 404 =200 /empty.gif;
error_page 404 =403 /forbidden.gif;
2.5 是否允许递归使用error_page
- 语法:recursive_error_pages [on|off];
- 默认:recursive_error_pages off;
- 配置块:http、server、location
2.6 try_files
- 语法:try_files path1 [path2] uri;
- 配置块:server、location
按照顺序依次访问每一个path,直到有一个可以访问为止。
3 内存及磁盘资源的分配
3.1 HTTP包体只存储到磁盘文件中
- 语法:client_body_in_file_only on | clean | off;
- 默认:client_body_in_file_only off;
- 配置块:http、server、location
3.2 HTTP包体尽量写入到一个内存buffer中
- 语法:client_body_in_single_buffer on | off;
- 默认:client_body_in_single_buffer off;
- 配置块:http、server、location
3.3 存储HTTP头部的内存buffer大小
- 语法:client_header_buffer_size size;
- 默认:client_header_buffer_size 1k;
- 配置块:http、server
3.4 存储超大HTTP头部的内存buffer大小
- 语法:large_client_header_buffers number size;
- 默认:large_client_header_buffers 4 8k;
- 配置块:http、server
3.5 存储HTTP包体的内存buffer大小
- 语法:client_body_buffer_size size;
- 默认:client_body_buffer_size 8k/16k;
- 配置块:http、server、location
3.6 HTTP包体的临时存放目录
- 语法:client_body_temp_path dir-path [level1 [level2 [level3]]]
- 默认:client_body_temp_path client_body_temp;
- 配置块:http、server、location
3.7 connection_pool_size
- 语法:connection_pool_size szie;
- 默认:connection_pool_size 256;
- 配置块:http、server
3.8 request_pool_size
- 语法:request_pool_size size;
- 默认:request_pool_size 4k;
- 配置块:http、server
TCP连接关闭时会销毁connection_pool_size指定的连接内存池
HTTP请求结束时会销毁request_pool_size指定的HTTP请求内存池
一个TCP连接可能被复用多个HTTP请求
4 网络连接的设置
4.1 读取HTTP头部的超时时间
- 语法:client_header_timeout time
- 默认:client_header_timeout 60;
- 配置块:http、server、location
4.2 读取HTTP包体的超时时间
- 语法:client_body_timeout time
- 默认:client_body_timeout 60;
- 配置块:http、server、location
4.3 发送响应的超时时间
- 语法:send_timeout time;
- 默认:send_timeout 60;
- 配置块:http、server、location
4.4 reset_timeout_connection
- 语法:reset_timeout_connection on | off;
- 默认:reset_timeout_connection off;
- 配置块:http、server、location
启动这个选项后,超时后会向客户端发送RST重置包,使得服务器避免产生许多处于FIN_WAIT1、FIN_WAIT2、TIME_WAIT状态的TCP连接。
4.5 lingering_close
- 语法:lingering_close off | on | always;
- 默认:lingering_close on;
- 配置块:http、server、location
控制Nginx关闭用户连接的方式:
- always:关闭用户连接前必须无条件处理连接上所有用户发送的数据
- off:关闭连接时完全不管连接上是否已经有准备就绪的数据
- on:有选择的处理连接上的用户数据
4.6 lingering_time
- 语法:lingering_time time;
- 默认:lingering_time 30s;
- 配置块:http、server、location
经过lingering_time设置的时间后,Nginx不管用户是否仍在上传文件,都会把连接关闭。
4.7 lingering_timeout
- 语法:lingering_timeout time;
- 默认:lingering_timeout 5s;
- 配置块:http、server、location
4.8 对某些浏览器禁用keepalive功能
- 语法:keepalive_disable [msie6 | safari | noe]...
- 默认:keepalive_disable msi6 safari
- 配置块:http、server、location
HTTP请求中的keepalive功能是为了让多个请求复用一个HTTP长连接。
4.9 keepalive超时时间
- 语法:keepalive_timeout time
- 默认:keepalive_timeout 75;
- 配置块:http、server、location
keepalive连接在闲置超过一定时间后(默认75秒),服务器和浏览器都会去关闭这个连接。
4.10 一个keepalive长连接上允许承载的请求最大数
- 语法:keepalive_requests n;
- 默认:keepalive_requests 100;
- 配置块:http、server、location
4.11 tcp_nodelay
- 语法:tcp_nodelay on | off;
- 默认:tcp_nodelay off;
- 配置块:http、server、location
确定对keepalive连接是否使用TCP_NODELAY选项。
4.12 tcp_nopush
- 语法:tcp_nopush on | off;
- 默认:tcp_nopush off;
- 配置块:http、server、location
5 MIME类型的设置
5.1 MIME type与文件扩展的映射
- 语法:type {...};
- 配置块:http、server、location
定义MIME type到文件扩展名的映射,多个扩展名可以映射到一个MIME type。
types {
text/html html;
text/html conf;
image/gif gif;
image/jpeg jpg;
}
5.2 默认MIME type
- 语法:default_type MIME-type;
- 默认:default_type text/plain;
- 配置块:http、server、location
当找不到相应的MIME type与文件扩展名之间的映射时,使用默认的MIME-type作为HTTP header中的Content-Type。
5.3 types_hash_bucket_size
- 语法:types_hash_bucket_size size;
- 默认:types_hash_bucket_size 32|64|128;
- 配置块:http、server、location
为了快速寻找到相应的MIME type,Nginx使用散列表来存储MIME type与文件扩展名,types_hash_bucket_size设置了每个散列桶占用的内存大小。
5.4 types_hash_max_size
- 语法:types_hash_max_size size;
- 默认:types_hash_max_size 1024;
- 配置块:http、server、location
6 对客户端请求的限制
6.1 按HTTP方法名限制用户请求
- 语法:limit_except method ... {...}
- 配置块:location
Nginx通过limit_except后面指定的方法来限制用户请求。方法名包括:get、head、post、put、delete、mkcol、copy、move、options、propfind、proppatch、lock、unlock、patch。
limit_except GET {
allow 192.168.0.1/32;
deny all;
}
6.2 HTTP请求包体的最大值
- 语法:client_max_body_size size;
- 默认:client_max_body_size 1m;
- 配置块:http、server、location
限制HTTP的Content-Length字段值得大小。
6.3 对请求的限速
- 语法:limit_rate speed;
- 默认:limit_rate 0;
- 配置块:http、server、location、if
限制客户端每秒传输的字节数,0表示不限速。
6.4 limit_rate_after
- 语法:limit_rate_after time;
- 默认:limit_rate_after 1m;
- 配置块:http、server、location、if
Nginx向客户端发送的响应长度超过limit_rate_after 后才开始限速。
猜你喜欢
- 2024-10-10 Nginx配置文件中限制(nginx限制请求大小)
- 2024-09-11 nginx源码剖析—nginx进程模型(nginx进程nobody)
- 2024-09-11 Nginx+keepalived实现高可用(nginx的keepalive)
- 2024-09-11 几种Python检测Nginx的连接数的方法
- 2024-09-11 Nginx系列:负载均衡(nginx负载均衡实现原理)
- 2024-09-11 nginx.conf配置文件详解(nginx. conf)
- 2024-09-11 Nginx 备忘录 - 05. 日志配置(nginx 日志文件)
- 2024-09-11 Python自动化脚本管理Nginx(python自动化脚本工具)
- 2024-09-11 Nginx配置详解(nginx配置cgi)
- 2024-09-11 nginx配置之nginx中的“防盗”配置
你 发表评论:
欢迎- 最近发表
-
- Win11学院:如何在Windows 11上使用WSL安装Ubuntu
- linux移植(Linux移植freemodbus)
- 独家解读:Win10预览版9879为何无法识别硬盘
- 基于Linux系统的本地Yum源搭建与配置(ISO方式、RPM方式)
- Docker镜像瘦身(docker 减小镜像大小)
- 在linux上安装ollama(linux安装locale)
- 渗透测试系统Kali推出Docker镜像(kali linux渗透测试技术详解pdf)
- Linux环境中部署Harbor私有镜像仓库
- linux之间传文件命令之Rsync傻瓜式教程
- 解决ollama在linux中安装或升级时,通过国内镜像缩短安装时长
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)