nginx
nginx
Http协议和 nginx
HTTP 超文本传输协议
HTTP 相关概念
互联网:是网络,是所有类型网络的母集。
因特网:世界上最大的互联网网络。即因特网概念从属于互联网概念。习惯上,大家把连接在因特网上的计算机都成为主机。
万维网:WWW(world wide web)万维网并非某种特殊的计算机网络,是一个大规模的、联机式的信息贮藏库,使用链接的方法能非常方便地从因特网上的一个站点访问另一个站点(超链技术),具有提供分布式服务的特点。万维网是一个分布式的超媒体系统,是超文本系统的扩充,基于B/S架构实现
URL:万维网使用统一资源定位符(Uniform Resource Locator)来标志万维网上的各种文档,并使每个文档在整个因特网的范围内具有唯一的标识符URL。
HTTP:为解决"用什么样的网络协议来实现整个因特网上的万维网文档”这一难题,就要使万维网客户程序(以浏览器为主,但不限于浏览器)与万维网服务器程序之间的交互遵守严格的协议,即超文本传送协议(HyperText Transfer Protocol)。HTTP是处于应用层的协议,使用TCP传输层协议进行可靠的传送。因此,需要特别提醒的是,万维网是基于因特网的一种广泛因特网应用系统,且万维网采用的是HTTP(80/TCP)和 HTTPS(443/TCP)的传输协议,但因特网还有其他的网络应用系统(如:FTP、SMTP等等)。
HTML:为了解决"怎样使不同作者创作的不同风格的万维网文档,都能在因特网上的各种主机上显示出来,同时使用户清楚地知道在什么地方存在着链接”这一问题,万维网使用超文本标记语言(HyperText Markup Language),使得万维网页面的设计者可以很方便地用链接从页面的某处链接到因特网的任何一个万维网页面,并且能够在自己的主机品目上将这些页面显示出来。HTML与txt一样,仅仅是是一种文档,不同之处在于,这种文档专供于浏览器上为浏览器用户提供统一的界面呈现的统一规约。且具备结构化的特征,这是txt所不具备的强制规定。
HTTP 协议通信过程
HTTP(HyperText Transfer Protocol,超文本传输协议)是一种用于分布式、协作式和超媒体信息系统的应用层协议。HTTP是万维网的数据通信的基础设计HTTP最初的目的是为了提供一种远距离共享知识的方式,借助多文档进行关联实现超文本,连成相互参阅的WWW(world wide web,万维网)HTTP的发展是由蒂姆·伯纳斯-李(Tim Berners-Lee)于1989年在欧洲核子研究组织(CERN)所发起。HTTP的标准制定由万维网协会(World Wide Web Consortium,W3C)和互联网工程任务组(Internet Engineering Task Force,IETF)进行协调,最终发布了一系列的RFC,其中最著名的是1999年6月公布的 RFC 2616,定义了HTTP协议中现今广泛使用的一个版本——HTTP 1.1版
HTTP****服务通信过程
HTTP协议分层
HTTP 相关技术
WEB 开发语言
http:Hyper Text Transfer Protocol 应用层协议,默认端口: 80/tcp
WEB前端开发语言:
- html
- css
- javascript
html
Hyper Text Markup Language 超文本标记语言,编程语言,主要负责实现页面的结构
范例:html 语言
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>HTML语言</title>
</head>
<body>
<img src="http://www.kgc.com/wp-content/uploads/2017/09/logo.png" >
<h1 style="color:red">欢迎</h1>
<p><a href=http://www.kgc.com>课工场</a>欢迎你</p>
</body>
</html>
#注意:html文件编码为utf-8编码
CSS
Cascading Style Sheet 层叠样式表, 定义了如何显示(装扮) HTML 元素,比如:字体大小和颜色属性等。样式通常保存在外部的 .css 文件中,用于存放一些HTML文件的公共属性,从而通过仅编辑一个简单的 CSS 文档,可以同时改变站点中所有页面的布局和外观。
范例 :CSS
#test.html 建议用Vscode创建文件,用记事本可能会出现乱码
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
<body>
<h1>这是 heading 1</h1>
<p>这是一段普通的段落。请注意,该段落的文本是红色的。在 body 选择器中定义了本页面中的默认文本颜
色。</p>
<p class="ex">该段落定义了 class="ex"。该段落中的文本是蓝色的。</p>
</body>
</html>
#mystyle.css
body {color:red}
h1 {color:#00ff00}
p.ex {color:rgb(0,0,255)}
Js
javascript,实现网页的动画效果,但实属于静态资源
,Java和javascript的关系: 周杰和周杰伦的关系
范例:javascript
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
</head>
<body>
<h2>我的第一个 JavaScript</h2>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
点击这里来显示日期和时间
</button>
<p id="demo"></p>
</body>
</html>
MIME
MIME : Multipurpose Internet Mail Extensions 多用途互联网邮件扩展
文件 /etc/mime.types ,来自于mailcap包
MIME格式:type/subtype
范例:
text/plain
text/html
text/css
image/jpeg jpg jpeg
image/png
video/mp4
application/javascript
URI urn url
URI: Uniform Resource Identifier 统一资源标识,分为URL 和 URN
URN:Uniform Resource Naming,统一资源命名
示例: P2P下载使用的磁力链接是URN的一种实现
magnet:?xt=urn:btih:660557A6890EF888666
URL:Uniform Resorce Locator,统一资源定位符,用于描述某服务器某特定资源位置
两者区别:URN如同一个人的名称,而URL代表一个人的住址。换言之,URN定义某事物的身份,而
URL提供查找该事物的方法。URN仅用于命名,而不指定地址
URL组成
<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>
www.baidu.com/test?name=test
scheme:方案,访问服务器以获取资源时要使用哪种协议
user:用户,某些方案访问资源时需要的用户名
password:密码,用户对应的密码,中间用:分隔
Host:主机,资源宿主服务器的主机名或IP地址
port:端口,资源宿主服务器正在监听的端口号,很多方案有默认端口号
path:路径,服务器资源的本地名,由一个/将其与前面的URL组件分隔
params:参数,指定输入的参数,参数为名/值对,多个参数,用;分隔
query:查询,传递参数给程序,如数据库,用?分隔,多个查询用&分隔
frag:片段,一小片或一部分资源的名字,此组件在客户端使用,用#分隔
网站的访问量
网站访问量统计的重要指标
- IP(独立IP):即Internet Protocol,指独立IP数。一天内来自相同客户机IP 地址只计算一次,记录远程客户机IP地址的计算机访问网站的次数,是衡量网站流量的重要指标
- PV(访问量): 即Page View, 页面浏览量或点击量,用户每次刷新即被计算一次,PV反映的是浏览某网站的页面数,PV与来访者的数量成正比,PV并不是页面的来访者数量,而是网站被访问的页面数量
- UV(独立访客):即Unique Visitor,访问网站的一台电脑为一个访客。一天内相同的客户端只被计算一次。可以理解成访问某网站的电脑的数量。网站判断来访电脑的身份是通过cookies实现的。如果更换了IP后但不清除cookies,再访问相同网站,该网站的统计中UV数是不变的
网站统计:http://www.alexa.cn/rank/
网站访问量
QPS:request per second,每秒请求数
PV,QPS和并发连接数换算公式
- QPS= PV * 页面衍生连接次数/ 统计时间(86400)
- 并发连接数 =QPS * http平均响应时间
峰值时间:每天80%的访问集中在20%的时间里,这20%时间为峰值时间
峰值时间每秒请求数(QPS)=( 总PV数 页面衍生连接次数)80% ) / ( 每天秒数 * 20% )
HTTP工作机制
一次http事务包括:
- http请求:http request
- http响应:http response
Web资源:web resource, 一个网页由多个资源(文件)构成,打开一个页面,通常会有多个资源展示出来,但是每个资源都要单独请求。因此,一个"Web 页面”通常并不是单个资源,而是一组资源的集合
资源类型:
- 静态文件:无需服务端做出额外处理,服务器端和客户端的文件内容相同
文件后缀:.html, .txt, .jpg, .js, .css, .mp3, .avi
- 动态文件:服务端执行程序,返回执行的结果,服务器端和客户端的文件内容不相同
文件后缀:.php, .jsp ,.asp
提高HTTP连接性能
- 并行连接:通过多条TCP连接发起并发的HTTP请求
- 持久连接:keep-alive,重用TCP连接,以消除连接和关闭的时延,以事务个数和时间来决定是否关闭连接
- 管道化连接:通过共享TCP连接,发起并发的HTTP请求
- 复用的连接:交替传送请求和响应报文(实验阶段)
串行和并行
管道
提高HTTP连接性能
- 并行连接:通过多条TCP连接发起并发的HTTP请求
- 持久连接:keep-alive,重用TCP连接,以消除连接和关闭的时延,以事务个数和时间来决定是否关闭连接
- 管道化连接:通过共享TCP连接,发起并发的HTTP请求
- 复用的连接:交替传送请求和响应报文(实验阶段)
HTTP 协议版本
RFC Hypertext Transfer Protocol -- HTTP/1.1
https://tools.ietf.org/html/rfc2616
http/0.9:
1991,原型版本,功能简陋,只有一个命令GET。GET /index.html ,服务器只能回应HTML格式字符串,不能回应别的格式
http/1.0
1996年5月,支持cache, MIME, method
每个TCP连接只能发送一个请求,发送数据完毕,连接就关闭,如果还要请求其他资源,就必须再新建一个连接引入了POST命令和HEAD命令头信息是 ASCII 码,后面数据可为任何格式。服务器回应时会告诉客户端,数据是什么格式,即Content-Type字段的作用。这些数据类型总称为MIME 多用途互联网邮件扩展,每个值包括一级类型和二级类型,预定义的类型,也可自定义类型, 常见Content-Type值:text/xml image/jpeg audio/mp3
http/1.1
1997年1月,引入了持久连接(persistent connection),即TCP连接默认不关闭,可以被多个请求复用,不用声明Connection: keep-alive。对于同一个域名,大多数浏览器允许同时建立6个持久连接引入了管道机制,即在同一个TCP连接里,客户端可以同时发送多个请求,进一步改进了HTTP协议的效率
新增方法:PUT、PATCH、OPTIONS、DELETE
同一个TCP连接里,所有的数据通信是按次序进行的。服务器只能顺序处理回应,前面的回应慢,会有许多请求排队,造成"队头堵塞"(Head-of-line blocking)为避免上述问题,
两种方法:一是减少请求数,二是同时多开持久连接。
网页优化技巧,如合并脚本和样式表、将图片嵌入CSS代码、域名分片(domain sharding)等
HTTP 协议不带有状态,每次请求都必须附上所有信息。请求的很多字段都是重复的,浪费带宽,影响速度
HTTP1.0和HTTP1.1的区别
- 缓存处理,在HTTP1.0中主要使用header里的If-Modified-Since,Expires来做为缓存判断的标准,HTTP1.1则引入了更多的缓存控制策略例如Entity tag,If-Unmodified-Since, If-Match, If-NoneMatch等更多可供选择的缓存头来控制缓存策略
- 带宽优化及网络连接的使用,HTTP1.0中,存在一些浪费带宽的现象,例如:客户端只是需要某个对象的一部分,而服务器却将整个对象送过来了,并且不支持断点续传功能,HTTP1.1则在请求头引入了range头域,它允许只请求资源的某个部分,即返回码是206(Partial Content),方便了开发者自由的选择以便于充分利用带宽和连接
- 错误通知的管理,在HTTP1.1中新增24个状态响应码,如409(Conflict)表示请求的资源与资源当前状态冲突;410(Gone)表示服务器上的某个资源被永久性的删除
- Host 头处理,在HTTP1.0中认为每台服务器都绑定一个唯一的IP地址,因此,请求消息中的URL并没有传递主机名(hostname)。但随着虚拟主机技术的发展,在一台物理服务器上可以存在多个虚拟主机(Multi-homed Web Servers),并且它们共享一个IP地址。HTTP1.1的请求消息和响应消息都应支持Host头域,且请求消息中如果没有Host头域会报告一个错误(400 Bad Request)
- 长连接,HTTP 1.1支持持久连接(PersistentConnection)和请求的流水线(Pipelining)处理,在一个TCP连接上可以传送多个HTTP请求和响应,减少了建立和关闭连接的消耗和延迟,在HTTP1.1中默认开启Connection: keep-alive,弥补了HTTP1.0每次请求都要创建连接的缺点
HTTP1.0和1.1的问题
- HTTP1.x在传输数据时,每次都需要重新建立连接,无疑增加了大量的延迟时间,特别是在移动端更为突出
- HTTP1.x在传输数据时,所有传输的内容都是明文,客户端和服务器端都无法验证对方的身份,无法保证数据的安全性
- HTTP1.x在使用时,header里携带的内容过大,增加了传输的成本,并且每次请求header基本不怎么变化,尤其在移动端增加用户流量
- 虽然HTTP1.x支持了keep-alive,来弥补多次创建连接产生的延迟,但是keep-alive使用多了同样会给服务端带来大量的性能压力,并且对于单个文件被不断请求的服务(例如图片存放网站),keep-alive可能会极大的影响性能,因为它在文件被请求之后还保持了不必要的连接很长时间
HTTPS协议:
为解决安全问题,网景在1994年创建了HTTPS,并应用在网景导航者浏览器中。 最初,HTTP是与SSL一起使用的;在SSL逐渐演变到TLS时(其实两个是一个东西,只是名字不同而已),最新的HTTPS也由在2000年五月公布的RFC 2818正式确定下来。HTTPS就是安全版的HTTP,目前大型网站基本实现全站HTTPS
HTTPS特点
- HTTPS协议需要到CA申请证书,一般免费证书很少,需要交费
- HTTP协议运行在TCP之上,所有传输的内容都是明文,HTTPS运行在SSL/TLS之上,SSL/TLS运行
- 在TCP之上,所有传输的内容都经过加密的
- HTTP和HTTPS使用的是不同的连接方式,端口不同,前者是80,后者是443HTTPS可以有效的防止运营商劫持,解决了防劫持的一个大问题
- HTTPS 实现过程降低用户访问速度,但经过合理优化和部署,HTTPS 对速度的影响还是可以接受的
http请求处理过程
- 建立连接
- 接收请求
- 处理请求
- 访问资源
- 构建响应报文
- 发送响应报文
- 记录日志
一次完整的http请求处理过程
1、建立连接:接收或拒绝连接请求
2、接收请求:接收客户端请求报文中对某资源的一次请求的过程
- 单进程I/O模型:启动一个进程处理用户请求,而且一次只处理一个,多个请求被串行响应
- 多进程I/O模型:并行启动多个进程,每个进程响应一个连接请求
- 复用I/O结构:启动一个进程,同时响应N个连接请求
- 复用的多进程I/O模型:启动M个进程,每个进程响应N个连接请求,同时接收M*N个请求
3、处理请求:服务器对请求报文进行解析,并获取请求的资源及请求方法等相关信息,根据方法,资源,首部和可选的主体部分对请求进行处理常用请求Method: GET、POST、HEAD、PUT、DELETE、TRACE、OPTIONS
4、访问资源:服务器获取请求报文中请求的资源web服务器,即存放了web资源的服务器,负责向请求者提供对方请求的静态资源,或动态运行后生成的资源
5、构建响应报文:
一旦Web服务器识别除了资源,就执行请求方法中描述的动作,并返回响应报文。响应报文中 包含有响应状态码、响应首部,如果生成了响应主体的话,还包括响应主体
1)响应实体:如果事务处理产生了响应主体,就将内容放在响应报文中回送过去。响应报文中通常包括:
描述了响应主体MIME类型的Content-Type首部
描述了响应主体长度的Content-Length
实际报文的主体内容
2)URL重定向:web服务构建的响应并非客户端请求的资源,而是资源另外一个访问路径
3)MIME类型: Web服务器要负责确定响应主体的MIME类型。多种配置服务器的方法可将MIME类型与资源管理起来
魔法分类:Apache web服务器可以扫描每个资源的内容,并将其与一个已知模式表(被称为魔法文件)进行匹配,以决定每个文件的MIME类型。这样做可能比较慢,但很方便,尤其是文件没有标准扩展名时
显式分类:可以对Web服务器进行配置,使其不考虑文件的扩展名或内容,强制特定文件或目录内容拥有某个MIME类型
类型协商: 有些Web服务器经过配置,可以以多种文档格式来存储资源。在这种情况下,可以配置Web服务器,使其可以通过与用户的协商来决定使用哪种格式(及相关的MIME类型)"最好"
6、发送响应报文
Web服务器通过连接发送数据时也会面临与接收数据一样的问题。服务器可能有很多条到各个客户端的连接,有些是空闲的,有些在向服务器发送数据,还有一些在向客户端回送响应数据。服务器要记录连接的状态,还要特别注意对持久连接的处理。对非持久连接而言,服务器应该在发送了整条报文之后,关闭自己这一端的连接。对持久连接来说,连接可能仍保持打开状态,在这种情况下,服务器要正确地计算Content-Length首部,不然客户端就无法知道响应什么时候结束
7、记录日志
最后,当事务结束时,Web服务器会在日志文件中添加一个条目,来描述已执行的事务
常见 http 服务器程序
-
httpd apache,存在C10K(10K connections)问题
-
nginx 解决C10K问题lighttpd
-
IIS .asp 应用程序服务器
-
tomcat .jsp 应用程序服务器
-
jetty 开源的servlet容器,基于Java的web容器
-
Resin CAUCHO公司,支持servlets和jsp的引擎
-
webshpere:IBM公司
-
weblogic:BEA,Oracle
-
jboss:RedHat,IBM
-
oc4j:Oracle
Apache 经典的 Web 服务端
Apache起初由美国的伊利诺伊大学香槟分校的国家超级计算机应用中心开发,目前经历了两大版本分别是1.X和2.X,其可以通过编译安装实现特定的功能
1.2.1.1 Apache prefork 模型
预派生模式,有一个主控制进程,然后生成多个子进程,使用select模型,最大并发1024,每个子进程有一个独立的线程响应用户请求,相对比较占用内存,但是比较稳定,可以设置最大和最小进程数,是最古老的一种模式,也是最稳定的模式,适用于访问量不是很大的场景。
优点:稳定
缺点:每个用户请求需要对应开启一个进程,占用资源较多,并发性差,不适用于高并发场景
一种多进程和多线程混合的模型,有一个控制进程,启动多个子进程,每个子进程里面包含固定的线程,使用线程程来处理请求,当线程不够使用的时候会再启动一个新的子进程,然后在进程里面再启动线程处理请求,由于其使用了线程处理请求,因此可以承受更高的并发。
优点:相比prefork 占用的内存较少,可以同时处理更多的请求
缺点:使用keepalive的长连接方式,某个线程会一直被占据,即使没有传输数据,也需要一直等待到超时才会被释放。如果过多的线程,被这样占据,也会导致在高并发场景下的无服务线程可用。(该问题在prefork模式下,同样会发生)
Apache event****模型
Apache中最新的模式,2012年发布的apache 2.4.X系列正式支持event 模型,属于事件驱动模型(epoll),每个进程响应多个请求,在现在版本里的已经是稳定可用的模式。它和worker模式很像,最大的区别在于,它解决了keepalive场景下,长期被占用的线程的资源浪费问题(某些线程因为被keepalive,空挂在哪里等待,中间几乎没有请求过来,甚至等到超时)。event MPM中,会有一个专门的线程来管理这些keepalive类型的线程,当有真实请求过来的时候,将请求传递给服务线程,执行毕后,又允许它释放。这样增强了高并发场景下的请求处理能力。
优点:单线程响应多请求,占据更少的内存,高并发下表现更优秀,会有一个专门的线程来管理keepalive类型的线程,当有真实请求过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放
缺点:没有线程安全控制
Nginx-高性能的 Web服务端
Nginx是由1994年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯rambler.ru公司开发的,开发工作最早从2002年开始,第一次公开发布时间是2004年10月4日,版本号是0.1.02019年3月11日F5 与 NGINX达成协议,F5 将收购 NGINX 的所有已发行股票,总价值约为 6.7 亿美元。6.7亿美金约合44.97亿人民币,nginx核心模块代码长度198430(包括空格、注释),所以一行代码约为2.6万人民币
官网地址 www.nginx.org
Nginx历经十几年的迭代更新(https://nginx.org/en/CHANGES), 目前功能已经非常完善且运行稳定,另外Nginx的版本分为开发版、稳定版和过期版,nginx以功能丰富著称,它即可以作为http服务器,也可以作为反向代理服务器或者邮件服务器,能够快速的响应静态网页的请求,支持FastCGI/SSL/Virtual Host/URL Rwrite/Gzip/HTTP Basic Auth/http或者TCP的负载均衡(1.9版本以上且开启stream模块)等功能,并且支持第三方的功能扩展。天猫 淘宝 京东 小米 163 新浪等一线互联网公司都在用Nginx或者进行二次开发
基于Nginx的工作场景
正向代理: 代理的客户端
反向代理: 代理的服务端
影响用户体验的因素
#客户端
客户端硬件配置
客户端网络速率
客户端与服务端距离
#服务器
服务端网络速率
服务端硬件配置
服务端架构设计
服务端应用程序工作模式
服务端并发数量
服务端响应文件大小及数量 buffer cache
服务端I/O压力
Input Output 模型
I/O在计算机中指Input/Output, IOPS (Input/Output Per Second)即每秒的输入输出量(或读写次数),是衡量磁盘性能的主要指标之一。IOPS是指单位时间内系统能处理的I/O请求数量,一般以每秒处理的I/O请求数量为单位,I/O请求通常为读或写数据操作请求。一次完整的I/O是用户空间的进程数据与内核空间的内核数据的报文的完整交换,但是由于内核空间与用户空间是严格隔离的,所以其数据交换过程中不能由用户空间的进程直接调用内核空间的内存数据,而是需要经历一次从内核空间中的内存数据copy到用户空间的进程内存当中,所以简单说I/O就是把数据从内核空间中的内存数据复制到用户空间中进程的内存当中。
Linux 的 I/O
- 磁盘I/O 磁盘I/O是进程向内核发起系统调用,请求磁盘上的某个资源比如是html 文件或者图片,然后内核通过相应的驱动程序将目标文件加载到内核的内存空间,加载完成之后把数据从内核内存再复制给进程内存,如果是比较大的数据也需要等待时间
- 网络I/O : 一切皆文件,本质为对socket文件的读写 网络通信就是网络协议栈到用户空间进程的IO就是网络IO
当用户 发起 http 请求需要请求一个index.html 网页文件
客户端请求与服务器端 建立连接,建立连接后, 会发送请求报文
服务端的网卡收到请求报文, 会将该报文复制到 内核空间, 内核空间分析报文后交给对应的程序
nginx 分析该报文, 将报文和自己的配置文件,一一比对,按照配置文件完成请求, 分析后发现 客户需要 index.html
由于 程序的权限问题, 没有资格直接调用磁盘上的文件,
程序会再将这个请求 再次转发给内核
内核得到后请求 去磁盘上找文件,找到文件后 复制给程序
程序会构建响应报文, 构建好 后 在交给内核空间
内核空间 得到响应报文后,再 交给网卡 发给 客户
1.客户端发起请求 先发送到网卡
2.网卡收到的报文复制到内核空间
3.内核空间再复制到用户空间的应用程序空间
4.nginx 分析得到一个磁盘页面文件
5.再将需求反馈给内核空间,应为应用程序没有权限从磁盘上直接读取文件,需要依靠内核
6.内核去磁盘上找到所需要的文件,加载到内核空间
7.加载后再复制到用户空间
8.用户空间构建响应报文,交给内核空间,内核空间再复制给网卡,返回给用户
整个过程会来回切换 用户空间,内核空间 那么我们可以再次基础上做优化处理
零拷贝技术
传统的 Linux 系统的标准 I/O 接口(read、write)是基于数据拷贝的,也就是数据都是 copy_to_user 或者 copy_from_user,这样做的好处是,通过中间缓存的机制,减少磁盘 I/O 的操作,但是坏处也很明显,大量数据的拷贝,用户态和内核态的频繁切换,会消耗大量的 CPU 资源,严重影响数据传输的性能,统计表明,在Linux协议栈中,数据包在内核态和用户态之间的拷贝所用的时间甚至占到了数据包整个处理流程时间的57.1%
I/O 模型相关概念
同步/异步(消息反馈机制):关注的是消息通信机制,即调用者在等待一件事情的处理结果时,被调用者是否提供完成状态的通知。
- 同步:synchronous,被调用者并不提供事件的处理结果相关的通知消息,需要调用者主动询问事情是否处理完成
- 异步:asynchronous,被调用者通过状态、通知或回调机制主动通知调用者被调用者的运行状态
阻塞/非阻塞:关注调用者在等待结果返回之前所处的状态
- 阻塞:blocking,指IO操作需要彻底完成后才返回到用户空间,调用结果返回之前,调用者被挂起,干不了别的事情。
- 非阻塞:nonblocking,指IO操作被调用后立即返回给用户一个状态值,而无需等到IO操作彻底完成,在最终的调用结果返回之前,调用者不会被挂起,可以去做别的事情。
网络I/O模型
阻塞型、非阻塞型、复用型、信号驱动型、异步
1 阻塞型 I/O 模型(blocking IO)
nginx 交代 内核 我需要 ajpg 文件构建报文,任务交代下去后,nginx 进程是阻塞的 无法继续接待下一个请求, 内核完成会主动告知 nginx
阻塞IO模型是最简单的I/O模型,用户线程在内核进行IO操作时被阻塞用户线程通过系统调用read发起I/O读操作,由用户空间转到内核空间。内核等到数据包到达后,然后将接收的数据拷贝到用户空间,完成read操作用户需要等待read将数据读取到buffer后,才继续处理接收的数据。整个I/O请求的过程中,用户线程是被阻塞的,这导致用户在发起IO请求时,不能做任何事情,对CPU的资源利用率不够
- 优点:程序简单,在阻塞等待数据期间进程/线程挂起,基本不会占用 CPU 资源
- 缺点:每个连接需要独立的进程/线程单独处理,当并发请求量大时为了维护程序,内存、线程切换开销较大,apache 的preforck使用的是这种模式。
同步阻塞:程序向内核发送I/O请求后一直等待内核响应,如果内核处理请求的IO操作不能立即返回,则进程将一直等待并不再接受新的请求,并由进程轮训查看I/O是否完成,完成后进程将I/O结果返回给Client,在IO没有返回期间进程不能接受其他客户的请求,而且是有进程自己去查看I/O是否完成,这种方式简单,但是比较慢,用的比较少。
2非阻塞型 I/O 模型 (nonblocking IO)
nginx 交代了一个任务给内核后,不需要等待这个任务完成,可以继续下一个请求,但由于,是同步的消息机制,需要反复询问内核 我nginx 交代给你内
核的任务是否完成,由于反复询问反而造成了效率低下
用户线程发起IO请求时立即返回。但并未读取到任何数据,用户线程需要不断地发起IO请求,直到数据到达后,才真正读取到数据,继续执行。即 “轮询”机制存在两个问题:如果有大量文件描述符都要等,那么就得一个一个的read。这会带来大量的Context Switch(read是系统调用,每调用一次就得在用户态和核心态切换一次)。轮询的时间不好把握。这里是要猜多久之后数据才能到。等待时间设的太长,程序响应延迟就过大;设的太短,就会造成过于频繁的重试,干耗CPU而已,是比较浪费CPU的方式,一般很少直接使用这种模型,而是在其他IO模型中使用非阻塞IO这一特性。
非阻塞:程序向内核发送请I/O求后一直等待内核响应,如果内核处理请求的IO操作不能立即返回IO结果,进程将不再等待,而且继续处理其他请求,但是仍然需要进程隔一段时间就要查看内核I/O是否完成。
查看上图可知,在设置连接为非阻塞时,当应用进程系统调用 recvfrom 没有数据返回时,内核会立即返回一个 EWOULDBLOCK 错误,而不会一直阻塞到数据准备好。如上图在第四次调用时有一个数据报准备好了,所以这时数据会被复制到 应用进程缓冲区 ,于是 recvfrom 成功返回数据当一个应用进程这样循环调用 recvfrom 时,称之为轮询 polling 。这么做往往会耗费大量CPU时间,实际使用很少
3 多路复用 I/O 型 ( I/O multiplexing )
I/O multiplexing 主要包括:select,poll,epoll三种系统调用,select/poll/epoll的好处就在于单个process就可以同时处理多个网络连接的IO。它的基本原理就是select/poll/epoll这个function会不断的轮询所负责的所有socket,当某个socket有数据到达了,就通知用户进程。当用户进程调用了select,那么整个进程会被block,而同时,kernel会“监视”所有select负责的socket,当任何一个socket中的数据准备好了,select就会返回。这个时候用户进程再调用read操作,将数据从kernel拷贝到用户进程。Apache prefork是此模式的select,work是poll模式。
4 信号驱动式 I/O 模型 (signal-driven IO)
信号驱动I/O的意思就是我们现在不用傻等着了,也不用去轮询。而是让内核在数据就绪时,发送信号通知我们。
调用的步骤是,通过系统调用 sigaction ,并注册一个信号处理的回调函数,该调用会立即返回,然后主程序可以继续向下执行,当有I/O操作准备就绪,即内核数据就绪时,内核会为该进程产生一个SIGIO 信号,并回调注册的信号回调函数,这样就可以在信号回调函数中系统调用 recvfrom 获取数据,将用户进程所需要的数据从内核空间拷贝到用户空间
此模型的优势在于等待数据报到达期间进程不被阻塞。用户主程序可以继续执行,只要等待来自信号处理函数的通知。
在信号驱动式 I/O 模型中,应用程序使用套接口进行信号驱动 I/O,并安装一个信号处理函数,进程继续运行并不阻塞
当数据准备好时,进程会收到一个 SIGIO 信号,可以在信号处理函数中调用 I/O 操作函数处理数据。
- 优点:线程并没有在等待数据时被阻塞,内核直接返回调用接收信号,不影响进程继续处理其他请求因此可以提高资源的利用率
- 缺点:信号 I/O 在大量 IO 操作时可能会因为信号队列溢出导致没法通知
5异步 I/O 模型 (asynchronous IO)
异步I/O 与 信号驱动I/O最大区别在于,信号驱动是内核通知我们何时开始一个I/O操作,而异步I/O是由内核通知我们I/O操作何时完成,两者有本质区别,相当于不用去饭店场吃饭,直接点个外卖,把等待上菜的时间也给省了。所有事情都交给内核处理。
这五种 I/O 模型中,越往后,阻塞越少,理论上效率也是最优前四种属于同步 I/O,因为其中真正的I/O 操作(recvfrom)将阻塞进程/线程,只有异步 I/O 模型才与 POSIX 定义的异步 I/O 相匹配
Nginx支持在多种不同的操作系统实现不同的事件驱动模型,但是其在不同的操作系统甚至是不同的系统版本上面的实现方式不尽相同,主要有以下实现方式:
1、select:
select库是在linux和windows平台都基本支持的 事件驱动模型库,并且在接口的定义也基本相同,只是部
分参数的含义略有差异,最大并发限制1024,是最早期的事件驱动模型。
2、poll:
在Linux 的基本驱动模型,windows不支持此驱动模型,是select的升级版,取消了最大的并发限制,在编
译nginx的时候可以使用--with-poll_module和--without-poll_module这两个指定是否编译select
库。
3、epoll:
epoll是库是Nginx服务器支持的最高性能的事件驱动库之一,是公认的非常优秀的事件驱动模型,它和
select和poll有很大的区别,epoll是poll的升级版,但是与poll有很大的区别.
epoll的处理方式是创建一个待处理的事件列表,然后把这个列表发给内核,返回的时候在去轮训检查这个
表,以判断事件是否发生,epoll支持一个进程打开的最大事件描述符的上限是系统可以打开的文件的最大
数,同时epoll库的I/O效率不随描述符数目增加而线性下降,因为它只会对内核上报的“活跃”的描述符进行
操作。
4、rtsig:
不是一个常用事件驱动,最大队列1024,不是很常用
5、kqueue:
用于支持BSD系列平台的高校事件驱动模型,主要用在FreeBSD 4.1及以上版本、OpenBSD 2.0级以上版
本,NetBSD级以上版本及Mac OS X 平台上,该模型也是poll库的变种,因此和epoll没有本质上的区别,
都是通过避免轮训操作提供效率。
6、/dev/poll:
用于支持unix衍生平台的高效事件驱动模型,主要在Solaris 平台、HP/UX,该模型是sun公司在开发
Solaris系列平台的时候提出的用于完成事件驱动机制的方案,它使用了虚拟的/dev/poll设备,开发人员
将要见识的文件描述符加入这个设备,然后通过ioctl()调用来获取事件通知,因此运行在以上系列平台的时
候请使用/dev/poll事件驱动机制。
7、eventport:
该方案也是sun公司在开发Solaris的时候提出的事件驱动库,只是Solaris 10以上的版本,该驱动库看防
止内核崩溃等情况的发生。
8、Iocp:
Windows系统上的实现方式,对应第5种(异步I/O)模型。
Select:
POSIX所规定,目前几乎在所有的平台上支持,其良好跨平台支持也是它的一个优点,本质上是通过设置或者
检查存放fd标志位的数据结构来进行下一步处理
缺点
单个进程能够监视的文件描述符的数量存在最大限制,在Linux上一般为1024,可以通过修改宏定义
FD_SETSIZE,再重新编译内核实现,但是这样也会造成效率的降低
单个进程可监视的fd数量被限制,默认是1024,修改此值需要重新编译内核
对socket是线性扫描,即采用轮询的方法,效率较低
select 采取了内存拷贝方法来实现内核将 FD 消息通知给用户空间,这样一个用来存放大量fd的数据结
构,这样会使得用户空间和内核空间在传递该结构时复制开销大
poll:
本质上和select没有区别,它将用户传入的数组拷贝到内核空间,然后查询每个fd对应的设备状态
其没有最大连接数的限制,原因是它是基于链表来存储的
大量的fd的数组被整体复制于用户态和内核地址空间之间,而不管这样的复制是不是有意义
poll特点是“水平触发”,如果报告了fd后,没有被处理,那么下次poll时会再次报告该fd
select是边缘触发即只通知一次
epoll:
在Linux 2.6内核中提出的select和poll的增强版本
支持水平触发LT和边缘触发ET,最大的特点在于边缘触发,它只告诉进程哪些fd刚刚变为就需态,并且只会
通知一次
使用“事件”的就绪通知方式,通过epoll_ctl注册fd,一旦该fd就绪,内核就会采用类似callback的回调
机制来激活该fd,epoll_wait便可以收到通知
优点:
没有最大并发连接的限制:能打开的FD的上限远大于1024(1G的内存能监听约10万个端口),具体查
看/proc/sys/fs/file-max,此值和系统内存大小相关
效率提升:非轮询的方式,不会随着FD数目的增加而效率下降;只有活跃可用的FD才会调用callback函数,
即epoll最大的优点就在于它只管理“活跃”的连接,而跟连接总数无关
内存拷贝,利用mmap(Memory Mapping)加速与内核空间的消息传递;即epoll使用mmap减少复制开销
Nginx概述
Nginx 功能介绍
-
静态的web资源服务器html,图片,js,css,txt等静态资源
-
http/https协议的反向代理 ,7层 url
-
结合FastCGI /uWSGI/SCGI等协议反向代理动态资源请求
-
tcp/udp协议的请求转发(反向代理) 4层
基础特性
- 模块化设计,较好的扩展性
- 高可靠性
- 支持热部署:不停机更新配置文件,升级版本,更换日志文件
- 低内存消耗:10000个keep-alive连接模式下的非活动连接,仅需2.5M内存
- event-driven, aio, mmap,sendfile
Web 服务相关的功能
- 虚拟主机(server)
- 支持 keep-alive 和管道连接(利用一个连接做多次请求)
- 访问日志(支持基于日志缓冲提高其性能)
- url rewirte
- 路径别名
- 基于IP及用户的访问控制
- 支持速率限制及并发数限制
- 重新配置和在线升级而无须中断客户的工作进程
Nginx 架构
Nginx 进程结构
web请求处理机制
-
多进程方式:服务器每接收到一个客户端请求就有服务器的主进程生成一个子进程响应客户端,直到用户关闭连接,这样的优势是处理速度快,子进程之间相互独立,但是如果访问过大会导致服务器资源耗尽而无法提供请求。
-
多线程方式:与多进程方式类似,但是每收到一个客户端请求会有服务进程派生出一个线程来个客户方进行交互,一个线程的开销远远小于一个进程,因此多线程方式在很大程度减轻了web服务器对系统资源的要求,但是多线程也有自己的缺点,即当多个线程位于同一个进程内工作的时候,可以相互访问同样的内存地址空间,所以他们相互影响,一旦主进程挂掉则所有子线程都不能工作了,IIS服务器使用了多线程的方式,需要间隔一段时间就重启一次才能稳定。
主进程(master process)的功能:
对外接口:接收外部的操作(信号)
对内转发:根据外部的操作的不同,通过信号管理 Worker
监控:监控 worker 进程的运行状态,worker 进程异常终止后,自动重启 worker 进程
读取Nginx 配置文件并验证其有效性和正确性
建立、绑定和关闭socket连接
按照配置生成、管理和结束工作进程
接受外界指令,比如重启、升级及退出服务器等指令
不中断服务,实现平滑升级,重启服务并应用新的配置
开启日志文件,获取文件描述符
不中断服务,实现平滑升级,升级失败进行回滚处理
编译和处理perl脚本
工作进程(worker process)的功能:
所有 Worker 进程都是平等的
实际处理:网络请求,由 Worker 进程处理
Worker进程数量:一般设置为核心数,充分利用CPU资源,同时避免进程数量过多,导致进程竞争CPU资源,
增加上下文切换的损耗
接受处理客户的请求
将请求依次送入各个功能模块进行处理
I/O调用,获取响应数据
与后端服务器通信,接收后端服务器的处理结果
缓存数据,访问缓存索引,查询和调用缓存数据
发送请求结果,响应客户的请求
接收主程序指令,比如重启、升级和退出等
nginx 模块
- 核心模块:是 Nginx 服务器正常运行必不可少的模块,提供错误日志记录 、配置文件解析 、事件驱动机制 、进程管理等核心功能
- 标准HTTP模块:提供 HTTP 协议解析相关的功能,比如: 端口配置 、 网页编码设置 、 HTTP响应头设置 等等
- 可选HTTP模块:主要用于扩展标准的 HTTP 功能,让 Nginx 能处理一些特殊的服务,比如:Flash 多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等
- 邮件服务模块:主要用于支持 Nginx 的 邮件服务 ,包括对 POP3 协议、 IMAP 协议和 SMTP协议的支持
- Stream服务模块: 实现反向代理功能,包括TCP协议代理 反向
- 第三方模块:是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如: Json 支持、 Lua 支持等
nginx高度模块化,但其模块早期不支持DSO机制;1.9.11 版本支持动态装载和卸载
核心模块:core module
标准模块:
HTTP 模块: ngx_http_*
HTTP Core modules #默认功能
HTTP Optional modules #需编译时指定
Mail 模块: ngx_mail_*
Stream 模块 ngx_stream_*
第三方模块
安装及使用Nginx
1 编译安装nginx
源码包内的文件:
- contrib:vim 格式文件,修改nginx配置文件的格式,高亮 cp -r /opt/nginx-1.18.0/contrib/vim/* /usr/share/vim/vimfiles/
- conf:配置文件
- man:man帮助 man man/nginx.8 不加路径看不了 nginx.8 文件
- src:源码包 点c 点h 结尾的文件 find src -type f |xargs cat |wc -l 193678
https://nginx.org/en/download.html
#官网
yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
#安装依赖包
useradd -M -s /sbin/nologin nginx
#新建nginx用户便于管理
cd /opt/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
#官网下载安装包
tar xf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
#解压软件包
mkdir /apps/nginx -p
./configure --help
#查看帮助模块
./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
make
make install
chown -R nginx.nginx /apps/nginx
#修改权限
ll /apps/nginx/
total 0
drwxr-xr-x 2 root root 333 Sep 22 12:49 conf
drwxr-xr-x 2 root root 40 Sep 22 12:49 html
drwxr-xr-x 2 root root 6 Sep 22 12:49 logs
drwxr-xr-x 2 root root 19 Sep 22 12:49 sbin
######安装好后生成四个文件功能如下
- conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。
- html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
- logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
- sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
1.1启动停止nginx
##########启动##############
/apps/nginx/sbin/nginx
#绝对路径启动
ln -s /apps/nginx/sbin/nginx /usr/sbin/
#创建软连接后直接 nginx启动
##########停止###############
killall nginx
1.2创建Nginx 自启动文件
#复制同一版本的nginx的yum安装生成的service文件
vim /usr/lib/systemd/system/nginx.service
#建立文件
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
#注意文件位置,如果不对 启动不了
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
#注意启动文件位置
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
###如果需要修改pid文件可以执行以下操作#################
mkdir /apps/nginx/run/
#创建目录
vim /apps/nginx/conf/nginx.conf
#修改配置文件
pid /apps/nginx/run/nginx.pid;
#找到 pid的位置修改
#######################################################
systemctl daemon-reload
#重新加载配置
systemctl enable --now nginx
#开机自启并立即启动 如果卡主是应为logs下有 nginx.pid 文件 删除即可
chown -R nginx.nginx /apps/nginx
#修改权限
2. yum安装
centos7 需要安装epel源
yum install -y epel-release
#安装epel源 额外 rpeo
yum install nginx -y
yum nginx
去使用官方源按装较新的版本
http://nginx.org/en/linux_packages.html#RHEL
3.平滑升级及命令、信号使用
3.1信号
nginx 命令支持向其发送信号,实现不同功能
nginx 当做单独命令使用有以下选项
[root@node2 ~]#nginx -h
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /etc/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
3.1.1 显示版本
[root@node2 ~]#nginx -v
nginx version: nginx/1.22.1
3.1.2显示编译详细情况 模块等信息
[root@node2 ~]#nginx -V
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
3.1.3发送信号
kill -l 看信号大全
nginx -h 中可以看到的信号较少
s signal : send signal to a master process: stop, quit, reopen, reload
可以使用man手册来查看详细的信号 如果没安装,去源码包里找到man文件
man 路径/nginx.8 不加路径打不开man帮助
stop SIGTERM 直接停止
quit SIGQUIT 优雅的退出:有人在访问不会结束进程
reopen SIGUSR1 分割日志
reload SIGHUP 重新加载配置文件
SIGHUP Reload configuration, start the new worker process with a new configuration, and
gracefully shut down old worker processes.
SIGQUIT Shut down gracefully. 优雅的关闭:有人在访问不会结束进程
SIGUSR1 Reopen log files. 重新分割日志
SIGUSR2 Upgrade the nginx executable on the fly. 运行中升级
SIGWINCH Shut down worker processes gracefully. 优雅的关闭worker进程,work进程负责处理请求,还有请求不会关闭
帮助: -? -h
使用指定的配置文件: -c
指定配置指令:-g
指定运行目录:-p
测试配置文件是否有语法错误:-t -T
打印nginx的版本信息、编译信息等:-v -V
发送信号: -s 示例: nginx -s reload
实例:
nginx -s stop #立即关闭nginx
nginx -s quit #优雅退出 不影响业务的状态下退出
nginx -s reload #重新加载
USR1分割日志
[root@node2 nginx]#cd /var/log/nginx/
#切换到
[root@node2 nginx]#mv access.log access.log.bak
[root@node2 nginx]#touch access.log
#此时日志不会写入到新文件
需要给master 进程发送 USR1信号
[root@node2 nginx]#ps aux |grep nginx
root 117994 0.0 0.0 49024 1084 ? Ss 11:23 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 117995 0.0 0.0 51512 2156 ? S 11:23 0:00 nginx: worker process
nginx 117996 0.0 0.0 51512 1932 ? S 11:23 0:00 nginx: worker process
root 120458 0.0 0.1 151664 5048 pts/1 S+ 12:32 0:00 vim /usr/lib/systemd/system/nginx.serv
[root@node2 nginx]#kill -s USR1 117994
#kill是要跟进程
[root@node2 nginx]#nginx -s reopen
#nginx -s 不能跟进程
3.1.4 指定配置 不已配置文件中的为准
nginx -g 指定配置 不已配置文件中的为准
nginx -g 'user zhangsan;' 已张三身份运行,默认是以nginx身份
nginx -g 'daemon off;' 前台运行命令
3.1.5 检查语法格式
nginx -t
3.2 升级 nginx1.18 nginx1.20
- 将旧Nginx文件换成新Nginx文件(注意备份)
- 向master进程发送USR2信号
- master进程修改pid文件名,加后缀.oldbin
- master进程用新Nginx文件启动新master进程,系统中将有新旧两个Nginx主进程共同提供Web服务
- 向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止,并删除Nginx.pid.oldbin文件
- 向旧master进程发送QUIT信号,关闭老master
- 如果发现升级有问题,可以回滚向老master发送HUP,向新master发送QUIT
[root@localhost ~]#ps aux |grep nginx
#先查看是否开启nginx
root 26603 0.0 0.0 46204 1160 ? Ss 04:58 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 26604 0.0 0.1 48736 2240 ? S 04:58 0:00 nginx: worker process
root 30924 0.0 0.0 118744 1152 pts/1 S+ 10:10 0:00 man /opt/nginx-1.18.0/man/nginx.8
root 31035 0.0 0.0 112824 976 pts/2 S+ 10:16 0:00 grep --color=auto nginx
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf
#开启 两核
#user nobody;
worker_processes 2;
#worker_processes 1 原来是1核
[root@localhost ~]#nginx -s reload
#重新加载配置文件
[root@localhost ~]#ps aux |grep nginx
root 26603 0.0 0.1 46344 2012 ? Ss 04:58 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 30924 0.0 0.0 118744 1152 pts/1 S+ 10:10 0:00 man /opt/nginx-1.18.0/man/nginx.8
nginx 31132 0.0 0.1 48856 2092 ? S 10:17 0:00 nginx: worker process
nginx 31133 0.0 0.1 48856 2080 ? S 10:17 0:00 nginx: worker process
##此处多了一个子进程
root 31158 0.0 0.0 112824 972 pts/2 S+ 10:19 0:00 grep --color=auto nginx
[root@localhost ~]#ps auxf |grep nginx
#查看进程树
root 30924 0.0 0.0 118744 1152 pts/1 S+ 10:10 0:00 | \_ man /opt/nginx-1.18.0/man/nginx.8
root 31184 0.0 0.0 112824 976 pts/2 S+ 10:20 0:00 \_ grep --color=auto nginx
root 26603 0.0 0.1 46344 2012 ? Ss 04:58 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 31132 0.0 0.1 48856 2092 ? S 10:17 0:00 \_ nginx: worker process
nginx 31133 0.0 0.1 48856 2080 ? S 10:17 0:00 \_ nginx: worker process
[root@localhost ~]#wget https://nginx.org/download/nginx-1.20.2.tar.gz -P /usr/local/src/
#下载安装包到src目录
[root@localhost ~]#cd /usr/local/src/
[root@localhost src]#ls
nginx-1.20.2.tar.gz
[root@localhost src]#tar xf nginx-1.20.2.tar.gz
[root@localhost src]#cd nginx-1.20.2/
[root@localhost nginx-1.20.2]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
########################################################################
这时需要重新编译安装 ./configur 安装参数基本一致 这时可以使用 nginx -V 查看
########################################################################
[root@localhost nginx-1.20.2]#nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
########################################################################
如果 有新模块在后添加即可
########################################################################
[root@localhost nginx-1.20.2]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#重新编译
[root@localhost nginx-1.20.2]#make
###########注意不要执行 make install
[root@localhost objs]#cd objs
#此文件夹中有新版本的nginx 运行程序
[root@localhost nginx-1.20.2]#objs/nginx -v
#查看版本
nginx version: nginx/1.20.2
[root@localhost nginx-1.20.2]#mv /apps/nginx/sbin/nginx /apps/nginx/sbin/nginx.bak
#将低版本的nginx主程序改名
[root@localhost nginx-1.20.2]#cp objs/nginx /apps/nginx/sbin/
#将新版本 拷入进去
[root@localhost nginx-1.20.2]#ll /apps/nginx/sbin/
总用量 15308
-rwxr-xr-x. 1 root root 7896080 4月 21 10:46 nginx
-rwxr-xr-x. 1 root root 7774624 4月 19 10:43 nginx.bak
[root@localhost nginx-1.20.2]#/apps/nginx/sbin/nginx -t
#检查下语法问题
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost nginx-1.20.2]#kill -USR2 `cat /apps/nginx/run/nginx.pid`
#发送 2 信号 信号在 man手册中可以看到
SIGNALS
The master process of nginx can handle the following signals:
SIGINT, SIGTERM Shut down quickly.
SIGHUP Reload configuration, start the new worker process with a new configuration,
and gracefully shut down old worker processes.
SIGQUIT Shut down gracefully.
SIGUSR1 Reopen log files.
SIGUSR2 Upgrade the nginx executable on the fly.
# 飞行中升级
SIGWINCH Shut down worker processes gracefully.
While there is no need to explicitly control worker processes normally, they support some sig‐
nals too:
SIGTERM Shut down quickly.
SIGQUIT Shut down gracefully.
SIGUSR1 Reopen log files.
[root@localhost nginx-1.20.2]#ps auxf|grep nginx
#生成新的master
root 34765 0.0 0.0 112824 976 pts/2 S+ 10:53 0:00 \_ grep --color=auto nginx
root 26603 0.0 0.1 46344 2012 ? Ss 04:58 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 31132 0.0 0.1 48856 2092 ? S 10:17 0:00 \_ nginx: worker process
nginx 31133 0.0 0.1 48856 2080 ? S 10:17 0:00 \_ nginx: worker process
root 34761 0.0 0.1 46220 3360 ? S 10:53 0:00 \_ nginx: master process /apps/ngin/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 34762 0.0 0.1 48756 1988 ? S 10:53 0:00 \_ nginx: worker process
nginx 34763 0.0 0.1 48756 1988 ? S 10:53 0:00 \_ nginx: worker process
[root@localhost nginx-1.20.2]#lsof -i :80
#查看谁在监听 80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 26603 root 6u IPv4 73696 0t0 TCP *:http (LISTEN)
nginx 31132 nginx 6u IPv4 73696 0t0 TCP *:http (LISTEN)
nginx 31133 nginx 6u IPv4 73696 0t0 TCP *:http (LISTEN)
nginx 34761 root 6u IPv4 73696 0t0 TCP *:http (LISTEN)
nginx 34762 nginx 6u IPv4 73696 0t0 TCP *:http (LISTEN)
nginx 34763 nginx 6u IPv4 73696 0t0 TCP *:http (LISTEN)
检验
[root@localhost html]#dd if=/dev/zero of=/apps/nginx/html/m.img bs=1G count=10
记录了10+0 的读入
记录了10+0 的写出
10737418240字节(11 GB)已复制,19.3238 秒,556 MB/秒
[root@localhost html]#ls
50x.html index.html m.img
[root@localhost html]#pwd
/apps/nginx/html
#开启新机器下载
[root@localhost data]#wget --limit-rate=1M http://192.168.91.100/m.img
--2022-04-21 11:05:14-- http://192.168.91.100/m.img
正在连接 192.168.91.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:10737418240 (10G) [application/octet-stream]
正在保存至: “m.img”
#回到网页服务器
[root@localhost html]#ss -ntap|grep 80
#查看那个进程在管理 下载
LISTEN 0 128 *:80 *:* users:(("nginx",pid=34763,fd=6),("nginx",pid=34762,fd=6),("nginx",pid=34761,fd=6),("nginx",pid=26603,fd=6))
ESTAB 0 993328 192.168.91.100:80 192.168.91.101:52402 users:(("nginx",pid=34763,fd=7))
[root@localhost man]#ls /apps/nginx/run/
#会有 新老两个进程
nginx.pid nginx.pid.oldbin
[root@localhost man]#cat /apps/nginx/run/nginx.pid.oldbin
26603
[root@localhost man]#kill -WINCH `cat /apps/nginx/logs/nginx.pid.oldbin`
#优雅关闭老进程的 worker 进程
#再开启一台服务器测试 是否是新的进程 在下载
[root@localhost ~]# wget --limit-rate=1M http://192.168.91.100/m.img
--2022-04-21 11:14:36-- http://192.168.91.100/m.img
正在连接 192.168.91.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:10737418240 (10G) [application/octet-stream]
正在保存至: “m.img”
#回到网页服务器可以看到 之前的在 但是又开启了一个 102
[root@localhost html]#ss -ntap|grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=34763,fd=6),("nginx",pid=34762,fd=6),("nginx",pid=34761,fd=6),("nginx",pid=26603,fd=6))
ESTAB 0 993328 192.168.91.100:80 192.168.91.101:52402 users:(("nginx",pid=34763,fd=7))
ESTAB 0 957128 192.168.91.100:80 192.168.91.102:44266 users:(("nginx",pid=34763,fd=12))
测试一段时间无问题 就可以了,如果断掉第一个 下载 ,老的进程就关闭了
[root@localhost html]#pstree -p |grep nginx
#查看进程关系 1.18
|-nginx(26603)
--nginx(work 23333)
--nginx(34761M)-+-nginx(34762)
| 1.20 `-nginx(34763)
3.3 回滚
[root@localhost man]#kill -HUP `cat /apps/nginx/logs/nginx.pid.oldbin`
#唤起老的进程
[root@localhost man]#ps aux|grep nginx
#又有两个master
root 26603 0.0 0.1 46344 2028 ? Ss 04:58 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 34761 0.0 0.1 46220 3360 ? S 10:53 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 34762 0.0 0.1 48756 1988 ? S 10:53 0:00 nginx: worker process
nginx 34763 0.0 0.1 48756 2232 ? S 10:53 0:01 nginx: worker process
nginx 35296 0.0 0.1 48856 2092 ? S 11:25 0:00 nginx: worker process
nginx 35297 0.0 0.1 48856 2092 ? S 11:25 0:00 nginx: worker process
root 35303 0.0 0.0 112824 976 pts/1 S+ 11:26 0:00 grep --color=auto nginx
kill -QUIT `cat /apps/nginx/run/nginx.pid` 新的进程
[root@localhost sbin]#curl -I 127.1
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Thu, 21 Apr 2022 03:35:18 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 19 Apr 2022 02:43:17 GMT
Connection: keep-alive
ETag: "625e21c5-264"
Accept-Ranges: bytes
4 配置详细解释
nginx 官方帮助文档:http://nginx.org/en/docs/
tengine 帮助文档:http://tengine.taobao.org/nginx_docs/cn/docs/
Nginx的配置文件的组成部分:
主配置文件:nginx.conf
子配置文件: include conf.d/*.conf
配置文件由指令和指令块构成
每条指令以;分号结尾,指令与值之间以空格符号分隔
pid /apps/run/nginx.pid
指令已{}达括号将多条指令组织在一起且可以嵌套指令块
include语句允许组合多个配置文件以提升可维护性
#号注释
$使用变量
部分支持正则
自定义变量:由用户使用set命令定义,格式: set variable_name value
全局配置
events{ 控制事件驱动 }
http { web网页配置有关 server { location } }
main block:主配置段,即全局配置段,对http,mail都有效
#事件驱动相关的配置 同步
event {
...
}
#http/https 协议相关配置段
http {
...
}
#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {
...
}
#stream 服务器相关配置段
stream {负载均衡
...
}
4.1全局配置
nginx 有多种模块
- 核心模块:是 Nginx 服务器正常运行必不可少的模块,提供错误日志记录 、配置文件解析 、事件驱动机制 、进程管理等核心功能
- 标准HTTP模块:提供 HTTP 协议解析相关的功能,比如: 端口配置 、 网页编码设置 、 HTTP响应头设置 等等
- 可选HTTP模块:主要用于扩展标准的 HTTP 功能,让 Nginx 能处理一些特殊的服务,比如:Flash 多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等
- 邮件服务模块:主要用于支持 Nginx 的 邮件服务 ,包括对 POP3 协议、 IMAP 协议和 SMTP协议的支持
- Stream服务模块: 实现反向代理功能,包括TCP协议代理
- 第三方模块:是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如: Json 支持、 Lua 支持等
4.1.1 关闭版本或修改版本
Syntax: server_tokens on | off | build | string;
Default:
server_tokens on;
Context: http, server, location
#修改配置文件 放在 http语句中
http {
server_tokens off;
[root@localhost nginx]#nginx -s reload
#重新加载
验证
[root@localhost sbin]#curl -I http://192.168.91.100/
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Thu, 21 Apr 2022 03:34:51 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 19 Apr 2022 02:43:17 GMT
Connection: keep-alive
ETag: "625e21c5-264"
Accept-Ranges: bytes
[root@localhost sbin]#curl -I http://192.168.91.100/
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 21 Apr 2022 04:04:23 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 19 Apr 2022 02:43:17 GMT
Connection: keep-alive
ETag: "625e21c5-264"
Accept-Ranges: bytes
自行修改
#去修改源码,在安装包里, 再重新编译 #号不要去掉
#先吧服务关闭,不然编译不成功
[root@localhost core]#vim /opt/nginx-1.18.0/src/core/nginx.h
13 #define NGINX_VERSION "9527"
14 #define NGINX_VER "http/" NGINX_VERSION
[root@localhost core]#vim /opt/nginx-1.18.0/src/http/ngx_http_header_filter_module.c
49 static u_char ngx_http_server_string[] = "Server: kgc" CRLF;
重新编译安装
./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@localhost nginx-1.18.0]#make && make install
4.1.2 修改启动的进程数
worker_processes 1;
#允许的启动工作进程数数量,和你真实的cpu数量有关 1
worker_processes auto;
#如果设置为auto 就是你真实的cpu数量
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep nginx
#可以看到 nginx的 worker数量
18620 nginx: master process /usr/ 0 0
18621 nginx: worker process 2 0
18622 nginx: worker process 3 0
18623 nginx: worker process 0 0
18624 nginx: worker process 3 0
18641 grep --color=auto nginx 0 0
4.1.3 cpu与work 进程 绑定
将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。
CPU序号:
CPU MASK: 00000001:0号CPU
00000010:1号CPU
................
10000000:7号CPU
worker_cpu_affinity 00000001 00000010 00000100 00001000;第0号---第3号CPU
#序号绑定cpu 亲缘性
worker_cpu_affinity 00000101 00001010;
#同一个work 可以绑定 两个cpu可以这么写 但是不建议,本来就是 不希望飘动,这样也是飘动
实际操作
例子:
user nginx;
worker_processes auto;
worker_cpu_affinity 00000001 00000010;
#绑定到 第一 和 第二块cpu上
error_log /var/log/nginx/error.log;
#注意 要绑一起绑
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep -v grep |grep nginx|sort -n
18620 nginx: master process /usr/ 2 0
18951 nginx: worker process 0 0
18952 nginx: worker process 1 0
18954 grep --color=auto nginx 2 0
验证cpu乱跑可以刷新网页即可
while true ;do ab -c 1000 -n 1000 http://192.168.91.100/;done
4.1.4 PID 路径
pid /apps/nginx/run/nginx.pid;
pid 进程号文件位置可以自定义省略
4.1.5 nginx进程的优先级(work进程的优先级)
当你想将nginx的work进程的优先级调高 可以使用nice设置
nice的优先级是 -20 到 19
worker_priority 0;
#工作进程优先级,-20~20(19)
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep nginx|sort -n
#查看默认优先级,默认优先级为0
18620 nginx: master process /usr/ 3 0
19045 nginx: worker process 0 0
19046 nginx: worker process 1 0
19082 vim /etc/nginx/nginx.conf 1 0
19173 grep --color=auto nginx 1 0
修改配置文件
user nginx;
worker_processes 2;
worker_cpu_affinity 00000101 00000010;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
worker_priority -20;
#添加此项
nginx -s reload
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep nginx|sort -n
18620 nginx: master process /usr/ 3 0
19196 nginx: worker process 0 -20
19197 nginx: worker process 1 -20
19199 grep --color=auto nginx 1 0
4.1.6 调试work进程打开的文件的个数
pid /run/nginx.pid;
worker_priority -20;
worker_rlimit_nofile 65536;
#所有worker进程能打开的文件数量上限,包括:Nginx的所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制.最好与ulimit -n 或者limits.conf的值保持一致,
只要机器性能够多加几个也没问题
例子:
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep nginx|sort -n
18620 nginx: master process /usr/ 0 0
19302 nginx: worker process 0 -20
19303 nginx: worker process 1 -20
19456 grep --color=auto nginx 0 0
#选择其中一个进程 到proc 下去查看 连接状态
[root@localhost ~]#ls /proc/19302/fd
#可以看到打开的连接数
0 1 10 13 14 15 2 4 5 6 7 9
在站点目录下生成大文件big.img
[root@localhost ~]#cd /usr/share/nginx/html/
[root@localhost ~]#dd if=/dev/zero of=big.img bs=100M count=1
可以在 2号机器上安装压力测试的工具写一个压力测试脚本
[root@localhost ~]#yum install httpd-tools -y
#安装压力测试的工具
[root@localhost ~]#while : ;do ab -c 1000 -n 10000 http://192.168.91.100/big.img;sleep 1;done
#写一个压力测试脚本
回到1号机器
[root@localhost ~]#ls /proc/19302/fd
#在2号机压力测试后 就会产生很多 进程文件
0 137 176 214 253 292 330 37 408 447 486 524 563 601 640 68
1 138 177 215 254 293 331 370 409 448 487 525 564 602 641 680
10 139 178 216 255 294 332 371 41 449 488 526 565 603 642 681
100 14 179 217 256 295 333 372 410 45 489 527 566 604 643 69
101 140 18 218 257 296 334 373 411 450 49 528 567 605 644 7
改了后还需要 修改 系统默认项
[root@localhost ~]#ulimit -a
#单个进程能打开的文件是1024
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7168
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7168
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@localhost ~]#ulimit -n 60000
#只修改当前窗口
需要修改pam认证文件
[root@localhost ~]#vim /etc/security/limits.conf
#最后加入
* soft core unlimited
* hard core unlimited
* soft nproc 1000000
* hard nproc 1000000
* soft nofile 1000000
* hard nofile 1000000
* soft memlock 32000
* hard memlock 32000
* soft msgqueue 8192000
* hard msgqueue 8192000
#设置完后重启
[root@localhost ~]#ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7168
max locked memory (kbytes, -l) 32000
max memory size (kbytes, -m) unlimited
open files (-n) 1000000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 8192000
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 1000000
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
4.1.7 服务是否已后台方式运行
一般服务都是后台运行,前台容器中会用到
user nginx;
worker_processes 2;
worker_cpu_affinity 00000101 00000010;
daemon off;
#加入此选项
[root@localhost ~]#systemctl start nginx
4.1.8 只有 master进程没有 work进程
实际生产中使用较少
master_process off|on;
#是否开启Nginx的master-worker工作模式,仅用于开发调试场景,默认为on
4.2 event事件
events {
worker_connections 65536; #设置单个工作进程的最大并发连接数
use epoll;
#使用epoll事件驱动,Nginx支持众多的事件驱动,比如:select、poll、epoll,只能设置在events模块中设置。
accept_mutex on;
#on为同一时刻一个请求轮流由work进程处理,而防止被同时唤醒所有worker,避免多个睡眠进程被唤醒的设置,默认为off,新请求会唤醒所有worker进程,此过程也称为"惊群",因此nginx刚安装完以后要进行适当的优化。建议设置为on
multi_accept on;
#ON时Nginx服务器的每个工作进程可以同时接受多个新的网络连接,此指令默认为off,即默认为一个工作进程只能一次接受一个新的网络连接,打开后几个同时接受多个。建议设置为on
}
4.3 http设置
http 是一个大的语句块,包含若干个小的语句块(比如server语句块)
http {
...
... #各server的公共配置
server { #每个server用于定义一个虚拟主机,第一个server为默认虚拟服务器
...
}
server {
...
server_name #虚拟主机名
root #主目录
alias #路径别名
location [OPERATOR] URL { #指定URL的特性
...
if CONDITION {
...
}
}
}
}
http 协议配置说明
http {
include mime.types; #导入支持的文件类型,是相对于/apps/nginx/conf的目录
default_type application/octet-stream; #除mime.types中文件类型外,设置其它文件默认类型,访问其它类型时会提示下载不匹配的类型文件
#日志配置部分
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
#自定义优化参数
sendfile on;
#tcp_nopush on; #在开启了sendfile的情况下,合并请求后统一发送给客户端。
#tcp_nodelay off; #在开启了keepalived模式下的连接是否启用TCP_NODELAY选项,当为off时,延迟0.2s发送,默认On时,不延迟发送,立即发送用户响应报文。
#keepalive_timeout 0;
keepalive_timeout 65 65; #设置会话保持时间,第二个值为响应首部:keepAlived:timeout=65,可以和第一个值不同
#gzip on; #开启文件压缩
server {
listen 80; #设置监听地址和端口
server_name localhost; #设置server name,可以以空格隔开写多个并支持正则表达式,如:*.kgc.com www.kgc.* ~^www\d+\.kgc\.com$ default_server
#charset koi8-r; #设置编码格式,默认是俄语格式,建议改为utf-8
#access_log logs/host.access.log main;
location /fxj { www.ky31.com/fsj /apps/nginx/html
root /data;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #定义错误页面
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ { #以http的方式转发php请求到指定web服务器
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ { #以fastcgi的方式转发php请求到php处理
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht { #拒绝web形式访问指定文件,如很多的网站都是通过.htaccess文件
来改变自己的重定向等功能。
# deny all;
#}
location ~ /passwd.html {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server { #自定义虚拟server
3.3.1 MIME
范例: 识别php文件为text/html
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm; #指定默认网页文件,此指令由
ngx_http_index_module模块提供
# }
#}
# HTTPS server
#
#server { #https服务器配置
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
4.3.1 mime
此项为支持的 文件格式,如果不支持的格式 会自动帮你下载,如果支持 就会显示在网页上
[root@localhost ~]#vim /etc/nginx/mime.types
types {
text/html html htm shtml;
.....................................................................
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
4.3.2 sever 下的 root
root指定了主页文件的位置
root路径格式 指定文件的路径 url
Syntax: root path;
Default:
root html;
Context: http, server, location,
指明 你软件的根目录
4.3.3 server块构建虚拟主机 实际只有1台, 好像有多台主机
A 建立独立的配置文件 构建不同虚拟主机 PC端和手机端
[root@localhost ~]#vim /etc/nginx/nginx.conf
#修改配置文件 要放在 http 模块里
include /etc/nginx/mime.types;
include /apps/nginx/conf.d/*.conf;
[root@localhost ~]#mkdir -p /apps/nginx/conf.d/
#建立子配置文件
[root@localhost conf.d]#vim pc.conf
#分别编写配置文件
server {
listen 80;
server_name localhost;
root /data/nginx/html/pc/;
}
#也可以使用location 模块
server{
listen 192.168.91.100:80;
server_name www.pc.com;
location / {
root /data/nginx/html/pc;
}
}
[root@localhost conf.d]#vim mobile.conf
#分别编写配置文件
server {
listen 80;
server_name m.kgc.com;
root /data/nginx/html/mobile/;
}
[root@localhost ~]#mkdir /data/nginx/html/pc -pv
[root@localhost ~]#mkdir /data/nginx/html/mobile -pv
#构建数据文件夹
[root@localhost ~]#echo pc > /data/nginx/html/pc/index.html
[root@localhost ~]#echo moblie > /data/nginx/html/mobile/index.html
#构建数据文件
去第二台机器修改/etc/hosts 文件
[root@localhost ~]#vim /etc/hosts
192.168.91.100 www.kgc.com
192.168.91.100 m.kgc.com
[root@localhost ~]#curl m.kgc.com
moblie
[root@localhost ~]#curl www.kgc.com
pc
[root@localhost pc]#vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.kgc.com;
location /admin {
root /data/nginx/html/pc/;
}
}
基于端口
server{
listen 192.168.91.100:8080;
server_name www.m.com;
root /data/nginx/html/m;
}
server{
listen 192.168.91.100:80;
server_name www.pc.com;
root /data/nginx/html/pc;
}
基于不同ip地址
4.3.4 alias 别名
别名设置
server {
listen 80;
server_name www.kgc.com;
location /nwes {
root /data/nginx/html/pc/;
#相当于追加 将 文件夹news追加到/data/nginx/html/pc/news
}
location /study{
alias /mnt/nginx/sports/;
#相当于替换 你访问 study 就是访问/mnt/nginx/sports
}
}
4.3.5 location去匹配
www.kgc.com/ url
文件路径描述符
在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,按一定的优化级找出一个最佳匹配,而后应用其配置在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri,uri是用户请求的字符串,即域名后面的web文件路径,然后使用该location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理此请求。
location 官方帮助:
http://nginx.org/en/docs/http/ngx_http_core_module.html#location
语法规则
#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
= #用于标准url前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立即处理请求
^~ #用于标准url前,表示包含正则表达式,并且匹配以指定的正则表达式开头,对URI的最左边部分做匹配检查,不区分字符大小写
~ #用于标准url前,表示包含正则表达式,并且区分大小写
~* #用于标准url前,表示包含正则表达式,并且不区分大写
不带符号 #匹配起始于此uri的所有的uri
#\ #用于标准url前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
#匹配优先级从高到低:
=, ^~, ~/~*, 不带符号
- = 用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立即处理请求
2. ^~ 用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头,对URI的最左边部分做匹配检查,不区分字符大小写
3. ~ 用于标准uri前,表示包含正则表达式,并且区分大小写
4. ~* 用于标准uri前,表示包含正则表达式,并且不区分大写
5. 不带符号 匹配起始于此uri的所有的uri
6. \ 用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
4.3.5.1 不区分大小写-案例
~* 用来对用户请求的uri做模糊匹配,uri中无论都是大写、都是小写或者大小写混合,此模式也都会匹配,通常使用此模式匹配用户request中的静态资源并继续做下一步操作,此方式使用较多注意: 此方式中,对于Linux文件系统上的文件仍然是区分大小写的,如果磁盘文件不存在,仍会提示404 ~* 虽然 不区分大小写 但是系统的文件系统区分大小写
#正则表达式匹配:
location ~* /A.?\.jpg {
#匹配 已A后面一个或没有字符,已.jpg结尾的图片
root /opt/nginx/html/image;
}
只要是图片就去 images中找
server{
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js|css)$ {
root /data/nginx/images/;
}
}
#####匹配优先级#########
此处的优先级有小问题
server{
listen 80;
server_name www.pc.com;
location = / {
root /data/nginx/pc;
}
location / {
root /mnt/nginx/news;
}
}
location = /1.jpg {
root /data/nginx/static1;
index index.html;
}
location /1.jpg {
root /data/nginx/static2;
index index.html;
}
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/nginx/static3;
index index.html;
}
[root@centos8 ~]# mkdir -p /data/nginx/static{1,2,3}
#上传图片到 /data/nginx/static{1,2,3} 并重启nginx访问测试
#匹配优先级:=, ^~, ~/~*,/
location优先级:(location =) > (location ^~ 路径) > (location ~,~* 正则顺序) >
(location 完整路径) > (location 部分起始路径) > (/)
4.3.6 access 模块 四层控制
如何查看模块是否默认安装
http://nginx.org/en/docs/http/ngx_http_access_module.html
[root@localhost nginx-1.18.0]#./configure --help |grep access
# 可以去源码包中 过滤 access模块 自带 不想要可以 without 去掉
--without-http_access_module disable ngx_http_access_module
--http-log-path=PATH set http access log pathname
--without-stream_access_module disable ngx_stream_access_module
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
配置文件修改
server {
listen 80;
server_name www.kgc.com;
allow 192.168.91.0/24;
deny 192.168.91.101;
location / {
root /data/nginx/html/pc;
}
}
匹配了之后就不往下匹配了
所以范围小的往上
#############
当访问 有问题时 默认站点 是按 文件的名字首字母开头排序
4.3.7 验证模块 需要输入用户名和密码
配置
server {
listen 80;
server_name www.kgc.com;
location / {
root /data/nginx/html/pc;
}
location /admin{
root /data/nginx/html/pc;
auth_basic "admin site";
#提示信息,不是所有浏览器都有用
auth_basic_user_file/apps/nginx/conf.d/.httpuser;
#密码文件存放位置
}
}
提示信息如下 火狐浏览器有,谷歌没有
htpasswd
此命令来自于 httpd-tools 包,如果没有安装 一下即可
使用方法:
第一次生成文件
htpasswd -c 文件路径 姓名 交互式生成密码
htpasswd -bc 文件路径 姓名 密码 直接将密码跟在后面
-c 代表新建用户名和密码对应的文件
-b 将密码跟在用户名后
非第一次
htpasswd 文件路径 姓名 交互式生成密码
htpasswd -b 文件路径 姓名 密码 直接将密码跟在后面
第一次
[root@localhost conf.d]#htpasswd -bc /apps/nginx/conf.d/.httpuser kgc 123456
#没有 yum install httpd-tools -b 将名字密码跟在后面 -c 新建文件
Adding password for user kgc
[root@localhost conf.d]#cat /apps/nginx/conf.d/.httpuser
kgc:$apr1$RtjFE23R$lf2n.YpdssSf1EYJbIDqT0
非第一次
[root@localhost error]#htpasswd /apps/nginx/conf.d/.httpuser xiaoming
New password:
Re-type new password:
Adding password for user xiaoming
4.3.8 自定义 错误页面
我们 可以改变 默认的错误页面,同时也可以用指定的响应状态码进行响应, 可用位置:http, server, location, if in location
格式:
error_page code ... [=[response]] uri;
页面错误代码
error_page 固定写法
code 响应码
= 可以将响应码转换
uri 访问连接
实际操作:
www.pc.com/xxxxxxxx 404
www.pc.com/40x.html
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/html/pc;
error_page 404 /40x.html;
#当出现404 错误 就去 root /data/nginx/html/pc/error/ 这个文件夹找40x.html 这个文件
location = /40x.html {
root /data/nginx/html/pc/error/;
}
location / {
root /data/nginx/html/pc;
}
location /admin{
auth_basic "admin site";
auth_basic_user_file /apps/nginx/conf.d/.httpuser;
}
}
[root@localhost nginx]#mkdir /data/nginx/html/pc/error/
#建立目录
[root@localhost nginx]#cd /data/nginx/html/pc/error/
#切换目录
[root@localhost nginx]#vim 40x.html
#新建页面,此处页面名字需要和 配置文件中的一致
自定义 错误码
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/html/pc;
error_page 404 =302 /40x.html;
#把错误码 404 指定成302 注意此处的 40x.html 需要真实存在建立的页面必须一致
location = /40x.html {
root /data/nginx/html/pc/error/;
}
location / {
root /data/nginx/html/pc;
}
location /admin{
auth_basic "admin site";
auth_basic_user_file /apps/nginx/conf.d/.httpuser;
}
}
#出了错 直接跳转到显示主站点
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/html/pc;
error_page 404 =302 /index.html;
#把错误码 404 指定成302 并跳到主页面:/index.html
4.3.9 日志位置存放
#格式
Syntax: error_log file [level];
error_log /apps/nginx/logs/kgc_error.log;
固定格式 文件路径 级别(info debug等 可以忽略不写)
Default:
error_log logs/error.log error;
Context: main, http, mail, stream, server, location
level: debug, info, notice, warn, error, crit, alert, emerg
[root@centos7 ~]# mkdir /data/nginx/logs
listen 80;
server_name www.kgc.org;
error_page 500 502 503 504 404 /error.html;
access_log /apps/nginx/logs/kgc_access.log;
error_log /apps/nginx/logs/kgc_error.log;
#定义错误日志文件
#重启nginx并访问不存在的页面进行测试并验证是在指定目录生成新的日志文件
[root@localhost ~]#mkdir /apps/nginx/logs
[root@localhost ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]#nginx -s reload
实际操作,将两个网站的 日志分离
[root@localhost error]#vim /apps/nginx/conf.d/m.conf
server{
listen 80;
server_name www.m.com;
root /data/nginx/m/;
error_log /data/logs/m_error.log;
access_log /data/logs/m_access.log;
}
[root@localhost error]#vim /apps/nginx/conf.d/pc.conf
server{
listen 80;
server_name www.pc.com;
root /data/nginx/pc;
error_log /data/logs/pc_error.log;
access_log /data/logs/pc_access.log;
}
[root@localhost error]#mkdir /data/logs
[root@localhost error]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost error]#nginx -s reload
查看日志是否生效
4.3.10 检测文件是否存在
try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误。
语法格式
Syntax: try_files file ... uri;
try_files file ... =code;
Default: —
Context: server, location
www.baidu.com/test
例子:
location / {
root /data/nginx/html/pc;
try_files $uri $uri.html $uri/index.html /about/default.html;
#try_files $uri $uri/index.html $uri.html =489;
}
www.pc.com/abc
我的访问是路径下的abc文件,那按照设置,先会去找abc,没有abc会在后面加上abc.html,再没有会在路径后补上abc/index.html
最后没有会有一个兜底的
[root@centos8 ~]# echo "default page" >> /data/nginx/html/pc/about/default.html
#重启nginx并测试,当访问到http://www.kgc.org/about/xx.html等不存在的uri会显示default.html,如果是自定义的状态码则会显示在返回数据的状态码中
#注释default.html行,启用上面489行生效后,再观察结果
[root@centos8 ~]# curl -I http://www.kgc.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
例子 解释
#一定要有兜底否则也还是报错 我测试的结果 kgc.html是兜底的可能版本不一
location / {
root /data/nginx/html/pc;
try_files $uri $uri.html $uri/index.html /about/default.html;
#你如果 访问一个路径 /kgc 如果没有这个页面 先去找kgc kgc.html 然后再去找 kgc/index.html 最后再 找/about/default.html
}
实际操作:
[root@localhost pc]#vim /apps/nginx/conf.d/pc.conf
server{
listen 80;
server_name www.pc.com;
root /data/nginx/pc;
location / {
root /data/nginx/pc/;
try_files $uri $uri.html $uri/index.html /about/default.html;
}
}
###注意 /about/default.html; 这边的根是 /data/nginx/pc/ 下
#所有的文件 都在主目录下
去第二台机器
curl www.pc.com/kgc
#####后面还可以自定义设置返回响应码######
server{
listen 80;
server_name www.pc.com;
root /data/nginx/pc;
location / {
root /data/nginx/pc/;
try_files $uri $uri.html $uri/index.html =489;
}
}
########自定义和重新定义页面##############
server{
listen 80;
server_name www.pc.com;
root /data/nginx/pc;
location / {
root /data/nginx/pc/;
try_files $uri $uri.html $uri/index.html =489;
}
error_page 489 /40x.html;
location = /40x.html {
root /data/nginx/error/;
}
}
4.3.11 长连接
http 基于 tcp 协议 先要 三次握手然后 再传输数据
一次三次握手 下载多个资源
一次三次握手下载一个资源
相关设置
keepalive_timeout timeout [header_timeout];
#设定保持连接超时时长,0表示禁止长连接,默认为75s,通常配置在http字段作为站点全局配置
keepalive_requests number;
#在一次长连接上所允许请求的资源的最大数量,默认为100次,建议适当调大,比如:500
可以加在全局或者 server
例子:
keepalive_requests 3;
#最大下载三个资源就会断开
keepalive_timeout 60 65; #只能有一个空格
#开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,后面的60为发送给客户端应答报文头部中显示的超时时间设置为60s:如不设置客户端将不显示超时时间。
Keep-Alive:timeout=60 #浏览器收到的服务器返回的报文
#如果设置为0表示关闭会话保持功能,将如下显示:
Connection:close #浏览器收到的服务器返回的报文
#使用命令测试:
[root@centos8 ~]# telnet www.kgc.org 80
Trying 10.0.0.8...
Connected to www.kgc.org.
Escape character is '^]'.
GET / HTTP/1.1
HOST: www.kgc.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
B 对哪种浏览器禁用长连接
keepalive_disable none | browser ...;
#对哪种浏览器禁用长连接
实际操作:
[root@centos8 ~]# telnet www.kgc.org 80
Trying 10.0.0.8...
Connected to www.kgc.org.
Escape character is '^]'.
#请求头
GET / HTTP/1.1 手打
HOST: www.kgc.org 手打
回车
4.3.12 作为下载服务器配置
ngx_http_autoindex_module 模块处理以斜杠字符 "/" 结尾的请求,并生成目录列表,可以做为下载服务
配置使用
官方文档:
http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
[root@localhost nginx-1.18.0]#./configure --help |grep auto
#自带
--without-http_autoindex_module disable 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
#变量提供 限制 变量优先级高
例子实现
[root@centos8 ~]# mkdir /opt/download
#注意:download不需要index.html文件
location /download {
autoindex on;
#开启下载服务器
autoindex_exact_size on;
#开启确切大小不建议开启
autoindex_localtime on;
#使用当地时间
limit_rate 1024k;
#所有人限速1024k,默认单位是字节数
set $limit_rate 2M;
#谁先生效
alias /opt/download;
}
直接访问页面
location /download {
autoindex on;
}
location /download {
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
limit_rate 1024k;
alias /opt/download;
}
4.3.13 用户上传资料
上传需要借助开发小的程序, 并且程序 5M 和 nginx 10M 都会限制。 两者取最小
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位作为目录名
上传文件大于限制 错误代码413
4.3.14 其他设置
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;
#缓存错误信息
limit_except method ... { ... },仅用于location
#限制客户端使用除了指定的请求方法之外的其它方法
method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND,
PROPPATCH, LOCK, UNLOCK, PATCH
limit_except GET {
allow 192.168.91.101;
deny all;
}
#除了GET和HEAD 之外其它方法仅允许192.168.1.0/24网段主机使用
5 高级配置
5.1 网页的状态页
基于nginx 模块 ngx_http_stub_status_module 实现,在编译安装nginx的时候需要添加编译参数 --with-http_stub_status_module,否则配置完成之后监测会是提示语法错误注意: 状态页显示的是整个服务器的状态,而非虚拟主机的状态
#配置示例:
location /nginx_status {
#stub_status;
auth_basic "auth login";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
allow 192.168.0.0/16;
allow 127.0.0.1;
deny all;
}
while : ;do ab -c 1000 -n 10000 http://192.168.91.100/;sleep 1;done
#压力测试 模拟高链接
[root@localhost ~]#curl http://www.kgc.com/nginx_status 2>/dev/null |awk '/Reading/{print $2,$4,$6}'
#
[root@localhost ~]#curl http://用户名:密码@www.kgc.com/nginx_status 2>/dev/null |awk '/Reading/{print $2,$4,$6}'
#curl http://www.pc.com/status 2> /dev/null |awk '/Reading/{print $2,$4,$6}'
实际操作:
只需要添加 location
location /nginx_status {
stub_status;
}
#状态页用于输出nginx的基本状态信息:
#输出信息示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections:
#当前处于活动状态的客户端连接数,包括连接等待空闲连接数=reading+writing+waiting
accepts:
#统计总值,Nginx自启动后已经接受的客户端请求的总数。
handled:
#统计总值,Nginx自启动后已经处理完成的客户端请求总数,通常等于accepts,除非有因worker_connections限制等被拒绝的连接
requests:
#统计总值,Nginx自启动后客户端发来的总的请求数。
Reading:
#当前状态,正在读取客户端请求报文首部的连接的连接数,数值越大,说明排队现象严重,性能不足
Writing:
#当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大
Waiting:
#当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于active – (reading+writing)
##############为了安全考虑#####################
增加验证模块
server{
listen 80;
server_name www.pc.com;
root /data/nginx/pc;
location /admin{
stub_status;
auth_basic "admin site";
auth_basic_user_file /apps/nginx/conf.d/.httpuser;
}
}
5.2 Nginx 第三方模块
5.2.1 ehco 模块
开源的echo模块 https://github.com/openresty/echo-nginx-module
location /ip {
default_type text/html;
echo "welcome, your ip addr: ";
echo $remote_addr;
}
[root@localhost opt]#yum install git -y
#安装git
[root@localhost opt]#git clone https://github.com/openresty/echo-nginx-module.git
./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/opt/echo-nginx-module-master
make && make install
[root@localhost ~]#nginx -t
nginx: [emerg] unknown directive "echo" in /apps/nginx/conf.d/pc.conf:26
nginx: configuration file /apps/nginx/conf/nginx.conf test failed
[root@localhost nginx-1.18.0]#ll /apps/nginx/sbin/
总用量 15808
-rwxr-xr-x. 1 root root 8407568 2月 19 15:35 nginx
-rwxr-xr-x. 1 nginx nginx 7774624 2月 14 17:02 nginx.old
[root@localhost nginx-1.18.0]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost nginx-1.18.0]#systemctl stop nginx
[root@localhost nginx-1.18.0]#systemctl start nginx
location /hello {
default_type text/html;
echo "hello world ";
}
yum install git -y
cd /opt
git clone https://github.com/openresty/echo-nginx-module.git
cd /opt/nginx-1.18.0
./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/opt/echo-nginx-module-master
make -j 2
make install
systemctl stop nginx
systemctl start nginx
vim /apps/nginx/conf.d/pc.conf
location /hello {
default_type text/html;
echo "hello world ";
}
第二台机器
curl www.pc.com/hello
5.3 变量
http://nginx.org/en/docs/varindex.html
官方文档
5.3.1 内置
常用内置变量
$remote_addr; #存放了客户端的地址,注意是客户端的公网IP
$proxy_add_x_forwarded_for
#此变量表示将客户端IP追加请求报文中X-Forwarded-For首部字段,多个IP之间用逗号分隔,如果请求中没有X-Forwarded-For,就使用$remote_addrthe “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.
客户机 代理1 代理2 nginx服务器
$proxy_add_x_forwarded_for: 在代理1 上存的是 客户机的ip
$proxy_add_x_forwarded_for: 在代理2 上存的是 客户机的ip,代理1的ip 用逗号隔开
$proxy_add_x_forwarded_for: nginx 上存的是 客户机的ip,代理1的ip,代理2的ip
$args;
#变量中存放了URL中的参数,例如:http://www.kgc.org/main/index.do?id=20190221&partner=search
#返回结果为: id=20190221&partner=search 存放的就是这个
select * from table where id=20190221
$document_root; #保存了针对当前资源的请求的系统根目录,例如:/apps/nginx/html
$document_uri; #保存了当前请求中不包含参数的URI,注意是不包含请求的指令,比
如:http://www.kgc.org/main/index.do?id=20190221&partner=search会被定义为/main/index.do #返回结果为:/main/index.do
$host; #存放了请求的host名称
limit_rate 10240;
echo $limit_rate;
#如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0
$remote_port; #客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口
$remote_user; #已经经过Auth Basic Module验证的用户名
$request_body_file; #做反向代理时发给后端服务器的本地资源的名称
$request_method; #请求资源的方式,GET/PUT/DELETE等
$request_filename;
#当前请求的资源文件的磁盘路径,由root或alias指令与URI请求生成的文件绝对路径,如:/apps/nginx/html/main/index.html
$request_uri; https:// www.baidu.com/main/index.do?id=20190221&partner=search
#包含请求参数的原始URI,不包含主机名,相当于:$document_uri?$args,例如:/main/index.do?id=20190221&partner=search
$scheme; #请求的协议,例如:http,https,ftp等
$server_protocol;
#保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr; #保存了服务器的IP地址
$server_name; #请求的服务器的主机名
$server_port; 443 https #请求的服务器的端口号
$http_<name>
#name为任意请求报文首部字段,表示记录请求报文的首部字段
arbitrary request header field; the last part of a variable name is the field name converted to lower case with dashes replaced by underscores
#用下划线代替横线
#示例: echo $http_User_Agent;
$http_user_agent; #客户端浏览器的详细信息
$http_cookie; #客户端的cookie信息
$cookie_<name> #name为任意请求报文首部字部cookie的key名
$http_<name>
#name为任意请求报文首部字段,表示记录请求报文的首部字段,ame的对应的首部字段名需要为小写,如果有
横线需要替换为下划线
arbitrary request header field; the last part of a variable name is the field
name converted to lower case with dashes replaced by underscores #用下划线代替横线
#示例:
echo $http_user_agent;
echo $http_host;
$sent_http_<name>
#name为响应报文的首部字段,name的对应的首部字段名需要为小写,如果有横线需要替换为下划线,此变量有问题
echo $sent_http_server;
$arg_<name>
#此变量存放了URL中的指定参数,name为请求url中指定的参数
#对比 变量 $arg 是全部, 如果 要id 如下
echo $arg_id;
location /main {
index index.html;
default_type text/html;
echo "hello world,main-->";
echo $remote_addr;
echo $args;
echo $document_root;
echo $document_uri;
echo $host;
echo $http_user_agent;
echo $http_cookie;
echo $request_filename;
echo $scheme;
echo $scheme://$host$document_uri?$args;
}
实际操作
location /main {
index index.html;
default_type text/html;
echo "hello world,main-->";
echo $remote_addr;
echo $args;
echo $arg_user
echo $document_root;
echo $document_uri;
echo $host;
echo $http_user_agent;
echo $http_cookie;
echo $request_filename;
echo $scheme;
echo $scheme://$host$document_uri?$args;
}
加入此段后 去主机2上进行测试
curl http://www.pc.com/main
curl 'http://www.pc.com/main?user=zhou&title=cto'
curl -b uid=100 'http://www.pc.com/main?user=zhou&title=cto'
-b 加上cookie
5.3.2自定义变量
假如需要自定义变量名称和值,使用指令set $variable value;
语法格式:
Syntax: set $variable value;
Default: —
Context: server, location, if
范例:
set $name kgc;
echo $name;
set $my_port $server_port;
echo $my_port;
echo "$server_name:$server_port"; #输出信息如下
[root@centos6 ~]#curl www.kgc.org/main
kgc
80
www.kgc.org:80
######################################################
实际例子:
location /test {
set $name kgc;
echo $name;
set $my_port $server_port;
echo $my_port;
}
5.4 自定义访问日志
5.4.1日志的格式 可以自由指定
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$server_name:$server_port';
log_format test '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$server_name:$server_port';
格式可以定义多个
###注意如果开启 include 注意定义自配置文件与 日志格式的上下关系 , 日志格式一定要在 include 之前 否则会不生效。
5.4.2自定义json 格式日志
方便ELK收集日志
vim /apps/nginx/conf/nginx.conf
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
vim /apps/nginx/conf.d/pc.conf
location / {
root /data/nginx/pc/;
access_log logs/access.log access_json;
}
tail -f /apps/nginx/logs/access.log
主机2 去访问问 www.pc.com
'"http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTMLe/101.0.4951.54 Safari/537.36",' '"status":"304"}'
cat /var/log/nginx/access.log |sed -r 's/.*(status)":"(.*)"}.*/\2/'
利用脚本分析返回码
[root@localhost ~]#cat /var/log/nginx/access.log |sed -r 's/.*(status)":"(.*)"}.*/\2/'|sort -n|uniq -c
#统计状态码个数
6 200
26 304
28 404
cat /var/log/nginx/access.log |awk -F: '{print $6}'|sed -r 's/"(.*)",.*/\1/'
利用脚本提取ip地址
#!/usr/bin/env python3
#coding:utf-8
status_200= []
status_404= []
with open("access_json.log") as f:
for line in f.readlines():
line = eval(line)
if line.get("status") == "200":
status_200.append(line.get)
elif line.get("status") == "404":
status_404.append(line.get)
else:
print("状态码 ERROR")
print((line.get("clientip")))
f.close()
print("状态码200的有--:",len(status_200))
print("状态码404的有--:",len(status_404))
5.5 Nginx压缩功能
支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,这样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相应的CPU资源。Nginx对文件的压缩功能是依赖于模块 ngx_http_gzip_module
官方文档: https://nginx.org/en/docs/http/ngx_http_gzip_module.html
配置指令如下:
#启用或禁用gzip压缩,默认关闭
gzip on | off;
#压缩比由低到高从1到9,默认为1
gzip_comp_level level;
#禁用IE6 gzip功能
gzip_disable "MSIE [1-6]\.";
#gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
#启用压缩功能时,协议的最小版本,默认HTTP/1.1
gzip_http_version 1.0 | 1.1;
#指定Nginx服务需要向服务器申请的缓存空间的个数和大小,平台不同,默认:32 4k或者16 8k;
gzip_buffers number size;
#指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错
gzip_types mime-type ...;
#如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”,一般建议打开
gzip_vary on | off;
#预压缩,先压缩好,不用临时压缩,消耗cpu
gzip_static on | off;
#重启nginx并进行访问测试压缩功能
[root@centos8 ~]# cp /apps/nginx/logs/access.log /data/nginx/html/pc/m.txt
[root@centos8 ~]# echo "test" > /data/nginx/html/pc/test.html #小于1k的文件测试是否会压缩
[root@centos8 ~]# vim /apps/nginx/conf/nginx.conf
gzip on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_types text/plain application/javascript application/x-javascript text/css
application/xml text/javascript application/x-httpd-php image/gif image/png;
gzip_vary on;
#重启Nginx并访问测试:
[root@centos8 ~]# curl --head --compressed 192.168.91.100/test.html
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 22 Feb 2019 01:52:23 GMT
Content-Type: text/html
Last-Modified: Thu, 21 Feb 2019 10:31:18 GMT
Connection: keep-alive
Keep-Alive: timeout=65
Vary: Accept-Encoding
ETag: W/"5c6e7df6-171109"
Content-Encoding: gzip #压缩传输
#验证不压缩访问的文件大小:
5.5.1实际操作
太小的文件没必要压缩,压缩说不定变大了
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
gzip on;
gzip_comp_level 9;
gzip_min_length 1k;
gzip_vary on;
.................
################开启预压缩##########################
gzip 需要的访问的文件
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
gzip on;
gzip_comp_level 9;
gzip_min_length 1k;
gzip_vary on;
gzip_static on;
访问 www.pc.com/文件名
5.6 https 功能
Web网站的登录页面都是使用https加密传输的,加密数据以保障数据的安全,HTTPS能够加密信息,以免敏感信息被第三方获取,所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议,HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据。
nginx 的https 功能基于模块ngx_http_ssl_module实现,因此如果是编译安装的nginx要使用参数ngx_http_ssl_module开启ssl功能,但是作为nginx的核心功能,yum安装的nginx默认就是开启的,编译安装的nginx需要指定编译参数--with-http_ssl_module开启
官方文档:
https://nginx.org/en/docs/http/ngx_http_ssl_module.html
参数 https
ssl on | off;
#为指定的虚拟主机配置是否启用ssl功能,此功能在1.15.0废弃,使用listen [ssl]替代
listen 443 ssl;
ssl_certificate /path/to/file;
#指向包含当前虚拟主机和CA的两个证书信息的文件,一般是crt文件
ssl_certificate_key /path/to/file;
#当前虚拟主机使用的私钥文件,一般是key文件
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
#支持ssl协议版本,早期为ssl现在是TLS,默认为后三个
ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
#配置ssl缓存
off: #关闭缓存
none: #通知客户端支持ssl session cache,但实际不支持
builtin[:size]:#使用OpenSSL内建缓存,为每worker进程私有
[shared:name:size]:#在各worker之间使用一个共享的缓存,需要定义一个缓存名称和缓存空间大小,一兆可以存储4000个会话信息,多个虚拟主机可以使用相同的缓存名称
ssl_session_timeout time;
#客户端连接可以复用ssl session cache中缓存的有效时长,默认5m
自签名证书
#所有的证书需要放在一起不能移开
bash certificate.sh
cat kgc.com.crt ca.crt > www.kgc.com.crt
mv kgc.com.key www.kgc.com.key
ll www.kgc.com*
server{
listen 80;
listen 443 ssl;
ssl_certificate /apps/nginx/conf.d/ssl/www.kgc.com.crt;
ssl_certificate_key /apps/nginx/conf.d/ssl/www.kgc.com.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
server_name www.kgc.com;
root /data/nginx/pc/;
gzip on;
gzip_comp_level 9;
gzip_min_length 1k;
gzip_static on;
}
location / {
root /apps/nginx/html;
if ( $scheme = http) {
rewrite ^/(.*)$ https://www.kgc.com/$1 redirect;
}
}
5.7 自定义图标
favicon.ico 文件是浏览器收藏网址时显示的图标,当客户端使用浏览器问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错
#方法一:服务器不记录访问日志:
location = /favicon.ico {
log_not_found off;
access_log off;
}
#方法二:将图标保存到指定目录访问:
#location ~ ^/favicon\.ico$ {
location = /favicon.ico {
root /data/nginx/html/pc/images;
expires 365d; #设置文件过期时间
}
【定制小图标】
wget www.baidu.com/favicon.ico
放到主目录就可以了
不生效可以重新打开浏览器
6 重写功能 rewrite
Nginx服务器利用 ngx_http_rewrite_module 模块解析和处理rewrite请求,此功能依靠 PCRE(perl compatible regular expression),因此编译之前要安装PCRE库,rewrite是nginx服务器的重要功能之
一,用于实现URL的重写,URL的重写是非常有用的功能,比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需其他网站修改我们的链接,就可以设置为自动访问,另外还可以在一定程度上提高网站的安全性。
6.1 ngx_http_rewrite_module模块指令
官方文档: https://nginx.org/en/docs/http/ngx_http_rewrite_module.html
6.1.1 if指令
官方文档:
https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if
用于条件匹配判断,并根据条件判断结果选择不同的Nginx配置,可以配置在server或location块中进行配置,Nginx的if语法仅能使用if做单次判断,不支持使用if else或者if elif这样的多重判断,用法如下:
if (条件匹配) {
action
}
使用正则表达式对变量进行匹配,匹配成功时if指令认为条件为true,否则认为false,变量与表达式之间使用以下符号链接:
= #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false
!= #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false
~ #区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~ #区分大小写字符,判断是否匹配,不满足匹配条件为真,满足匹配条件为假
~* #不区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~* #不区分大小字符,判断是否匹配,满足匹配条件为假,不满足匹配条件为真
-f 和 !-f #判断请求的文件是否存在和是否不存在
-d 和 !-d #判断请求的目录是否存在和是否不存在
-x 和 !-x #判断文件是否可执行和是否不可执行
-e 和 !-e #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)
#注意:
#如果$变量的值为空字符串或0,则if指令认为该条件为false,其他条件为true。
#nginx 1.0.1之前$变量的值如果以0开头的任意字符串会返回false
#示例:
http://www.baidu.com
3字打头重定向
301 永久重定向 将缓存记录在浏览器中
302 临时重定向 没有缓存 每次都要重定向
304
location /main {
index index.html;
default_type text/html;
if ( $scheme = http ){
echo "if-----> $scheme";
}
if ( $scheme = https ){
echo "if ----> $scheme";
}
#if (-f $request_filename) {
# echo "$request_filename is exist";
#}
if (!-e $request_filename) {
echo "$request_filename is not exist";
#return ;
}
}
【用户访问的文件不存在 直接返回主页】
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/pc/;
location / {
root /data/nginx/pc/;
}
location /test {
default_type text/plain;
return 301 http://www.baidu.com;
}
location /main {
index index.html;
default_type text/html;
if ( $scheme = http ){
return 666 "if-----> $scheme";
}
if (!-e $request_filename){
return 302 /index.html; #如果用户不存在直接跳转到主页面
}
}
}
www.kgc.com/main/xxxxx
#注意访问的main下的不存在 目录
#注意前面的if语句执行后会 停止匹配
【想控制所有网站可以 放到前面】
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/pc/;
if (!-e $request_filename){
return 302 /index.html; #如果用户不存在直接跳转到主页面
}
location / {
root /data/nginx/pc/;
}
location /test {
default_type text/plain;
return 301 http://www.baidu.com;
}
location /main {
index index.html;
default_type text/html;
if ( $scheme = http ){
return 666 "if-----> $scheme";
}
}
}
6.1.2 return
return用于完成对请求的处理,并直接向客户端返回响应状态码,比如:可以指定重定向URL(对于特殊重定向状态码,301/302等) 或者是指定提示文本内容(对于特殊状态码403/500等),处于此指令后的所有配置都将不被执行,return可以在server、if 和 location块进行配置
语法格式:
www.kgc.com/test/
404
return code; #返回给客户端指定的HTTP状态码
return code [text]; #返回给客户端的状态码及响应报文的实体内容,可以调用变量,其中text如果有空格,需要用单或双引号
return code url; #返回给客户端的URL地址
范例:
location / {
root /data/nginx/html/pc;
default_type text/html;
index index.html;
if ( $scheme = http ){
#return 666;
#return 666 "not allow http";
#return 301 http://www.baidu.com;
return 500 "service error";
echo "if-----> $scheme"; #return后面的将不再执行
}
if ( $scheme = https ){
echo "if ----> $scheme";
}
}
##############################################################
例子1:
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/pc/;
location /{
root /data/nginx/pc/;
}
location /test { #访问test 直接返回403
return 403; #可以改成666
}
}
例子2:
location /test { #访问test 直接返回403
return 666 "hello"; #可以改成666自定义,hello是描述 文字可以 图形浏览器不可以
}
例子3:
location /test {
default_type text/plain; #定义文本格式后图形浏览器可以看见
return 666 "hello";
}
例子4:
location /test {
default_type text/plain;
return 302 http://www.baidu.com;
}
location /test {
default_type text/plain;
return 301 /index.html;
}
301 缓存在磁盘上,有些
302 没有缓存 , 服务器断开无法重定向 jd
6.1.3 set 指令
指定key并给其定义一个变量,变量可以调用Nginx内置变量赋值给key,另外set定义格式为set $key value,value可以是text, variables和两者的组合。
location /main {
root /data/nginx/html/pc;
index index.html;
default_type text/html;
set $name kgc;
echo $name;
set $my_port $server_port(nginx 自带的变量 服务端口 一般80);
echo $my_port;
}
6.1.4 break 指令
用于中断当前相同作用域(location)中的其他Nginx配置,与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效,位于后面的 ngx_http_rewrite_module 模块中指令就不再执行,Nginx服务器在根据配置处理请求的过程中遇到该指令的时候,回到上一层作用域继续向下读取配置,该指令可以在server块和locationif块中使用
注意: 如果break指令在location块中后续指令还会继续执行,只是不执行 ngx_http_rewrite_module 模块的指令,其它指令还会执行
使用语法如下:
if ($slow) {
limit_rate 10k;
break;
}
location /main {
root /data/nginx/html/pc;
index index.html;
default_type text/html;
set $name kgc;
echo $name;
break; #location块中break后面指令还会执行
set $my_port $server_port;
echo $my_port;
}
6.2 rewrite 指令
通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配,rewrite主要是针对用户请求的URL或者是URI做具体处理
官方文档:
https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite
% s/旧的/新的/
rewrite可以配置在 server、location、if
语法格式 :
rewrite可以配置在 server、location、if
语法格式 :
rewrite regex replacement(www.baidu.com) [flag];
正则匹配原始访问url 替代你想让客户访问的 标志 ()premanent301 redirect302 break last
rewrite将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为表达式指定的新的URI
注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查,隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]所表示的标志位用于控制此循环机制如果替换后的URL是以http://或https://开头,则替换结果会直接以重定向返回给客户端, 即永久重定向 301
正则表达式格式
. #匹配除换行符以外的任意字符
\w #匹配字母或数字或下划线或汉字
\s #匹配任意的空白符
\d #匹配数字 [0-9]
\b #匹配单词的开始或结束
^ #匹配字付串的开始
$ #匹配字符串的结束
* #匹配重复零次或更多次
+ #匹配重复一次或更多次
? #匹配重复零次或一次
(n) #匹配重复n次
{n,} #匹配重复n次或更多次
{n,m} #匹配重复n到m次
*? #匹配重复任意次,但尽可能少重复
+? #匹配重复1次或更多次,但尽可能少重复
?? #匹配重复0次或1次,但尽可能少重复
{n,m}? #匹配重复n到m次,但尽可能少重复
{n,}? #匹配重复n次以上,但尽可能少重复
\W #匹配任意不是字母,数字,下划线,汉字的字符
\S #匹配任意不是空白符的字符
\D #匹配任意非数字的字符
\B #匹配不是单词开头或结束的位置
[^x] #匹配除了x以外的任意字符
[^kgc] #匹配除了kgc 这几个字母以外的任意字符
rewrite flag 使用介绍
利用nginx的rewrite的指令,可以实现url的重新跳转,rewrtie有四种不同的flag,分别是redirect(临时重定向302)、permanent(永久重定向301)、break和last。其中前两种是跳转型的flag,后两种是代理型
- 跳转型指由客户端浏览器重新对新地址进行请求
- 代理型是在WEB服务器内部实现跳转
rewrite 格式
Syntax: rewrite regex replacement [flag]; #通过正则表达式处理用户请求并返回替换后的数据包。
Default: —
Context: server, location, if
flag 说明
redirect;302
#临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求;使用相对路径,或者http://或https://开头,状态码:302
permanent;301 www.bj.com www.beijing.com
#重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求,状态码:301
break; www.bj.com
#重写完成后,停止对当前URL在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环,建议在location中使用
#适用于一个URL一次重写
last;
#重写完成后,停止对当前URI在当前location中后续的其它重写操作,而后对新的URL启动新一轮重写检查,不建议在location中使用
#适用于一个URL多次重写,要注意避免出现超过十次以及URL重写后返回错误的给用户301
例子:
www.haishi.com www.hs.com
#访问 bj 跳转到 beijing
location /bj {
root /data/nginx/pc;
rewrite ^/bj/(.*) /beijing/$1 permanent;
}
此处的$1代表后项引用
location / {
root /data/nginx/html/pc;
index index.html;
rewrite / http://www.kgc.com permanent;
#rewrite / http://www.kgc.com redirect;
}
#重启Nginx并访问域名 http://www.kgc.org 进行测试
【rewrite】
rewrite regex replacement [flag];
指令 正则 替换 标志
【访问bj 等于 访问beijing】
location /bj {
rewrite ^/bj/(.*) /beijing/$1 permanent;
}
mkdir beijing
echo beijing > beijing/index.html
整个网页跳转 老域名跳转到新域名
location / {
root /data/nginx/pc/;
rewrite / http://www.accp.com permanent;
}
server {
listen 80;
server_name www.accp.com;
root /data/nginx/pc/accp/;
}
~
cd /data/nginx/pc/
mkdir accp
echo accp > accp/index.html
【break】
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/pc/;
if (!-e $request_filename){
return 302 /index.html;
}
location /test {
default_type text/plain;
set $name kgc;
return 666 $name;
}
location /main {
index index.html;
default_type text/html;
if ( $scheme = http ){
return 666 "if-----> $scheme";
}
}
location /bj {
rewrite ^/bj/(.*) /beijing/$1 break;
}
}
mkdir beijing
echo beijing > beijing/index.html
mkdir bj
echo bj > bj/index.html
301 302 请求后 告诉你重定向的域名, 让你重新发起请求
break 服务器缓存好网页直接让你访问,直接给你结果
实战案例 http 转https
server {
listen 443 ssl;
listen 80;
ssl_certificate /apps/nginx/certs/www.kgc.org.crt;
ssl_certificate_key /apps/nginx/certs/www.kgc.org.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
server_name www.kgc.org;
location / { #针对全站跳转
root /data/nginx/html/pc;
index index.html;
if ($scheme = http ){ #如果没有加条件判断,会导致死循环
rewrite / https://$host redirect;
} http://www.kgc.com https://www.kgc.com
}
location /login { #针对特定的URL进行跳转https
if ($scheme = http ){ #如果没有加条件判断,会导致死循环
rewrite / https://$host/login redirect;
}
}
}
6.3 防盗链
防盗链基于客户端携带的referer实现,referer是记录打开一个页面之前记录是从哪个页面跳转过来的标记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗链,referer就是之前的那个网站域名,正常的referer信息有以下几种:
none:#请求报文首部没有referer首部,比如用户直接在浏览器输入域名访问web网站,就没有referer信息。
blocked:#请求报文有referer首部,但无有效值,比如为空。
server_names:#referer首部中包含本主机名及即nginx 监听的server_name。
arbitrary_string:#自定义指定字符串,但可使用*作通配符。示例: *.kgc.org www.kgc.*
regular expression:#被指定的正则表达式模式匹配到的字符串,要使用~开头,例如:~.*\.kgc\.com
实现盗链
在二机器上:
systemctl stop firewalld
setenforce 0
yum install epel-release.noarch -y
yum install nginx
cd /usr/share/nginx/html
vim index.html
<html>
<body>
<h1>this is yunjisuan </h1>
<img src="http://www.kgc.com/a.jpg"/>
</body>
</html>
systemctl start nginx
vim /etc/nginx/nginx.conf
41 server_name www.accp.com;
修改41行如上
真机上 添加dns 解析
C:\Windows\System32\drivers\etc
打开 hosts 文件
第二台机器的IP地址 www.accp.com
真机上测试
www.accp.com 是否可以打开图片
第一胎服务器
vim /apps/nginx/conf.d/pc.conf
server{
listen 80;
server_name www.pc.com;
root /data/nginx/pc;
location / {
root /data/nginx/pc;
}
location ~* \.(jpg|gif|swf|jpeg|bmp)$ {
root /data/nginx/pc;
valid_referers none blocked *.pc.com pc.com;
if ( $invalid_referer ) {
#rewrite ^/ http://www.pc.com/error.png;
return 403;
}
}
}
}
cd /data/nginx/pc/
拖入两张图片 a.jpg error.png
再次测试www.accp.com 是否可以打开图片
www.kgc.com
www.accp.com
80
/data/nginx/kgc
/data/nginx/accp
server {
listen 80;
server_name www.kgc.com;
root /data/nginx/kgc;
}
server {
listen 80;
server_name www.accp.com;
root /data/nginx/accp;
}
实现防盗链
location ~* \.(jpg|gif|swf)$ {
root /data/nginx/pc;
valid_referers none blocked *.pc.com pc.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.pc.com/error.png;
#return 403
}
}
~* \.(jpg|gif|swf)$:这段正则表达式表示匹配不区分大小写,以.jpg 或.gif 或.swf 结尾的文件
Valid_referers:设置信任的网站,可以正常使用图片。
None :浏览器中 referer 为空的情况,就是直接在浏览器访问图片。
Blocked :referer 不为空的情况 ,但是值被代理或防火墙删除了,这些值不以 http://或https://开头。后面的网址或者域名:referer 中包含相关字符串的网址。
If 语句:如果链接的来源域名不在 valid_referers 所列出的列表中,$invalid_referer 为1,则执行后面的操作,即进行重写或返回 403 页面。
https://www.digitalocean.com/community/tools/nginx
正向代理: 代理的客户端 加快访问速度 squid
反向代理:负载均衡 安全 nginx haproxy lvs 硬件:F5
7反向代理
反向代理:reverse proxy,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。
Nginx 除了可以在企业提供高性能的web服务之外,另外还可以将 nginx 本身不具备的请求通过某种预定义的协议转发至其它服务器处理,不同的协议就是Nginx服务器与其他服务器进行通信的一种规范,主要在不同的场景使用以下模块实现不同的功能
ngx_http_proxy_module: #将客户端的请求以http协议转发至指定服务器进行处理
ngx_http_upstream_module #用于定义为proxy_pass,fastcgi_pass,uwsgi_pass等指令引用的后端服务器分组
ngx_stream_proxy_module:#将客户端的请求以tcp协议转发至指定服务器处理
ngx_http_fastcgi_module:#将客户端对php的请求以fastcgi协议转发至指定服务器助理
ngx_http_uwsgi_module: #将客户端对Python的请求以uwsgi协议转发至指定服务器处理
7.1 实现 http 反向代理
官方文档: https://nginx.org/en/docs/http/ngx_http_proxy_module.html,
7.1.1 http 协议反向代理
7.1.1.1 反向代理配置参数
#官方文档:https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
proxy_pass;
#用来设置将客户端请求转发给的后端服务器的主机,可以是主机名(将转发至后端服务做为主机头首部)、IP
地址:端口的方式
#也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module支持
#示例:
10.0.0.8/web
location /web {
index index.html;
proxy_pass http://10.0.0.18:8080; #8080后面无uri,即无 / 符号,需要将location后面url 附加到proxy_pass指定的url后面,此行为类似于root
#proxy_pass指定的uri不带斜线将访问的/web,等于访问后端服务器
http://10.0.0.18:8080/web/index.html,即后端服务器配置的站点根目录要有web目录才可以被访问
# http://nginx/web/index.html ==> http://10.0.0.18:8080/web/index.html
proxy_pass http://10.0.0.18:8080/; #8080后面有uri,即有 / 符号,相当于置换,即访问/web时实际返回proxy_pass后面uri内容.此行为类似于alias
#proxy_pass指定的uri带斜线,等于访问后端服务器的http://10.0.0.18:8080/index.html 内容返回给客户端
} # http://nginx/web/index.html ==> http://10.0.0.18:8080
#重启Nginx测试访问效果:
#curl -L http://www.kgc.org/web
#如果location定义其uri时使用了正则表达式模式(包括~,~*,但不包括^~),则proxy_pass之后必须不能使用uri; 即不能有/ ,用户请求时传递的uri将直接附加至后端服务器之后
server {
...
server_name HOSTNAME;
location ~|~* /uri/ {
proxy_pass http://host:port; #proxy_pass后面的url 不能加/
}
...
}
http://HOSTNAME/uri/ --> http://host/uri/
proxy_hide_header field;
#用于nginx作为反向代理的时候,在返回给客户端http响应时,隐藏后端服务器相应头部的信息,可以设置
在http,server或location块
#示例: 隐藏后端服务器ETag首部字段
location /web {
index index.html;
proxy_pass http://10.0.0.18:8080/;
proxy_hide_header ETag;
}
proxy_pass_header field;
#默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel等参数,如果
要传递的话则要使用 proxy_pass_header field声明将后端服务器返回的值传递给客户端
#field 首部字段大小不敏感
#示例:透传后端服务器的Server和Date首部给客户端,同时不再响应报中显示前端服务器的Server字段
proxy_pass_header Server;
proxy_pass_header Date;
proxy_pass_request_body on | off;
#是否向后端服务器发送HTTP实体部分,可以设置在http,server或location块,默认即为开启
proxy_pass_request_headers on | off;
#是否将客户端的请求头部转发给后端服务器,可以设置在http,server或location块,默认即为开启
7.1.1.2 **实战案例: **反向代理单台web 服务器
实现单台反向代理
7-1 代理服务器
vim /apps/nginx/conf.d/pc.conf
server{
listen 80;
server_name www.pc.com;
root /data/nginx/pc;
location / {
proxy_pass http://192.168.91.101;
}
}
7-2 真实服务器
yum install httpd -y
cd /var/www/html
echo "real server 101" > index.html
systemctl stop nginx
systemctl start httpd
7-3 客户机
vim /etc/hosts
192.168.91.100 www.pc.com
代理服务器
server{
listen 80;
server_name www.pc.com;
root /data/nginx/pc;
location / {
root /data/nginx/pc;
proxy_pass http://192.168.91.101;
}
}
真实服务器
yum install httpd -y
cd /var/www/html/
echo "real server httpd" >index.html
建立大文件查看连接情况
dd if=/dev/zero of=test.img count=1 bs=1G
找台服务器访问
curl www.pc.com
wget --limit-rate=1024 www.pc.com/test.img
修改端口号
真实服务器
httpd 服务器修改端口号
server{
listen 80;
server_name www.pc.com;
root /data/nginx/pc;
location / {
root /data/nginx/pc;
proxy_pass http://192.168.91.101:8080;
}
}
在真实服务器上 做防火墙规则
iptables -A INPUT -s 192.168.91.100 -j DROP
客户端再次访问 会出现504网关超时(有可能只是处理时间久,服务器不一定挂了),时间较长1分钟,没有定义代理超时时间
iptables -A INPUT -s 192.168.91.100 -j REJECT
客户端再次访问 会出现502,一般出现502 代表后端真实服务器挂了
针对某个uri 进行访问
要求:将用户对域 www.kgc.com的请求转发给后端服务器处理
[root@centos8 ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name www.kgc.com;
location ~* /api {
proxy_pass http://192.168.91.100/; # http://10.0.0.18/ 最后的 / 可加或不加
}
}
#重启Nginx 并访问测试
访问www.kgc.com 后端服务器
如果后端服务器无法连接((比如:iptables -AINPUT -s nginx_ip -j REJECT或者systemctl stop httpd)), 会出现下面提示
默认在1分钟内后端服务器无法响应(比如:iptables -AINPUT -s nginx_ip -j DROP),会显示下面的504超时提示
7.1.1.3 实战案例:指定location 实现反向代理 动静分离
server {
listen 80;
server_name www.kgc.org;
location / {
index index.html index.php;
root /data/nginx/html/pc;
}
location /api {
#proxy_pass http://10.0.0.18:80/; #注意有后面的/, 表示置换
proxy_pass http://192.168.91.101; #后面没有 / , 表示追加
}
}
server {
......
location ~* \.(jpe?g|png|bmp|gif)$ {
proxy_pass http://192.168.91.103; #如果加/ 语法出错
}
}
#如果 http://10.0.0.18/ 有 / 语法出错
server{
listen 80;
server_name www.pc.com;
root /data/nginx/pc;
location / {
root /data/nginx/pc;
}
location /api {
root /data/nginx/pc;
proxy_pass http://192.168.91.101:8080;
}
location ~* \.(jpg|jpeg|png|gif|bmp)$ {
root /data/nginx/pc;
proxy_pass http://192.168.91.102;
}
}
7.1.1.4反向代理示例:缓存功能
关闭后端服务器后,图片无法访问
缓存功能默认关闭,需要开启
proxy_cache zone_name | off; 默认off
#指明调用的缓存,或关闭缓存机制;Context:http, server, location
#zone_name 表示缓存的名称.需要由proxy_cache_path事先定义
proxy_cache_key string;
#缓存中用于“键”的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri;
proxy_cache_valid [code ...] time;
#定义对特定响应码的响应内容的缓存时长,定义在http{...}中
示例:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_path;
#定义可用于proxy功能的缓存;Context:http 必须放在http语句中
proxy_cache_path path [levels=levels] [use_temp_path=on|off]
keys_zone=zone_name:size [inactive=time] [max_size=size] [manager_files=number]
[manager_sleep=time] [manager_threshold=time] [loader_files=number]
[loader_sleep=time] [loader_threshold=time] [purger=on|off]
[purger_files=number] [purger_sleep=time] [purger_threshold=time];
#示例:在http配置定义缓存信息
proxy_cache_path /var/cache/nginx/proxy_cache #定义缓存保存路径,proxy_cache会自动创建
levels=1:2:2 #定义缓存目录结构层次,1:2:2可以生成2^4x2^8x2^8=2^20=1048576个目录
keys_zone=proxycache:20m #指内存中缓存的大小,主要用于存放key和metadata(如:使用次数),一般1M可存放8000个左右的key
inactive=120s #缓存有效时间
max_size=10g; #最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值
#调用缓存功能,需要定义在相应的配置段,如server{...};或者location等
proxy_cache proxycache;
proxy_cache_key $request_uri; #对指定的数据进行MD5的运算做为缓存的key
proxy_cache_valid 200 302 301 10m; #指定的状态码返回的数据缓存多长时间
proxy_cache_valid any 1m; #除指定的状态码返回的数据以外的缓存多长时间,必须设置,否则不会缓存
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ; #默认是off
#在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端
#示例
proxy_cache_use_stale error http_502 http_503;
proxy_cache_methods GET | HEAD | POST ...;
#对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存
扩展知识:清理缓存
方法1: rm -rf 缓存目录
方法2: 第三方扩展模块ngx_cache_purge
例子:
proxy_cache_path /data/nginx/proyxcache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
http 语句
1:1:1 16个二进制 2^16/2^16/2^16 2^48
server {
listen 80;
proxy_cache proxycache;
proxy_cache_key $request_uri;
#proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 302 301 10m;
proxy_cache_valid any 5m;
server_name www.kgc.com;
root /data/nginx/pc;
location / {
root /data/nginx/pc;
}
location /api {
proxy_pass http://192.168.91.101:9527;
}
location ~* \.(jpg|png|gif|html)$ {
proxy_pass http://192.168.91.102;
}
}
实现缓存实际操作
在未开启缓存前可以测试下载速度
ab -c100 -n1000 http://www.pc.com/3m.jpg
去代理服务器上 添加操作
先去 主配置文件 的 http模块 中加入下方配置
proxy_cache_path /data/nginx/proyxcache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
再去 子配置文件中添加
server{
listen 80;
server_name www.pc.com;
proxy_cache proxycache;
proxy_cache_key $request_uri; www.kg.com/a.jpg
#proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 302 301 10m;
proxy_cache_valid any 5m;
root /data/nginx/pc;
location / {
root /data/nginx/pc;
}
location /api {
root /data/nginx/pc;
proxy_pass http://192.168.91.101:8080;
}
location ~* \.(jpg|jpeg|png|gif|bmp)$ {
root /data/nginx/pc;
proxy_pass http://192.168.91.102;
}
}
7.1.1.5 实现反向代理客户端 IP 透传
192.168.91.102 7-3 192.168.91.100 7-1 192.168.91.101 7-2
一级代理
多级代理 ip地址透传
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#var_proxy_add_x_forwarded_for
[root@centos8 ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name www.kgc.org;
location / {
index index.html index.php;
root /data/nginx/html/pc;
proxy_pass http://10.0.0.18;
#proxy_set_header X-Real-IP $remote_addr; #只添加客户端IP到请求报文头部,转发至后端服务器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #添加客户端IP和反向代理服务器IP到请求报文头部
}
}
#重启nginx
[root@centos7 ~]#systemctl restart nginx
#后端Apache配置:
[root@centos7 ~]#vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#重启apache访问web界面并验证apache日志
[root@centos8 ~]#cat /var/log/httpd/access_log
10.0.0.1 10.0.0.8 - - [05/Mar/2019:00:40:46 +0800] "GET / HTTP/1.0" 200 19 "-"
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/72.0.3626.119 Safari/537.36"
#Nginx配置:
[root@centos8 conf.d]# cat /apps/nginx/conf/nginx.conf
"$http_x_forwarded_for"' #默认日志格式就有此配置
#重启nginx访问web界面并验证日志格式:
10.0.0.8 - - [04/Mar/2019:16:40:51 +0800] "GET / HTTP/1.0" 200 24 "-"
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/72.0.3626.119 Safari/537.36" "10.0.0.1"
7.1.1.6 http反向代理负载均衡
在上一个节中Nginx可以将客户端的请求转发至单台后端服务器但是无法转发至特定的一组的服务器,而且不能对后端服务器提供相应的服务器状态监测,Nginx 可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能
官方文档: https://nginx.org/en/docs/http/ngx_http_up
#自定义一组服务器,配置在http块内
upstream web {
server 192.168.91.100 调度算法
server 192.168.91.101
}
location / {
pass_proxy http://web/
}
#示例
upstream backend {
server backend1.example.com weight=5; 权重
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
server backup1.example.com backup;
}
server address [parameters];
#配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。
#server支持的parameters如下:
weight=number #设置权重,默认为1,实现类似于LVS中的WRR,WLC等
max_conns=number #给当前后端server设置最大活动链接数,默认为0表示没有限制
max_fails=number #后端服务器的下线条件,当客户端访问时,对本次调度选中的后端服务器连续进行检测多少次,如果都失败就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性检查,而非周期性的探测
fail_timeout=time #后端服务器的上线条件,对已经检测到处于不可用的后端服务器,每隔此时间间隔再次进行检测是否恢复可用,如果发现可用,则将后端服务器参与调度,默认为10秒
backup #设置为备份服务器,当所有后端服务器不可用时,才会启用此备用服务器 sorry server 自己不能转自己
down #标记为down状态
resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx
hash KEY [consistent];
#基于指定请求报文中首部字段或者URI等key做hash计算,使consistent参数,将使用ketama一致性
www.kgc.com/test1
hash test1 103
hash算法,适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一
致性hash基于取模运算
hash $request_uri consistent; #基于用户请求的uri做hash
hash $cookie_sessionid #基于cookie中的sessionid这个key进行hash调度,实现会话绑定
ip_hash;
#源地址hash调度方法,基于的客户端的remote_addr(源地址IPv4的前24位或整个IPv6地址)做hash计算,以实现会话保持
least_conn;
#最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器,相当于LVS中的WLC
实际操作
http {
upstream web {
server 192.168.91.101:80;
server 192.168.91.103:80;
}
........
}
[root@localhost nginx]#vim conf.d/test.conf
server {
listen 80;
server_name www.kgc.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root /data/nginx/pc;
location / {
root /data/nginx/pc;
proxy_pass http://web;
}
}
8LNMP 架构
环境准备 lnmp 需要 安装 nginx mysql php 软件
systemctl disable --now firewalld
setenforce 0
8.1 安装依赖包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
8.2 创建运行用户、组
(Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)
useradd -M -s /sbin/nologin nginx
8.3 编译安装nginx
cd /opt
tar zxvf nginx-1.22.0.tar.gz -C /opt/
cd nginx-1.22.0/
./configure \
--prefix=/usr/local/nginx \ #指定nginx的安装路径
--user=nginx \ #指定用户名
--group=nginx \ #指定组名
--with-http_stub_status_module #启用 http_stub_status_module 模块以支持状态统计
cd nginx-1.22.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#让系统识别nginx的操作命令可以自动补全 nginx $PATH
添加Nginx系统服务 systemctl start nginx
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -1 $MAINPID
ExecStop=/bin/kill -3 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl damon-relaod
chmod 777 /lib/systemd/system/nginx.service
systemctl enable --now nginx.service
8.4 编译安mysql
准备好本次的安装包 :mysql-boost-5.7.20.tar.gz
1、安装Mysql环境依赖包
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake
yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel
2、创建运行用户
useradd -M -s /sbin/nologin mysql
3、编译安装
cd /opt
tar zxvf mysql-boost-5.7.20.tar.gz
cd /opt/mysql-5.7.20/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
make && make install
4、修改mysql 配置文件
vim /etc/my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
5、更改mysql安装目录和配置文件的属主属组
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
6、设置路径环境变量
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile
7、初始化数据库
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
8、添加mysqld系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
9、修改mysql 的登录密码
mysqladmin -u root -p password "abc123"
给root账号设置密码为abc123,提示输入的是原始密码(为空)要求输入的是原密码直接回车即可
10、授权远程登录
mysql -u root -p
8.5 编译安装php软件
php的编译安装
本次使用的php软件包为:php-7.1.10.tar.bz2
1、安装环境依赖包
yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
2、编译安装
cd /opt
tar jxvf php-7.1.10.tar.bz2
cd php-7.1.10
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make && make install
3、路径优化
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/
4、调整PHP配置文件
php有三个配置文件:
php.ini 主配置文件
php-fpm.conf 进程服务配置文件
www.conf 扩展配置文件
#调整主配置文件:
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
vim /usr/local/php/lib/php.ini
--1170行--修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
--939行--取消注释,修改
date.timezone = Asia/Shanghai
php -m #验证安装的模块
#调整进程服务配置文件:
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
--17行--去掉";"注释
pid = run/php-fpm.pid
#调整扩展配置文件:
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
5、启动php-fpm
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
netstat -anpt | grep 9000
#PHP-FPM(FastCGI Process Manager:FastCGI 进程管理器)是一个 PHPFastCGI 管理器, 由于Nginx服务器不能处理动态页面,需要由 Nginx 把动态请求交给 php-fpm 进程进行解析。
cd /opt/php-7.1.10/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system/php-fpm.service
systemctl restart php-fpm.service
8.6配置 Nginx 支持 PHP 解析
vim /usr/local/nginx/conf/nginx.conf
--65行--取消注释,修改
location ~ \.php$ {
root html; $document_root
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #将 /scripts 修改为nginx的工作目录
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #$document_root 代表当前请求在root指令中指定的值
include fastcgi_params;
}
systemctl restart nginx.service
验证PHP 测试页
vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
验证lnmp的是否搭建成功:
数据库的用户名
用户名@主机名
root@192.168.91.101
root@192.168.91.100
验证数据库工作是否正常
mysql -u root -p
CREATE DATABASE bbs;
GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
flush privileges;
vim /usr/local/nginx/html/index.php #替换原来的测试页内容
<?php
$link=mysqli_connect('192.168.233.21','bbsuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
浏览器访问
http://192.168.233.21/index.php
8.7安装论坛
【部署 Discuz!社区论坛 Web 应用】
cd /opt
unzip Discuz_X3.4_SC_UTF8.zip
cd /opt/dis/dir_SC_UTF8/
cp -r upload/ /usr/local/nginx/html/bbs/
调整论坛目录的权限:
cd /usr/local/nginx/html/bbs/
chown -R nginx ./config/
chown -R nginx ./data/
chown -R nginx ./uc_client/
chown -R nginx ./uc_server/
都要处理
chmod -R 777 ./config/
chmod -R 777 ./data/
chmod -R 777 ./uc_client/
chmod -R 777 ./uc_server/
论坛页面访问
http://192.168.91.100/bbs/install/index.php
----------------------------------------------------------------------------------------------------------
数据库服务器:localhost ###本地架设就用localhost,如何不是在在本机上就要填写IP地址和端口号
数据库名字:bbs
数据库用户名:bbsuser
数据库密码:admin123
管理员账号:admin
管理员密码:admin123
----------------------------------------------------------------------------------------------------------
安装完后访问论坛页面:
http://192.168.91.100/bbs/index.php
http://192.168.91.100/bbs/admin.php
8.8 安装博客
tar xf wordpress-5.4.1-zh_CN.tar.gz -C /opt/
cp -r wordpress /usr/local/nginx/html/
安装页面在 wp-admin 下
chmod 777 wordpress/ -R
mysql -u root -p
CREATE DATABASE blog;
GRANT all ON blog.* TO 'bloguser'@'%' IDENTIFIED BY 'admin123';
GRANT all ON blog.* TO 'bloguser'@'localhost' IDENTIFIED BY 'admin123';
flush privileges;
wordpress/wp-admin
http://192.168.91.100/wordpress/wp-admin/install.php
数据库名字:bbs
数据库用户名:bbsuser
数据库密码:admin123
select user,host from mysql.user;
http://192.168.91.100/wordpress/wp-admin/index.php
9内核参数调优
默认的Linux内核参数考虑的是最通用场景,不符合用于支持高并发访问的Web服务器的定义,根据业务特点来进行调整,当Nginx作为静态web内容服务器、反向代理或者提供压缩服务器的服务器时,内核参数的调整都是不同的,此处针对最通用的、使Nginx支持更多并发请求的TCP网络参数做简单的配置
修改/etc/sysctl.conf
fs.file-max = 1000000 #表示单个进程较大可以打开的句柄数
net.ipv4.tcp_tw_reuse = 1
#参数设置为 1 ,表示允许将TIME_WAIT状态的socket重新用于新的TCP链接,这对于服务器来说意义重大,因为总有大量TIME_WAIT状态的链接存在
net.ipv4.tcp_keepalive_time = 600
#当keepalive启动时,TCP发送keepalive消息的频度;默认是2小时,将其设置为10分钟,可更快的清理无效链接
net.ipv4.tcp_fin_timeout = 30
#当服务器主动关闭链接时,socket保持在FIN_WAIT_2状态的较大时间
net.ipv4.tcp_max_tw_buckets = 5000
#表示操作系统允许TIME_WAIT套接字数量的较大值,如超过此值,TIME_WAIT套接字将立刻被清除并打印警告信息,默认为8000,过多的TIME_WAIT套接字会使Web服务器变慢
net.ipv4.ip_local_port_range = 1024 65000
#定义UDP和TCP链接的本地端口的取值范围
net.ipv4.tcp_rmem = 10240 87380 12582912
#定义了TCP接受缓存的最小值、默认值、较大值
net.ipv4.tcp_wmem = 10240 87380 12582912
#定义TCP发送缓存的最小值、默认值、较大值
net.core.netdev_max_backlog = 8096
#当网卡接收数据包的速度大于内核处理速度时,会有一个列队保存这些数据包。这个参数表示该列队的较大值
net.core.rmem_default = 6291456 #表示内核套接字接受缓存区默认大小
net.core.wmem_default = 6291456 #表示内核套接字发送缓存区默认大小
net.core.rmem_max = 12582912 #表示内核套接字接受缓存区较大大小
net.core.wmem_max = 12582912 #表示内核套接字发送缓存区较大大小
注意:以上的四个参数,需要根据业务逻辑和实际的硬件成本来综合考虑
net.ipv4.tcp_syncookies = 1 #与性能无关。用于解决TCP的SYN攻击
net.ipv4.tcp_max_syn_backlog = 8192
#这个参数表示TCP三次握手建立阶段接受SYN请求列队的较大长度,默认1024,将其设置的大一些可使出现Nginx繁忙来不及accept新连接时,Linux不至于丢失客户端发起的链接请求
net.ipv4.tcp_tw_recycle = 1 #这个参数用于设置启用timewait快速回收
net.core.somaxconn=262114
#选项默认值是128,这个参数用于调节系统同时发起的TCP连接数,在高并发的请求中,默认的值可能会导致链接超时或者重传,因此需要结合高并发请求数来调节此值。
net.ipv4.tcp_max_orphans=262114
#选项用于设定系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤立链接将立即被复位并输出警告信息。这个限制指示为了防止简单的DOS攻击,不用过分依靠这个限制甚至认为的减小这个值,更多的情况是增加这个值
9.1 pam 资源限制
在/etc/security/limits.conf 最后增加:
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程