1、多台 web服务器 负载均衡

Nginx负载均衡_nginx

1、环境准备

服务器

IP地址

作用

系统版本

Proxy代理服务器

10.0.0.8

负载均衡Nginx Web服务器

Rocky8.6

Web1-Nginx服务器

10.0.0.18

网站服务器1

Rocky8.6

Web2-Nginx服务器

10.0.0.28

网站服务器2

Rocky8.6

client

10.0.0.101

测试访问网站

Ubuntu2004

2、web环境准备
# web1(10.0.0.18):
[root@rocky8 ~]# vim /usr/share/nginx/html/index.html
10.0.0.18
========================
# web2(10.0.0.28):
[root@rocky8 ~]# vim /apps/nginx/html/index.html
10.0.0.28
3、负载均衡配置
# Proxy(10.0.0.8):
[root@rocky8 conf.d]# vim www.wang.org.conf
upstream webserver{ #定义负载均衡主机群
server 10.0.0.18:80;
server 10.0.0.28:80;
keepalive 100; #该connections参数设置保留在每个工作进程缓存中的上游服务器的最大空闲保活连接数。当超过这个数字时,最近最少使用的连接将被关闭。
keepalive_timeout 60s; #空闲连接超时时长
}
server {
listen 80;
listen 443 ssl http2;
charset utf8;
server_name www.wang.org;
ssl_certificate /apps/nginx/cert/www.wang.org.pem;
ssl_certificate_key /apps/nginx/cert/www.wang.org.key;
ssl_session_timeout 10m;
root /data/nginx/html/pc;
access_log /apps/nginx/logs/www.wang.org_access.log main;
location /web {
proxy_pass http://webserver/; #访问/web页面转至负载均衡主机群
proxy_set_header Host $http_host; #转发主机头至后端服务器
proxy_next_upstream error;
proxy_http_version 1.1; #指定使用http1.1版本才能支持长连接
proxy_set_header Connection ""; #和上面的keepalived指令配合实现代理和后端服务器的长连接功能
}
}
[root@rocky8 conf.d]# nginx -s reload
4、客户端测试
[root@ubuntu2004 ~]#curl www.wang.org/web
10.0.0.18
[root@ubuntu2004 ~]#curl www.wang.org/web
10.0.0.28
[root@ubuntu2004 ~]#curl www.wang.org/web
10.0.0.18
[root@ubuntu2004 ~]#curl www.wang.org/web
10.0.0.28
[root@ubuntu2004 ~]#curl www.wang.org/web
10.0.0.18
[root@ubuntu2004 ~]#curl www.wang.org/web
10.0.0.28
[root@ubuntu2004 ~]#curl www.wang.org/web
10.0.0.18
[root@ubuntu2004 ~]#curl www.wang.org/web
10.0.0.28
2、基于Cookie 实现会话绑定
[root@rocky8 conf.d]# vim www.wang.org.conf 
upstream webcluster{
hash $cookie_wdy;
server 10.0.0.18:80 weight=2;
server 10.0.0.28:80 weight=1;
}
server {
listen 80;
listen 443 ssl http2;
charset utf8;
server_name www.wang.org;
ssl_certificate /apps/nginx/cert/www.wang.org.pem;
ssl_certificate_key /apps/nginx/cert/www.wang.org.key;
ssl_session_timeout 10m;
root /data/nginx/html/pc;
access_log /apps/nginx/logs/www.wang.org_access.log main;
location / {
proxy_pass http://webcluster/;
proxy_set_header Host $http_host;
}
}
[root@rocky8 conf.d]# nginx -s reload
# 客户端测试
[root@ubuntu2004 ~]#curl -b wdy=ddd www.wang.org
10.0.0.28
[root@ubuntu2004 ~]#curl -b wdy=ddd www.wang.org
10.0.0.28
[root@ubuntu2004 ~]#curl -b wdy=ddd www.wang.org
10.0.0.28
[root@ubuntu2004 ~]#curl -b wdy=yyy www.wang.org
10.0.0.18
[root@ubuntu2004 ~]#curl -b wdy=yyy www.wang.org
10.0.0.18
[root@ubuntu2004 ~]#curl -b wdy=yyy www.wang.org
10.0.0.18
[root@ubuntu2004 ~]#curl -b wdy=yyy www.wang.org
10.0.0.18
3、实现 HTTPS 的负载均衡

Nginx负载均衡_nginx_02

1、环境准备

服务器

IP地址

作用

系统版本

Proxy代理服务器

10.0.0.8

负载均衡Nginx Web服务器

Rocky8.6

Web1-Nginx服务器

10.0.0.18

网站服务器1

Rocky8.6

Web2-Nginx服务器

10.0.0.28

网站服务器2

Rocky8.6

client

10.0.0.101

测试访问网站

Ubuntu2004

2、配置
[root@rocky8 conf.d]# vim www.wang.org.conf
upstream webcluster {
server 10.0.0.18:80 ;
server 10.0.0.28:80 ;
}
server {
listen 80;
charset utf8;
server_name www.wang.org;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
charset utf8;
server_name www.wang.org;
ssl_certificate /apps/nginx/cert/www.wang.org.pem;
ssl_certificate_key /apps/nginx/cert/www.wang.org.key;
ssl_session_timeout 10m;
root /data/nginx/html/pc;
access_log /apps/nginx/logs/www.wang.org_access.log main;
location / {
proxy_pass http://webcluster/;
proxy_set_header Host $http_host;
}
}
[root@rocky8 conf.d]# nginx -s reload
# 客户端测试
[root@ubuntu2004 ~]#curl www.wang.org -iLk
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.22.0
Date: Sat, 17 Sep 2022 14:10:41 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: https://www.wang.org/

HTTP/2 200
server: nginx/1.22.0
date: Sat, 17 Sep 2022 14:10:41 GMT
content-type: text/html; charset=utf8
content-length: 10
last-modified: Sat, 17 Sep 2022 13:22:00 GMT
etag: "6325c9f8-a"
accept-ranges: bytes

10.0.0.18
[root@ubuntu2004 ~]#curl www.wang.org -iLk
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.22.0
Date: Sat, 17 Sep 2022 14:10:44 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: https://www.wang.org/

HTTP/2 200
server: nginx/1.22.0
date: Sat, 17 Sep 2022 14:10:44 GMT
content-type: text/html; charset=utf8
content-length: 10
last-modified: Sat, 17 Sep 2022 13:22:35 GMT
etag: "6325ca1b-a"
accept-ranges: bytes

10.0.0.28