使用 nginx 实现动静分离的负载均衡集群*:
LB负载均衡集群分两类: LVS (四层)和 nginx或haproxy (七层) 客户端通过访问分发器的VIP来访问网站 现在应用更复杂,比如现在网站页面有: .php .html .png .jpeg .jsp 等, 有动态页面有静态页面。静态页面一般是不变的* LVS是四层的,基于IP的。现在需要在应用层基于不同的应用进行分发, 七层LB , Nginx / Haproxy都可以支持7层LB
拓扑图:
工作中,希望这样:
静态文件处理:可以使用nginx 或apache
动文件处理: apache ,tomcat
图片文件处理: squid
Nginx 负载均衡基础知识以及部署步骤:
-
基础知识:
Nginx 的 upstream 负载的5种方式,目前最常用 前3 种方式
-
轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
-
weight
指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。
-
ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。
-
fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
-
url_hash(第三方) url哈西
按访问url的hash结果来分配请求,使同样的url定向到同一个后端服务器,后端服务器为缓存时比较有效
部署安装:
-
安装 nginx 时必须先安装相应的编译工具 和相关依赖**
[root@instance-atub3vww ~]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
Nginx源码下载请点击:http://nginx.org/en/download.html
本次安装使用:nginx-1.19.1
-
下载源码包并解压:
[root@instance-atub3vww ~]# wget "http://nginx.org/download/nginx-1.19.1.tar.gz" -O "nginx-1.19.1.tar.gz" [root@instance-atub3vww ~]# tar -zxvf nginx-1.19.1.tar.gz
-
查看Nginx源码目录
[root@instance-atub3vww ~]# ll nginx-1.19.1
-
为nginx设置安装目录和启用的模块
[root@instance-atub3vww nginx-1.19.1]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
查看参数:
[root@xuegod63 nginx-1.8.0]# ./configure --help | grep mp4
参数:
–with-http_dav_module :
启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启–with-http_stub_status_module :
启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)–with-http_addition_module:
启用ngx_http_addition_module支持(作为一个输出过滤器,学神 IT 教育,祝您早日成为技术牛人! Man 老师
支持不完全缓冲,分部分响应请求)–with-http_sub_module :
启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)–with-http_flv_module :
启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)–with-http_mp4_module :
启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件) -
编译和安装
查看CPU逻辑数cat /proc/cpuinfo | grep processor | wc -l
[root@instance-atub3vww nginx-1.19.1]# cat /proc/cpuinfo | grep processor | wc -l 1
[root@instance-atub3vww nginx-1.19.1]# make & make install
-
生成运行nginx的用户:
[root@instance-atub3vww nginx-1.19.1]# useradd -u 8000 -s /sbin/nologin nginx [root@instance-atub3vww nginx-1.19.1]# id nginx uid=8000(nginx) gid=8000(nginx) groups=8000(nginx) [root@instance-atub3vww nginx-1.19.1]#
[root@instance-atub3vww nginx-1.19.1]# ll /usr/local/nginx/
conf | 配置文件 |
---|---|
html | 网站根目录 |
logs | 日志 |
sbin | nginx启动脚本 |
-
备份conf,修改index.html,启动nginx
[root@instance-atub3vww conf]# cp nginx.conf nginx.conf.bak [root@instance-atub3vww nginx]# ./sbin/nginx [root@instance-atub3vww html]# echo "Test Server:101.12.74.199" > index.html [root@instance-atub3vww html]# ../sbin/nginx
查看Nginx是否启动成功:
[root@instance-atub3vww html]# netstat -ntlp
访问Nginx[root@instance-atub3vww html]# curl "http://127.0.0.1:80" Test Server:101.12.74.199
-
nginx服务日常操作:
测试配置文件语法:
[root@instance-atub3vww sbin]# ./nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu
重新加载配置文件:
[root@instance-atub3vww sbin]# ./nginx -s reload
关闭nginx:
[root@instance-atub3vww sbin]# /usr/local/nginx/sbin/nginx -s stop
开机自启动,请参考链接:https://www.jianshu.com/p/72603ec31c74
**
配置Nginx为分发器,实现动静分离:
- 修改nginx.conf,如以下配置
[root@instance-atub3vww conf]# pwd
/usr/local/nginx/conf
[root@instance-atub3vww conf]# vim nginx.conf
user nginx nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
if ($request_uri ~* \.html$){
proxy_pass http://htmlservers;
}
if ($request_uri ~* \.php$){
proxy_pass http://phpservers;
}
proxy_pass http://picservers;
}
#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$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.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 {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# 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;
# }
#}
upstream htmlservers { #定义负载均衡服务器组名称
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
upstream phpservers{
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
upstream picservers {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
}
-
定义负载均衡设备的 Ip
在配置文件nginx.conf的最后一行}前,添加以下内容:
upstream htmlservers { #定义负载均衡服务器组名称
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
upstream phpservers{
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
upstream picservers {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}[root@instance-atub3vww conf]# vim nginx.conf [root@instance-atub3vww conf]# ../sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@instance-atub3vww conf]# ../sbin/nginx -s reload
查看端口有没有在监听:
[root@instance-atub3vww conf]# netstat -ntlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 507/rpcbind tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12900/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 979/sshd
-
配置Web Server1:
生成静态测试文件:
[root@cs00 html]# echo "Test Server:192.168.88.129 Static Server" > index.html
生成动态测试文件:
[root@cs00 html]# cat 8080.php 127.0.0.1:8080-php <? php phpinfo(); ?>
上传一张图片,如下:
配置Web Server2:
生成静态测试文件:[root@cs01 html]# echo "Test Server:192.168.88.130 Static Server" > index.html
生成动态测试文件:
[root@cs01 html]# cat 8080.php 127.0.0.1:8081-php <? php phpinfo(); ?> [root@cs01 html]#
上传一张图片,如下:
-
测试转发静态界面以及动态页面:
[root@instance-atub3vww ~]# curl http://127.0.0.1 Test Server:192.168.88.129 Static Server [root@instance-atub3vww ~]# curl http://127.0.0.1 Test Server:192.168.88.130 Static Server [root@instance-atub3vww ~]# curl http://127.0.0.1 Test Server:192.168.88.129 Static Server [root@instance-atub3vww ~]# curl http://127.0.0.1 Test Server:192.168.88.130 Static Server [root@instance-atub3vww ~]# [root@instance-atub3vww ~]# [root@instance-atub3vww ~]# curl http://127.0.0.1/8080.php 127.0.0.1:8081-php <? php phpinfo(); ?> [root@instance-atub3vww ~]# curl http://127.0.0.1/8080.php 127.0.0.1:8080-php <? php phpinfo(); ?> [root@instance-atub3vww ~]# curl http://127.0.0.1/8080.php 127.0.0.1:8081-php <? php phpinfo(); ?> [root@instance-atub3vww ~]# curl http://127.0.0.1/8080.php 127.0.0.1:8080-php <? php phpinfo(); ?> [root@instance-atub3vww ~]#
测试转发图片:
-
测试自动剔除坏的节点:
停掉一边Nginx,本此演示停的是8081端口服务:
[root@cs01 html]# systemctl stop nginx [root@cs01 html]#
测试结果,Nginx服务正常可用,只访问8080端口的服务:
Nginx负载的5种策略设置方法:
-
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream backserver {
server 192.168.88.129;
server 192.168.88.130;
} -
2、指定权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream backserver {
server 192.168.88.129 weight=1;
server 192.168.88.130 weight=2;
} -
3、IP绑定 ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream backserver {
ip_hash;
server 192.168.88.129;
server 192.168.88.130;
} -
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backserver {
server server1;
server server2;
fair;
} -
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}