(一)项目准备
服务器主机名 | 主机地址 内 :外 |
---|---|
负载均衡:lb01 | 172.16.1.5 :10.0.0.5 |
web01 | 172.16.1.7 :10.0.0.7 |
web02 | 172.16.1.8 :10.0.0.8 |
web03 | 172.16.1.9 :10.0.0.9 |
(二)环境配置
web服务器配置https://blog.csdn.net/qq_38255759/article/details/124714237
负载均衡服务器配置:
第一步:更新yum源:安装nginx官方仓库
[root@lb01 ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
第二步:安装nginx
[root@lb01 ~]# yum install nginx -y
第三步:启动、开机自启nginx服务
[root@lb01 ~]# systemctl start nginx
[root@lb01 ~]# systemctl enable nginx
第四步:配置nginx:创建配置文件
[root@lb01 ~]# vim /etc/nginx/conf.d/lb.conf
upstream jiqun { # 可选参数 追加在尾部; 默认:轮询模式;
server 172.16.1.7:80; # weigth=1 权重值;
server 172.16.1.8:80; # backup 备用服务器;
server 172.16.1.9:80; # hash_map
}
server {
listen 80;
server_name www.blog.com;
location / {
proxy_pass http://jiqun; # 调用服务器集群
proxy_set_header Host $host; # 调用用户请求头
proxy_set_header X-Forward-For $remote_addr; #获取用户真实IP
}
}
#可选参数
#client_max_body_size 10m; 用户请求最大单文件字节数
#client_body_buffer_size 128k; 缓冲区代理缓冲用户端请求的最大字节数
#proxy_connect_timeout 90; nginx跟后端服务器连接超时时间(代理连接超时) #proxy_send_timeout 90; 后端服务器数据回传时间(代理发送超时)
#proxy_read_timeout 90; 连接成功后,后端服务器响应时间(代理接收超时)
#proxy_buffer_size 4k; 设置代理服务器(nginx)保存用户头信息的缓冲区大小
#proxy_buffers 4 32k; proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
#proxy_busy_buffers_size 64k; 高负荷下缓冲大小(proxy_buffers*2)
#proxy_temp_file_write_size 64k; 设定缓存文件夹大小,大于这个值,将从upstream服务器传
第五步:测试nginx文件,重启nginx服务
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx
第六步:打开浏览器测试
第一次刷新
第二次刷新
第三次刷新
Ps:精简nginx主配置文件 -(可选)
[root@lb01 ~]# cp /etc/nginx/nginx.conf{,.bak}
[root@lb01 ~]# grep -Ev '^$|#' /etc/nginx/nginx.conf.bak >/etc/nginx/nginx.conf
[root@lb01 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}