目录
源码安装
笔记为 nginx 1.21.6版本
源码包的下载:
wget http://nginx.org/download/nginx-1.21.6.tar.gz
环境配置(基础安装):
需要依赖(编译器):
在不知道需要哪些依赖的情况下 直接./configure
查看给的报错
如:
等。。。进行安装
yum install -y gcc
安装pcre库
yum install -y pcre pcre-deve1
安装zlib库
yum install -y zlib zlib-deve1
重新执行脚本文件,无错误或无需要安装的库后进行下一步。
执行脚本文件:
./configure \
--user=www \ ---指定的用户
--group=www \ ---指定的组
--prefix=/usr/local/nginx \ ---指定安装路径
--需要添加的模块
小提示:新建用户时 useradd www -U www 可创建同名用户组并加入,无需再次添加。
安装过程中注意提示信息,安装缺少的库
编译
make
编译安装
make install
基础管理命令:
/usr/local/nginx/sbin/nginx 启动 (根据你的安装位置)
/usr/local/nginx/sbin/nginx -s stop 快速停止(立刻停止服务)
/usr/local/nginx/sbin/nginx -s quit 优雅的结束(在停止前完成已经接受的连接请求,如有用户进行下载,下载完成后停止)
/usr/local/nginx/sbin/nginx -s reload 重载配置(一般在更改配置文件后使用)
检查是否启动:
网页查看:访问网页
端口查看:lsof -i:服务端口号
进程查看:ps -aux | grep 服务名
查看防火墙(测试环境中可直接关闭防火墙):外网记得配置进出站策略放行端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd reload
安装成系统服务(可用systemctl系统服务管理命令管理):就无需每次都去执行可执行文件了
创建服务脚本: vi /usr/lib/systemd/system/nginx.service
配置脚本内容:
[Unit]
Description=nginx web service
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
注意:nginx.pid 文件显示了服务启动后的pid号,并不是你指定的,而是随机的,只是将服务的pid号显示到了文件里(服务此次运行后的pid号)
其他服务也是有的
配置完成后记得关闭可执行文件启动的服务进程 /usr/local/nginx/sbin/nginx -s stop
重新加载系统服务:
systemctl daemon-reload
启动nginx服务:
systemctl start nginx
如果启动出错检查服务配置文件(nginx.service) 且利用systemctl status nginx 查看报错信息
例:
[root@apache system]# systemctl status nginx
● nginx.service - nginx web service
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Jun 18 14:55:10 apache systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Missing '='.
Jun 18 14:58:12 apache systemd[1]: [/usr/lib/systemd/system/nginx.service:8] Unknown lvalue 'ExecstartPre' in section 'Service'
Jun 18 14:58:12 apache systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Missing '='.
Jun 18 14:58:24 apache systemd[1]: [/usr/lib/systemd/system/nginx.service:8] Unknown lvalue 'ExecstartPre' in section 'Service'
Jun 18 14:58:24 apache systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Missing '='.
Jun 18 14:59:16 apache systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Missing '='.
Jun 18 14:59:25 apache systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Missing '='.
Jun 18 15:15:09 apache systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Missing '='.
Jun 18 15:15:21 apache systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Missing '='.
Jun 18 15:16:07 apache systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' in section 'Service'
出现了两个错误:8行的位置变量 ExecstartPre 12行的 "=" 消失
经检查:8行变量拼写错误:ExecstartPre 应为 ExecStartPre 12行变量后 少写一个 "="
更改配置文件后再次 systemctl daemon-reload
再次启动nginx服务 sysstemctl status nginx 发现启动成功
目录结构与基本运行原理
目录:
*_temp 运行后产生的临时文件目录
conf 配置文件目录
nginx.conf 主配置文件 内引用了其他配置文件
如:
就是引用了 mime.types 配置文件
logs 日志文件目录
sbin 执行文件目录(可执行程序,主程序,主进程)
html 网页目录(默认情况下的网页,和静态资源)
更改默认页:
更改默认html页面 /usr/local/nginx/html/index.html (在配置文件中可指定默认页面,在为配置的情况下就是这个,当然路径还是根据你的安装路径)
基本运行原理:
在执行sbin/nginx的主进程文件后:
启动一个Master主进程:它会加载及校验配置文件,如果配置文件没有错误将会开启它的子进程。
主进程不会处理连接请求,它的工作是协调子进程(worker)
用户请求发来后worker会进行解析,根据配置文件查看是否能够处理此请求
解析成功后读取文件,不成功则显示未找到页面等。。
如果更改配置文件,进行重载配置文件。
他会杀掉worker进程,但会留有一段的存活时间以完成正在连接的用户请求,且不会再接受新的请求,处理完成后此进程就会被杀掉。
nginx的配置文件
最小配置文件:#user nobody; 指定nginx启动时使用的用户 默认被注释掉
查看nginx默认不被注释的行:
[root@apache html]# sed -r '/.*#|^$/d' ../conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
详解:
worker_processes 1; 默认为1, ---表示开启一个worker进程
worker_connections 1024; ---默认为1024,表示每个worker进程可接受连接数为1024
include mime.types; ---引入http mime类型
mime文件:
制定了文件类型和后缀名。(告诉浏览器的)
default_type application/octet-stream; ---如果mime类型没有匹配上,默认使用二进制流的方式传输
比如点开网页就下载exe文件等,就是浏览器无法识别,自动用二进制流的方式进行的传输。
可以说是:无法识别的就会下载。(如果不想让他下载,就在mime中自己定义)
网络中传输的数据是01,浏览器是无法识别的,由服务器在http协议头中指定了文件类型
浏览器中检查头部信息可见:
图片:
静态页面:
js脚本:
等。
或使用curl -I 域名也可查看页面头部信息。
sendfile on; ---使用Linux的sendfile(socket、file、len)高效网络传输,也就是数据0拷贝。
减少了拷贝、调度、复制的过程,
关闭:
nginx去read你请求的文件,加载到nginx应用程序的内存里,读取完成后发送给linux的网络接口缓存,再发送给用户
(层层读取,层层复制)
开启:
通过nginx向操作系统内核发送一个信号:请求发到nginx,nginx发送信号(sendfile()内有socket、文件描述符等)到网络接口,网络接口来读取文件,直接通过网络发送给用户。(少了一层拷贝的过程)
keepalive_timeout 65; ---保持连接到超时
下面的
server {
listen 80; ---此主机的监听端口
server_name localhost; ---域名或主机名(能解析的主机名,如localhost,因为他在hosts文件中配置了,一个server中可配置多个server_name,或者直接使用泛解析 :*.qqqq.com(dns服务器中))
若实验环境为配置dns服务器,只是修改了本地hosts文件,此处要写hosts文件中的全域名。
location / { ---URI 如:https://www.nginx.com/products/nginx/load-balancing/ 绿色部分(资源定位)
root html; ---匹配到 location后,从哪个目录去查找相应的网页,此例为html下(注:此root不是指root用户)
一个主机下可以有n个location,定位到不同的目录,且互不影响,此处html为local/nginx/的相对路径。
index index.html index.htm; ---此location的默认展示页面(没有默认页面的话),只显示这两个后缀的
}
error_page 500 502 503 504 /50x.html; ---发生服务器端错误的时候,发生错误的相应号,跳转到哪个页面
location = /50x.html { ---当用户访问此页面的时候
root html; 指向50x所在的目录(也是相对路径,从哪个目录下查找50x.html)
}
}
}
一个server就是一个虚拟主机(vhost),个主机间相互不干扰,有独立的站点,有独立的根目录
在阿里云在配置了泛解析后,如果站点没有匹配上,默认显示第一个站点如:
两个站点a.qwep.com
b.qwep.com
访问b2.qwep.com 会显示第一个站点a.qwep.com
核心配置: 同上
虚拟主机配置:
nginx的多虚拟主机可以基于多IP的形式、基于多域名的形式、多端口的形式
基于ip:可防止单个网卡损坏而无法提供服务
基于端口:可根据端口访问不同站点
基于域名的虚拟主机配置:
根据不同的域名,访问不同的网站
由于是实验,用的本地域名解析,没有申请域名
更改本机域名解析文件hosts
加入实验的域名与ip
如: 设置一个一级域名。
192.168.1.132 qqqq.com (而基于ip的则ip不同,但真实网络环境中不需要用户自己配置,dns服务器会解决)
而虚拟主机做两个子域名,如:
jns.qqqq.com
mails.qqqqq.com
域名的泛解析 *.qqqq.com 不管是a.qqqq.com 还是b.qqqq.com …..都解析到qqqq.com域名
配置服务器站点:(实验)
mkdir mail
mkdir mp3
配置nginx配置文件:
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name mail.qwep.com;
location / {
root /mail;
index mail.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name mp3.qwep.com;
location / {
root /mp3;
index mp3.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
注:内网做基于域名的虚拟主机的话要自己搭建dns服务器做配置:
A mail.qwep.com
A mp3.qwep.com
如果是:A mail.qwep.com
A .qwep.com
会将不带mail的也指向mail.qwep.com而awdaw.qwep.com就不会(阿里云中,内网环境未经实验)
server_name的匹配规则
一个server_name中匹配多个域名
server_name mail.momoh.top namil123.momoh.top
完整匹配
不用多解释
通配符匹配
server_name *.momoh.top
或 server_name *.www.momoh.top 等
通配符结束匹配
server_name www.momoh.*
正则匹配
方便建立二级域名系统
server_name ~[0-9]+\.momoh\.top$等
反向代理
配置反向代理:
proxy_pass 不支持https。
注:proxy_pass http:// 是固定格式。必须写http://(域名全写)
proxy_pass 与 root 二选一。
listen与server_name加起来要是唯一的。
192.168.1.132(mail站点)
server {
listen 80;
server_name mail.momoh.top;
location / {
root /mail;
index mail.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
192.168.1.133 (mp3站点)
server {
listen 80;
server_name mp3.momoh.top;
location / {
root /mp3;
index mp3.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
192.168.1.134 (nginx代理服务器) 此处反向代理了两个站点。
server {
listen 80;
server_name mail.momoh.top;
location / {
proxy_pass http://192.168.1.132; #域名如果不是全拼如:http:momoh.top 会变为跳转页面
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name mp3.momoh.top;
location / {
proxy_pass http://192.168.1.133;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
其他两台服务器在内网 132与133,可设置防火墙策略 除134都不可访问(根据情况)
也可直接让两台服务器无法联通外网。
可两者皆做。
负载均衡
策略一
权重、down、backup down不常用。
需求:访问134代理:
一此显示服务器1,一此显示服务器2.
达到负载效果。当然,服务器1与服务器2网站一模一样。
就是轮询
服务器1:192.168.1.132
服务器2:192.168.1.133
nginx代理:192.168.1.134
正常环境下nginx代理应有两个,保证代理服务器一台损坏另一台继续工作,或是也做负载均衡。设置权重等。
在nginx代理配置文件中配置:
upstream suiyi {
server 192.168.1.132:80;
server 192.168.1.133:80;
} 此模块与server模块同级。且要写在server模块上面。
将原server{
} 中的: proxy_pass http://suiyi; 注意此处sunyi要与上面对应,只要与上方一样,什么都无所谓,建议起名有意义。
权重:
权重则是 有两台nginx做代理如:mail:132 mail:133 (两台站点一样,做负载均衡)
nginx_proxy1:134
想要访问mail走132的多 ,走133的少
这时候就要配置权重了。(权重0为不走此线路,配置文件中不可,
weight=2 down; 就代表此服务器不参与了。
阿里云可以)按权重高低分配路线接收请求比例。(服务器带宽不同的情况下)
(两台代理情况下,不严谨)删除权重时 dns解析可能会有几分钟的时间无法访问。分析可能是dns缓存的问题,关掉135且删除权重与135的解析后,短时间内访问mp3仍请求135。
如此:
8此请求服务器一 后 2次请求 服务器二。
两台nginx代理的情况下,可在阿里云解析配置。
此图释意:
用户访问mail站点
1000m带宽的nginx_proxy1:根据权重比例,分配给代理1线路,100此请求后,再分给代理2。
100m带宽的nginx_proxy2: 根据权重比例,分配给代理2线路,50此请求后,再分给代理1。
下方mail1,2,3服务器同理。
注意:此处请求次数只是猜测,真实根据访问量根据权重比例分配。
weight=0 为不给此线路分配。配置文件中不可为0,配置文件中为 down
weight=2 down; 就代表此服务器不参与了。
,而阿里云可以。
阿里云的权重配置:
本地nginx配置文件权重配置:
upstream suiyi {
server 192.168.1.132:80 weight=10;
server 192.168.1.133:80 weight=5;
}
即可。
配置文件内权重为零:
upstream suiyi {
server 192.168.1.132:80 weight=10 down; 就等同于为0,阿里云可直接配置为0。
server 192.168.1.133:80 weight=5;
}
备用服务器(正常情况下服务器不参与负载,当其他服务器都不可用后,再参与):
upstream suiyi {
server 192.168.1.132:80 weight=10;
server 192.168.1.133:80 weight=5 backup; 132不可用后133才会启用。比如132服务器停止。
}
策略二
轮询的缺点:无法保持会话:
如登录:cooike存储在服务器1,登录到服务器2,cookie对不上,会重复登录。
流程化的操作等,所以不能单用。
其他策略: 都不常用:无法动态上下线,在固定配置文件中转发固定地址,不灵活。
ip_hash :
根据客户端的ip地址转发给同一台服务器,可以保持会话。
缺点:移动端,在行动过程中切换了基站,那又会重新登录等。无法保持会话了。
least_conn:
最少连接访问
将新的请求分配给接受连接少的服务器。 (并不合理,接受连接少可能是因为配置了权重的问题,本意就是让他少接受连接)
fair: 需要下载第三方插件。
根据后端服务器的响应时间转发请求。(不合理,资源倾斜)
url_hash:需要下载第三方插件。
定向流量转发(不是用户转发)
将用户访问页面生成哈希值,相同哈希值的转发给相同服务器,所以同样无法保持会话。
适用于访问固定资源,且不在同一服务器。
所以,在实际应用场景中:
要么只使用轮询:缺点,无法保持会话
要么使用lua脚本自定义转发规则
主流保持会话方式:无状态会话保持
动静分离
适合中小型网站。
使用动静分离的场景:
并发量不高,需要分离出来的静态资源不多。
配置动静分离后 可大大提高并发量。
动静分离原理:
将静态资源放到 前置 的nginx服务器里
nginx配置动静分离(基础应用):
server {
listen 80;
server_name mail.momoh.top;
location / {
proxy_pass http://192.168.1.132;
}
location /img {
root /html/mail;
index mail.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name mp3.momoh.top;
location / {
proxy_pass http://192.168.1.133;
}
location /img {
root /html/mp3;
index mp3.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
配置原理:
由于上方location已经配置代理,无法指定roothtml目录,所以要新设置一个location
html网页调用的路径是 img/静态资源
静态资源在nginx代理下的路径: /html/mail
/html/mp3
会自动将 location /img 与内部的 root /html/mail
拼接成 /html/mail/img,而下面的index 部分写不写无所谓(此场景中)
location 匹配规则:
location / 比 location /css 匹配规则低。
location 匹配多个目录:
正则匹配: location ~*/(js|img|css) {
}
~: 表示正则开始
*:表示不区分大小写。
|:表示或者
注意修改完配置文件后记得 reload重载nginx或restart重启
URLRewrite伪静态配置
UrlRewrite就是我们通常说的地址重写,用户得到的全部都是经过处理后的URL地址
来自 <百度安全验证>
原访问页面为
:http://mail.momoh.top/index.jsp?pageNum=2
由于暴露了入参等信息
改为伪静态:
变为:http://mail.momoh.top/2.html
在nginx代理的配置文件内:
location / {
rewrite ^/([0-9]+\.html)$ index.jsp?pageNum=$1 break; ^$格式 , 括号及内部 正则匹配的组1. 要替换的内容 $1=调用组1 flag标记
\ 取消.的特殊含义。
flag标记:
last 本条规则匹配完成后,继续向下匹配新的location url规则 可隐藏入参
break 本条规则匹配完成即终止 可隐藏入参
redirect 返回302临时重定向,浏览器地址会显示跳转后的url地址
permanent 返回301永久重定向,浏览器地址会显示跳转后的url地址
proxy不动,依旧需要代理
}
负载均衡加URLrewrite:
网关服务器配置文件:
有upstream(轮询负载均衡)、proxy_pass(反向代理) 、location /img{}(动静分离)、rewrite(重写、伪静态)
用户切换页面上方默认显示静态
防盗链与http的referer
盗链:
其他网站非法引用本站资源
referer:
网站第二次请求开始带有请求头中会带有referer信息,用于判断是否为本站引用,防止盗链
不常用,一般网站都希望别的网站给自己引用户。
除非你的服务器资源消耗过大,或资源稀缺,只希望从本站的请求引用才可。
基本配置
哪个location下的资源不想被盗链
就在哪个location下配置:
如:我192.168.1.133下的静态资源img不想被盗链。(此img已做动静分离。img资源在代理nginx134下)
server {
listen 80;
server_name localhost;
location /img {
valid_referers mp3.momoh.top; 匹配来源的网址:从哪个网址过来的(没有http://)
if ($invalid_referer){ 检测referer是否有效
return 403; 无效返回403
}
root /mp3;
index index.html;
}
}
盗链的网页:
<html>
<head>
</head>
<body>
<img src="http://mp3.momoh.top/img/b.png">
</body>
</html>
实验中第一次检测referer没有打开:可以盗链
第二次检测页面打开了:就不可以盗链了(记得删除第一次的缓存,或F12禁用缓存(在打开F12才有用))
要写在想设置防盗链的上方,referer无效后就不匹配了,写在上方就会先匹配资源,防盗链的设置就没用了
其他几种情况: 让没有referer的访问,但不让错误的访问
valid_referers none mp3.momoh.top;
nonoe:检测referer头域不存在的情况
blocked:如果不带http或httpd也是可以访问 (不常用)
server_names:设置一个或多个URL,erferer是这些的其中一个就允许访问
referer不符合要求
返回图片
整合rewrite返回错误页面
将上方 return替换为(位置不变):rewrite ^/ 图片路径 break;
匹配所有 最好绝对路径 隐藏地址
返回页面
新建一个 location 模块:
error_page 403 /403.html; (可不写,因为我们的设置中只有一个403)
location = /403.html {
root /html;
}
也可以之际在上方return /错误页面
高可用的场景及解决方案
keepalived
!!!keepalived基于vrrp协议,工作在三层交换机和路由器上,stop network停掉的是上层网络服务,而keepalived检测的是物理层面的链接,所以,keepalived无法检测网卡状态。
与交换机路由器的vrrp原理相同,虚拟出一个ip(名为vip)
做ngnix代理服务器之间的热备份。
只有一台坏了,另一台才会工作
安装方式:
编译安装
下载地址:https://www.keepalived.org/download.html#
如果遇到报错提示:
!!!openssl is not……..!!!
!!!....!!!
报错:
configure: error: no acceptable C compiler found in $PATH
来自 <configure: error: no acceptable C compiler found in $PATH 问题解决_rich@cai的博客-优快云博客>
则安装gcc套件:
yum install gcc
来自 <configure: error: no acceptable C compiler found in $PATH 问题解决_rich@cai的博客-优快云博客>
则安装依赖:
yum install -y openssl-devel
yun安装:
yum install -y keepalived
配置文件位置(yum安装):
/etc/keepalived/keepalived.conf
实验环境:
后端nginx 192.168.1.132 mail.momoh.top
nginx代理1 192.168.1.134
nginx代理2 192.168.1.135
keepalived vip 192.168.1.200
配置keepalived配置文件: 注意:将不配置的选项删掉或注释
nginx代理1 192.168.1.134:
! Configuration File for keepalived
global_defs {
# notification_email {
# acassen@firewall.loc
#failover@firewall.loc
#sysadmin@firewall.loc 宕机发email,此处不配置
#}
#notification_email_from Alexandre.Cassen@firewall.loc
#smtp_server 192.168.200.1
#smtp_connect_timeout 30
router_id ad134 给此台机器起一个id
#vrrp_skip_check_adv_addr
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
}
vrrp_instance ceyan { ceyan为实例名称(两边要一致)
state BACKUP 当前这台机器是BACKUP,优先级比另一台低
interface ens33 你的网卡名
virtual_router_id 51 两边要一致
priority 100 竞选时谁的优先级高谁是master
advert_int 1 间隔检测的时间
authentication { 一组认证服务器,方便keepalived配对
auth_type PASS 同下 认证类型
auth_pass 1111 认证配置,同组保持一致 认证密码
}
virtual_ipaddress { 虚拟的ip地址,就是vip
192.168.1.200 可多个,但意义不大
# 192.168.200.17
# 192.168.200.18
}
}
nginx代理2 192.168.1.135:
! Configuration File for keepalived
global_defs {
router_id ad135
}
vrrp_instance ceyan {
state MASTER
interface ens33
virtual_router_id 51
priority 110
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200
}
}
然后两台机器会根据优先级进行选举,优先级高的设置的网卡会多一个虚拟ip(就是vip)(新版本两边都会多(如2.2.7),但只有master起作用)
ip addr 查看
此实验135为主。
dns解析设为vip地址
这样配置:
抢占模式
当nginx代理1宕机后,nginx代理2接替工作,但nginx代理1修复后,会抢回vip。(虚拟机挂起会出现vip两边都没有的状况)
非抢占模式:
防止vip来回切换:
两台nginx代理都改为 BACKUP (注意这只是标记,真正看优先级)
优先级下加:
nopreempt
注意keepalived只监视自己的进程,无法监视nginx或其他服务的进程
写个脚本,每秒访问一次nginx,返回响应值为200,不做操作
不为200 kill 掉keepalived的进程,注意 是 kill 掉
如:
vim 200.sh
#!/bin/bash
code=`curl -o /dev/null -s -w %{http_code} mail.momoh.top |xargs echo`
if [ $code -eq 200 ];then
else
ps -aux | grep keepalived | sed -nr '1,3p' | awk '{print $2}' | kill -9
fi
然后cron计划任务运行脚本
其他服务原理相同。
keepalived只可以检测物理连接,无法自主检测network服务
#!/bin/bash
curl -o /dev/null 127.0.0.1
number=`echo $?`
if [ $number -eq 0 ];then
else
ps -aux | grep keepalived | sed -nr '1,3p' | awk '{print $2}' |xargs kill -9
fi
既可以检测web服务,也可以检测网卡
正常应用环境:
不抢占:nopreempt
两个都是BACKUP
优先级一高一低
查看是否有keep-alived
检测
网络
浏览器默认是期望keep-alived
而有时是没有的:
什么时候应该开启/关闭:
当只请求一次页面引用的静态资源,就无需开启keepalived了,只需要把静态资源放在前端服务器上(动静分离)
怎样关闭keepalived长连接:(在http1.0与http1.1中配置有些许不同)
对客户端的配置:
实在http模块中:
http {
include mime.types;
default_type application/octet-stream;
#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;
#keepalive_timeout 0;
keepalive_timeout 65; 单位秒(65秒内不操作关闭此连接),0则是关闭长连接。(并不是关闭keepalived高可用)
#gzip on;
upstream suiyi{
sticky;
server 192.168.1.132;
server 192.168.1.133;
}
disable 有的浏览器keepalived是有问题的,就直接使用它给禁用掉了。
对上游服务器的配置:
是在上游服务器所在组的upstream中:
AB压力测试keepalived效果
apache benckmark安装与基本使用
安装:
yum install -y httpd-tools
参数:
ab -n 100000 -c 300 https://mail.momoh.top/ 注:后面的/不能少
在线压测网站:
GTmetrix | Website Performance Testing and Monitoring
环境:
上游nginx:
192.168.1.132
192.168.1.133
nginx代理:(keepalived vip :192.168.1.200 轮询状态,无保持会话)
192.168.1.134
192.168.1.135
压力测试机:
192.168.1.9
除域名不同,统一上方 ab -n 100000 -c 300
测试nginx直连:
Server Software: nginx/1.21.6
Server Hostname: 192.168.1.133
Server Port: 80
Document Path: /
Document Length: 160 bytes
Concurrency Level: 300
Time taken for tests: 19.014 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 39200000 bytes
HTML transferred: 16000000 bytes
Requests per second: 5259.40 [#/sec] (mean)
Time per request: 57.041 [ms] (mean)
Time per request: 0.190 [ms] (mean, across all concurrent requests)
Transfer rate: 2013.36 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 27 105.5 16 3042
Processing: 2 28 32.3 19 572
Waiting: 2 24 30.0 18 549
Total: 6 55 111.7 35 3249
Percentage of the requests served within a certain time (ms)
50% 35
66% 42
75% 51
80% 59
90% 84
95% 106
98% 165
99% 470
100% 3249 (longest request)
测试nginx代理:
此处将133,变为了132的代理:
只加了个:
location / {
proxy_pass http://192.168.1.132;
#root /mp3;
#index mp3.html;
}
}
完成测试后记得改回去。
Server Software: nginx/1.21.6
Server Hostname: 192.168.1.133
Server Port: 80
Document Path: /
Document Length: 9305 bytes
Concurrency Level: 300
Time taken for tests: 54.670 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 954000000 bytes
HTML transferred: 930500000 bytes
Requests per second: 1829.17 [#/sec] (mean)
Time per request: 164.009 [ms] (mean)
Time per request: 0.547 [ms] (mean, across all concurrent requests)
Transfer rate: 17041.27 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 11 40.0 6 1031
Processing: 40 152 58.4 143 1399
Waiting: 6 103 53.8 93 1347
Total: 53 164 73.7 152 1404
Percentage of the requests served within a certain time (ms)
50% 152
66% 171
75% 186
80% 194
90% 225
95% 251
98% 285
99% 319
100% 1404 (longest request)
测试nginx Upstream 加轮询式负载 keepalived SSL(HTTPS)代理
Server Software: nginx/1.21.6
Server Hostname: mail.momoh.top
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256
Document Path: /
Document Length: 9305 bytes
Concurrency Level: 300
Time taken for tests: 356.547 seconds
Complete requests: 100000
Failed requests: 50041
(Connect: 0, Receive: 0, Length: 50014, Exceptions: 27)
Write errors: 0
Total transferred: 496460952 bytes
HTML transferred: 473117490 bytes
Requests per second: 280.47 [#/sec] (mean)
Time per request: 1069.641 [ms] (mean)
Time per request: 3.565 [ms] (mean, across all concurrent requests)
Transfer rate: 1359.78 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 999 2493.4 492 70026
Processing: 11 56 2116.0 19 127337
Waiting: 8 20 14.2 19 504
Total: 231 1055 3259.4 512 127337
Percentage of the requests served within a certain time (ms)
50% 512
66% 564
75% 706
80% 870
90% 1528
95% 3463
98% 6644
99% 7725
100% 127337 (longest request)
分析部分失败原因可能为,响应时间过长关闭连接,或请求数过大等
测试tomcat直连与代理
直连:
将132换为tomcat
安装tomcat:
yum install -y tomcat tomcat-webapps tomcat-admin-webapps
默认端口8080
测试:
Server Software: Apache-Coyote/1.1
Server Hostname: 192.168.1.132
Server Port: 8080
Document Path: /
Document Length: 11197 bytes
Concurrency Level: 300
Time taken for tests: 55.089 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 1134300000 bytes
HTML transferred: 1119700000 bytes
Requests per second: 1815.26 [#/sec] (mean)
Time per request: 165.266 [ms] (mean)
Time per request: 0.551 [ms] (mean, across all concurrent requests)
Transfer rate: 20107.91 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 67 84.6 61 1087
Processing: 17 97 39.1 94 673
Waiting: 7 66 35.5 63 641
Total: 28 165 99.0 161 1314
Percentage of the requests served within a certain time (ms)
50% 161
66% 188
75% 201
80% 208
90% 225
95% 239
98% 266
99% 415
100% 1314 (longest request)
代理:
133做tomcat的代理:
proxy_pass http://192.168.1.132:8080
测试:
Server Software: nginx/1.21.6
Server Hostname: 192.168.1.133
Server Port: 80
Document Path: /
Document Length: 11197 bytes
Concurrency Level: 300
Time taken for tests: 49.358 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 1133800000 bytes
HTML transferred: 1119700000 bytes
Requests per second: 2026.01 [#/sec] (mean)
Time per request: 148.074 [ms] (mean)
Time per request: 0.494 [ms] (mean, across all concurrent requests)
Transfer rate: 22432.56 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 15 47.4 8 1038
Processing: 16 133 139.0 111 3129
Waiting: 8 107 136.9 86 3104
Total: 22 148 149.0 122 3140
Percentage of the requests served within a certain time (ms)
50% 122
66% 139
75% 154
80% 164
90% 197
95% 230
98% 395
99% 1136
100% 3140 (longest request)