
(1)主从方式 (非对称方式)
(2)双机双工方式(互备互援)
(3)集群工作方式(多服务器互备方式)
主要品牌有:F5、Array、NetScaler等
优点
缺点
主要有LVS、HAProxy、Nginx等
优点
缺点
RR 轮询
WRR 加权轮
LBLC 基于局部性的最少连接调度算法
LBLCR 复杂的基于局部性最少的连接算法
DH 目标地址散列调度算法
SH 源地址散列调度算法
R 随机法
WR 加权随机法
https://baike.baidu.com/item/nginxnginx简介
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EMNRloXP-1632891678744)(G:笔记Linux运维第三阶段高可用负载均衡&反向代理服务.assetsimage-20210919164035870.png)]
一个master进程生成多个worker子进程(每个进程只有一个线程),一个worker响应多个用户请求 非阻塞、IO复用、事件驱动:select,poll, epoll, kqueue,/dev/poll 支持sendfile,sendfile64 支持文件AIO(异步I/O) 支持mmap 灵活的文件配置 占用内存小:10,000个非活动HTTP保持连接占用大约2.5M内存nginx基本功能
实现与服务静态文件(静态资源的web服务器),能缓存打开的文件描述符 反向代理服务器,缓存、负载均衡、健康状态检测 支持FastCGI 模块化机制,非DSO机制,支持多种过滤器gzip,SSI和图像的模块完成图形大小调整等支持SSL 基于名称和IP做虚拟主机 支持keeplive 支持平滑配置更新或程序版本升级 定制访问日志,支持使用日志缓存以提高性能 支持URL rewrite 支持路径别名 支持基于IP及用户的认证 支持速率限制,并发数限制等基本概念 什么是代理
代理其实就是一个中介,A和B本来可以直连,中间插入一个C,C就是中介。刚开始的时候,代理多数是帮助内网client访问外网server用的后来出现了反向代理,"反向"这个词在这儿的意思其实是指方向相反,即代理将来自外网客户端的请求转发到内网服务器,从外到内。
总结:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ASrReYFc-1632891678746)(G:笔记Linux运维第三阶段高可用负载均衡&反向代理服务.assetsimage-20210919164405774.png)]
正向代理的用途[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d4yvTPpl-1632891678747)(G:笔记Linux运维第三阶段高可用负载均衡&反向代理服务.assetsimage-20210919164455937.png)]
反向代理的作用为了提高网站的响应速度,减轻程序服务器(tomcat,jboss等)的负载,对于静态资源比如图片,js,css等文件,我们可以在反向代理服务器中进行缓存,这样浏览器在请求一个静态资源时,代理服务器就可以直接处理,而不用将请求转发给后端服务器。用户请求的动态文件比如servlet,jsp则转发给tomcat,jboss服务器处理,这就是动静分离。这也是反向代理服务器的一个重要的作用。
java动静分离。主要是通过nginx+tomcat来实现,其中nginx处理图片、html、js、css等静态文件,tomcat处理jsp、servlet等动态请求。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ShO7MWQc-1632891678748)(G:笔记Linux运维第三阶段高可用负载均衡&反向代理服务.assetsimage-20210919164632142.png)]
nginx安装 官网地址http://nginx.org/Nginx官方文档 英文版
http://tengine.taobao.org/nginx_docs/en/index.html中文版
http://tengine.taobao.org/nginx_docs/cn/版本介绍
由于nginx默认端口也是80端口,如果此时你的机器上开启了Apache或者IIS服务,切忌在启动nginx之前务必关闭IIS或Apache服务,否则nginx启动命令不会成功。
安装目录中不要包含中文或者特殊字符
控制台(CMD)切换到Nginx目录下,输入start nginx ,然后在浏览器页面输入IP地址,出现如下界面则表示安装成功。默认监听80端口号。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YSJR4hVn-1632891678749)(G:笔记Linux运维第三阶段高可用负载均衡&反向代理服务.assetsimage-20210919165627033.png)]
http://192.168.198.198/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xfE8SaKN-1632891678749)(G:笔记Linux运维第三阶段高可用负载均衡&反向代理服务.assetsimage-20210919165705614.png)]
nginx 命令开启nginx服务 start nginx nginx -v 显示nginx的版本信息(nginx version) nginx -t 查看nginx的配置文件的目录 nginx -h 查看帮助信息 关闭nginx服务,快速停止nginx,可能并不保存相关信息 nginx.exe -s stop 关闭nginx服务,完整有序的停止nginx,并保存相关信息 nginx.exe -s quit 重载nginx服务,当你改变了nginx配置信息并需要重新载入这些配置时可以使用此命令重载nginx nginx.exe -s reload 重新打开日志文件命令 nginx -s reopennginx的目录结构
conf 目录:存放 Nginx 的主要配置文件,很多功能实现都是通过配置该目录下的 nginx.conf 文件,后面我们会详细介绍。 docs 目录:存放 Nginx 服务器的主要文档资料,包括 Nginx 服务器的 LICENSE、 OpenSSL 的 LICENSE 、PCRE 的 LICENSE 以及 zlib 的 LICENSE ,还包括本版 本的 Nginx服务器升级的版本变更说明,以及 README 文档。 html 目录:存放了两个后缀名为 .html 的静态网页文件,这两个文件与 Nginx 服务器 的运行相关。 logs 目录:存放 Nginx 服务器运行的日志文件。 temp:用来存放零时文件 nginx.exe:启动 Nginx 服务器的exe文件,如果 conf 目录下的 nginx.conf 文 件配置正确的话,通过该文件即可启动 Nginx 服务器。配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
基本配置
########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组,默认为nobodynobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:
debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,
select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local]
$request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
}
}
入门案例
案例一:修改欢迎页面
中文乱码问题Welcome to nginx! 欢迎来到nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
案例二:修改端口号 修改配置文件Welcome to nginx! 欢迎来到nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
.....省略配置.....
server {
listen 8080;
server_name localhost;
.....省略配置.....
重启nginx
nginx.exe -s reload测试
http://192.168.198.198:8080/实战案例 案例一:配置本地虚拟主机 修改nginx.conf文件
include lagouedu/*.conf;lagouedu.conf文件
server {
listen 9090;
server_name localhost;
location / {
root html;
index lagouedu.html lagouedu.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
lagouedu.html
重启nginxWelcome to nginx! 欢迎来到拉勾教育!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
nginx.exe -s reload测试
http://192.168.198.198:9090/案例二:修改首页位置
server {
listen 9090;
server_name localhost;
location / {
root E:lagoueduhtml;
index lagouedu.html lagouedu.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
lagouedu.html
重启nginxWelcome to nginx! 欢迎来到拉勾教育!修改了首页位置
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
nginx.exe -s reload测试
http://192.168.198.198:9090/案例三:虚拟域名
127.0.0.1 www.devopstest.com重启nginx
nginx.exe -s reload测试
http://www.devopstest.com:9090/案例四:反向代理
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eHwfUojS-1632891678750)(G:笔记Linux运维第三阶段高可用负载均衡&反向代理服务.assetsimage-20210919172004115.png)]
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:9090;
index index.html index.htm;
}
}
重启nginx
nginx -s reload测试
http://www.devopstest.com/反向代理相关指令介绍 listen
listen address[:port] [default_server] [setfib=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [deferred] [accept_filter=filter] [bind] [ssl];
listen port[default_server] [setfib=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [ssl];
listen unix:path [default_server] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ssl];
listen *:80 | *:8080 #监听所有80端口和8080端口 listen IP_address:port #监听指定的地址和端口号 listen IP_address #监听指定ip地址所有端口 listen port #监听该端口的所有IP连接
server_name name ...;
server_name 123.com www.123.com
server_name *.123.com www.123.*
server_name ~^wwwd+.123.com$;
1 准确匹配 server_name 2 通配符在开始时匹配 server_name 成功 3 通配符在结尾时匹配 server_name 成功 4 正则表达式匹配 server_name 成功基于 IP 地址的虚拟主机配置
server_name 192.168.1.1location
location [ = | ~ | ~* | ^~] uri {
}
该指令用于设置被代理服务器的地址。可以是主机名称、IP地址加端口号的形式。
语法结构如下:
proxy_pass URL;
proxy_pass http://www.123.com/uri;index
index filename ...;
index index.html index.jsp;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9XrHgKf3-1632891678751)(G:笔记Linux运维第三阶段高可用负载均衡&反向代理服务.assetsimage-20210919173105028.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ytsaXNoX-1632891678751)(G:笔记Linux运维第三阶段高可用负载均衡&反向代理服务.assetsimage-20210919173156566.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RzbrLJEW-1632891678752)(G:笔记Linux运维第三阶段高可用负载均衡&反向代理服务.assetsimage-20210919173229243.png)]
负载均衡算法两台相同的Tomcat服务器,通过 localhost:8080 访问Tomcat1,通过localhost:8081访问Tomcat2,现在我们要输入 localhost 这个地址,可以在这两个Tomcat服务器之间进行交替访问基于比例加权轮询
我们知道一个请求在经过一个服务器处理时,服务器会保存相关的会话信息,比如session,但是该请求如果第一个服务器没处理完,通过nginx轮询到第二个服务器上,那么这个服务器是没有会话信息的。
最典型的一个例子:用户第一次进入一个系统是需要进行登录身份验证的,首先将请求跳转到Tomcat1服务器进行处理,登录信息是保存在Tomcat1 上的,这时候需要进行别的操作,那么可能会将请求轮询到第二个Tomcat2上,那么由于Tomcat2 没有保存会话信息,会以为该用户没有登录,然后继续登录一次,如果有多个服务器,每次第一次访问都要进行登录,这显然是很影响用户体验的。
这里产生的一个问题也就是集群环境下的 session 共享,如何解决这个问题?
通常由两种方法:
而 nginx 的基于 IP 路由负载的机制就是第二种形式。
nginx-master nginx-agent01 nginx-agent02nginx-agent01配置
include lagouedu/*.conf;
server {
listen 9090;
server_name localhost;
location / {
root html;
index lagouedujava.html lagouedujava.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
拉勾教育java课程首页!
start nginx
localhost:9090nginx-agent02配置
include lagouedu/*.conf;
server {
listen 9091;
server_name localhost;
location / {
root html;
index lagouedudevops.html lagouedudevops.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
拉勾教育运维课程首页!
start nginx
localhost:9091nginx-master配置
server {
listen 8090;
}
include lagouedu/*.conf;
upstream lagouedu {
server 127.0.0.1:9090;
server 127.0.0.1:9091;
}
server {
listen 8088;
server_name localhost;
location / {
proxy_pass http://lagouedu;
}
}
start nginx
localhost:8088centos7系统
yum install -y gcc yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel
tar -zxvf nginx-1.18.0.tar.gz目录结构
. ├── auto 自动检测系统环境以及编译相关的脚本 │ ├── cc 关于编译器相关的编译选项的检测脚本 │ ├── lib nginx编译所需要的一些库的检测脚本 │ ├── os 与平台相关的一些系统参数与系统调用相关的检测 │ └── types 与数据类型相关的一些辅助脚本 ├── conf 存放默认配置文件,在make install后,会拷贝到安装目录中去 ├── contrib 存放一些实用工具,如geo配置生成工具(geo2nginx.pl) ├── html 存放默认的网页文件,在make install后,会拷贝到安装目录中去 ├── man nginx的man手册 └── src 存放nginx的源代码 ├── core nginx的核心源代码,包括常用数据结构的定义,以及nginx初始化运行的核心代码如main函数 ├── event 对系统事件处理机制的封装,以及定时器的实现相关代码 │ └── modules 不同事件处理方式的模块化,如select、poll、epoll、kqueue等 ├── http nginx作为http服务器相关的代码 │ └── modules 包含http的各种功能模块 ├── mail nginx作为邮件代理服务器相关的代码 ├── misc 一些辅助代码,测试c++头的兼容性,以及对google_perftools的支持 └── os 主要是对各种不同体系统结构所提供的系统函数的封装,对外提 供统一的系统调用接口 接着进入到解压之后的目录,进行编译安装。 ./configure make make install 接着我们进入到 /usr/local/nginx/sbin 目录,通过如下命令启动 nginx: ./nginx 查看 nginx 是否有服务正在运行: ps -ef | grep nginx 测试: http://192.168.198.90 nginx安装目录 /usr/local/nginxnginx命令
进入到 /usr/local/nginx/sbin 目录: 查看nginx版本号 ./nginx -v 启动nginx ./nginx 关闭nginx ./nginx -s stop 重启nginx ./nginx -s reloadyum方式安装
如果采用yum安装。Gcc等程序不用安装。默认情况Centos7中无nginx的源,因此可以如下执行命令添加源。
添加源
rpm方式安装: rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginxrelease-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx
systemctl start nginx.service systemctl enable nginx.service
以下是Nginx的默认路径: 1.Nginx配置文件路径:/etc/nginx/ 2.PID目录:/var/run/nginx.pid 3.错误日志:/var/log/nginx/error.log 4.访问日志:/var/log/nginx/access.log 默认站点目录:/usr/share/nginx/html官方yum安装方式
http://nginx.org/en/linux_packages.html#stable安装依赖
yum install -y yum-utils创建nginx.repo文件
vi /etc/yum.repos.d/nginx.repo
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$ba search/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key安装nginx
yum install -y nginx启动Nginx并设置开机自动运行
systemctl start nginx.service systemctl enable nginx.servicenginx默认安装路径
以下是Nginx的默认路径: 1.Nginx配置文件路径:/etc/nginx/ 2.PID目录:/var/run/nginx.pid 3.错误日志:/var/log/nginx/error.log 4.访问日志:/var/log/nginx/access.log 默认站点目录:/usr/share/nginx/html