文章目录

前言

一、准备工作

1.准备多台服务器

2.修改服务器的名字,明确配置

3.安装nginx

二、配置负载均衡

1.调度算法

2.修改配置

3.更新配置

4.https的负载均衡配置

三、实现realip

前言

负载均衡,英文名称为Load Balance,其含义就是指将负载(工作任务)进行平衡、分摊到多个操作单元上进行运行,例如FTP服务器、Web服务器、企业核心应用服务器和其它主要任务服务器等,从而协同完成工作任务。

负载均衡构建在原有网络结构之上,它提供了一种透明且廉价有效的方法扩展服务器和网络设备的带宽、加强网络数据处理能力、增加吞吐量、提高网络的可用性和灵活性。

一、准备工作

1.准备多台服务器

将其中一台作为负载均衡器,剩下的服务器作为后端的web服务器,所有的服务器建议统一编译安装nginx,实现统一配置。本文使用3台服务器作为实现负载均衡的流程演示。

tips:为节省时间,可用已经安装的nginx服务器直接进行克隆

克隆步骤:

        (1)init 0 将虚拟机关机

        (2)在上方的选项栏中点 虚拟机 ---> 管理 ---> 克隆

        (3)选择创建链接克隆,其他都为默认选项即可。创建链接克隆可以节省一定的空间。

2.修改服务器的名字,明确配置

LB : 192.168.81.128

WEB1 : 192.168.81.127

WEB2 :192.168.81.100

按照规划修改ip地址:

[root@lianyu ~] hostnamectl set-hostname LB

[root@lianyu ~] su

[root@lb ~]#

[root@lianyu ~] hostnamectl set-hostname web1

[root@lianyu ~] su

su

[root@web1 ~]#

[root@localhost ~] hostnamectl set-hostname web2

[root@localhost ~] su

[root@web2 ~]#

3.安装nginx

可将一键安装脚本复制到未安装的nginx的服务器上(远程复制),一键安装脚本在其他文章中已呈现,可进行直接复制。

[root@lb nginx] scp onekey_install_nginx.sh 192.168.81.100:/root

二、配置负载均衡

1.调度算法

nginx中负载均衡的一些调度算法:

round-robin 轮询调度(默认) least-connected 最小连接数 ip-hash :同一个ip地址的所有请求都集中在一台服务器上,用于需要保存用户会话信息的场景

2.修改配置

[root@lb nginx]# cd /usr/local/scnginx99/conf

[root@lb conf]# vim nginx.conf

#在http模块中定义一个负载均衡器,名字为layapp(可自行更换)

http{

upstream layapp{

server 192.168.81.127;

server 192.168.81.100 weight=3;

}

# weight=3,表示权重值,默认情况下为1,可以使负载均衡向加了权重值的浏览器倾斜

server {

listen 80;

server_name www.li.com;

location / {

proxy_pass http://layapp;

#引用添加进来的内容

}

}

}

3.更新配置

[root@lb conf] nginx -t

nginx: the configuration file /usr/local/scnginx99/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/scnginx99/conf/nginx.conf test is successful

[root@lb conf] nginx -s reload

4.https的负载均衡配置

实现https的负载均衡,在负载均衡器LB上配置https,后端的服务器WB1,WB2可以不用配置https

server {

listen 443 ssl;

server_name cdn.xxx.xyz;

ssl_certificate cdn.xxx.xyz_nginx/cdn.xxx.xyz_bundle.pem ;

ssl_certificate_key cdn.xxx.xyz_nginx/cdn.xxx.xyz.key ;

ssl_session_cache shared:SSL:1m;

ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers on;

location / {

proxy_pass http://layapp; #添加

}

}

}

三、实现realip

在完成上述操作后,已经实现了负载均衡,但是后端的客户机并不知道前端用户访问时的真正ip,只知道负载均衡器的ip地址,下面操作目的是让后端的real server知道前端用户真实的ip地址 。本文采用不使用realip模块的方法,在负载均衡器的http协议中添加新的ip存储字段 x-real-ip.

1.在负载均衡器上修改http请求报文头部字段,添加一个x_real_ip字段

http{

server {

listen 80;

server_name www.li.com;

access_log logs/li.com.access.log main;

location / {

proxy_pass http://layapp;

proxy_set_header X-Real-IP $remote_addr; #添加部分

}

}

2.在后端real server上使用这个x_real_ip字段,在记录日志部分加入$http_x_real_ip

http {

include mime.types;

default_type application/octet-stream;

log_format main '$remote_addr - $http_x_real_ip - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

}

3.更新配置

[root@lb conf] nginx -t

nginx: the configuration file /usr/local/scnginx99/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/scnginx99/conf/nginx.conf test is successful

[root@lb conf] nginx -s reload

相关阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: