配置负载均衡主要是要用upstream配置模块和proxy反向代理的相关配置
1.开六台服务器,
一台作为nginx负载均衡转发器,ip地址:192.168.241.198
三台作为web页面服务器(nginx负载均衡器,会把对应请求转发到这组服务器作轮询访问页面)
这三个台服务器ip地址为:192.168.241.10,192.168.241.11,192.168.241.12
两台作为后端应用程序服务器(nginx负载均衡器,会把对应请求转发到这组服务器作轮询访问php应用程序)
这两台服务器ip地址为:192.168.241.13,192.168.241.15
2.nginx负载均衡转发器配置nginx.conf
#user nobody;
worker_processes 4;
error_log /home/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /run/nginx.pid;
#worker jin cheng zui da da kai wen jian shu
worker_rlimit_nofile 51200;
events {
#shi yong epoll shi jian chu li ji zhi
use epoll;
#worker zuida lianjieshu
worker_connections 51200;
}
http {
include mime.types;
default_type application/octet-stream;
#she zhi zi fu ji
charset utf-8;
#服务器名字的hash表大小
server_names_hash_bucket_size 128;
#用于指定来自客户端请求头headerbuffer大小,对于大多数请求,1KB的缓冲区大小已经足够,如果自定义了消息头或有更大的cookie,可以增加缓冲区大小。这里设置为32KB
client_header_buffer_size 32k;
#用来指定客户端请求中较大的消息头的缓存最大数量和大小,“4”为个数,“128”为大小,最大缓存为4个128KB。
large_client_header_buffers 4 32k;
#开启高效文件传输模式,将tcp_nopush和tcp_nodely两个指令设置为on,用于防止网络阻塞。
sendfile on;
#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;
log_format mylogformat '$http_x_forwarded_for - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
#
#sendfile on;
#防止网络阻塞
tcp_nopush on;
#防止网络阻塞
tcp_nopush on;
#防止网络阻塞
tcp_nodelay on;
#keepalive_timeout 0;
#用于设置客户端连接保持活动的超时时间,在超过这个时间之后服务器会关闭该链接。
keepalive_timeout 65;
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
#指定连接到后端fastCGI的超时时间
fastcgi_connect_timeout 300;
#向fastCGI请求的超时时间,这个值是指已经完成两次握手后向fastCGI传送的超时时间
fastcgi_send_timeout 300;
#接收fastCGI应答的超时时间,这个值已经完成两次握手后接收fastCGI应答的超时时间
fastcgi_read_timeout 300;
#指定读取fastCGI应答第一部分需要用多大的缓冲区,一般第一部分应答不会超过1k,一般设置为64k
fastcgi_buffer_size 64k;
# 指定本地需要用多少和多大的缓冲区来缓冲fastCGI的应答
fastcgi_buffers 4 64k;
#默认值是fastcgi_buffers的两倍
fastcgi_busy_buffers_size 128k;
#在写入fastcgi_temp_path是用多大的数据块,默认值是fastcgi_buffers两倍
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#开启限制IP连接数的时候需要使用
#limit_zone crawler $binary_remote_addr 10m;
#允许客户端请求的最大单个文件字节数
client_max_body_size 300m;
#缓冲区代理缓冲缓冲用户端请求的最大字节数,可以理解为先保存到本地在传给用户
client_body_buffer_size 128k;
#跟后端服务器连接的超时时间_发起握手等候响应超时时间
proxy_connect_timeout 600;
#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理
proxy_read_timeout 600;
#后端服务器数据回传时间_就是在规定时间内后端服务器必须传完所有的数据
proxy_send_timeout 600;
#代理请求缓存区_这个缓存区间会保存用户的头信息以供nginx进行规则处理_一般只要能保存下头信息即可
proxy_buffer_size 16k;
#同上 告诉nginx保存单个用的几个buffer最大用多大空间
proxy_buffers 4 32k;
#如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2
proxy_busy_buffers_size 64k;
#proxy缓存临时文件的大小
proxy_temp_file_write_size 64k;
#web页面转发池
upstream web_server_pool {
server 192.168.241.10:80 weight=4 max_fails=2 fail_timeout=30s;
server 192.168.241.11:80 weight=4 max_fails=2 fail_timeout=30s;
server 192.168.241.12:80 weight=2 max_fails=2 fail_timeout=30s;
}
#php后端程序转发池
upstream php_server_pool {
server 192.168.241.13:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.241.15:80 weight=1 max_fails=2 fail_timeout=30s;
}
#第一个虚拟主机,反向代理php_server_pool 这组服务器
server
{
listen 80;
server_name www.yourdomain.com;
location /
{
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://php_server_pool;
proxy_set_header Host www.yourdomain.com;
proxy_set_header X-Forwarded-For $remote_addr;
}
access_log /home/logs/www.yourdomain.com_access.log;
}
#第二个虚拟主机
server{
listen 80;
server_name www1.yourdomain.com;
location /
{
proxy_pass http://web_server_pool;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
access_log off;
}
}
3.配置web页面应用服务器nginx.conf(就是三个web页面虚拟主机可以参考:https://blog.csdn.net/u014415344/article/details/117465622)
server
{
listen 192.168.241.10:80;
server_name 192.168.241.10;
access_log /home/logs/server10.access.log combined;
location /
{
index index.html index.htm;
root /home/server10;
}
}
server
{
listen 192.168.241.11:80;
server_name 192.168.241.11;
access_log /home/logs/server11.access.log combined;
location /
{
index index.html index.htm;
root /home/server11;
}
}
server
{
listen 192.168.241.12:80;
server_name 192.168.241.12;
access_log /home/logs/server12.access.log combined;
location /
{
index index.html index.htm;
root /home/server12;
}
}
4.配置两台nginx+php集成环境:(可参考https://blog.csdn.net/u014415344/article/details/117820458)
在192.168.241.13机器的nginx.conf里添加
server
{
listen 192.168.241.13:80;
server_name 192.168.241.13;
access_log /home/logs/server13.access.log combined;
location /
{
index index.php index.html index.htm;
}
location ~ \.php$
{
root /home/server13;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在192.168.241.15机器的nginx.conf里添加
server
{
listen 192.168.241.15:80;
server_name 192.168.241.15;
access_log /home/logs/serverphp.access.log combined;
location /
{
index index.php index.html index.htm;
}
#location ~ \.php$ {
# proxy_pass http://192.168.241.15;
#}
#
location ~ \.php$
{
root /home/server15;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
5.测试负载均衡
将配置改完后,重启所有nginx服务器
在对应服务器新建以下测试文件
192.168.241.10,根目录/home/server10下新建index.html 写入server10文字
192.168.241.11,根目录/home/server11下新建index.html 写入server11文字
192.168.241.12,根目录/home/server12下新建index.html 写入server12文字
192.168.241.13,根目录/home/server13下新建index.php写入:
<?php
$a=0;
for($i=0;$i<100000;$i++){
$a+=$i;
echo $a;
}
?>
192.168.241.15,根目录/home/server15下新建index.php写入:
<?php
$a=0;
for($i=0;$i<10;$i++){
$a+=$i;
echo $a;
}
?>
分别访问www.yourdomain.com,www1.yourdomain.com发现可以轮询了。