准备3台服务器,一台做nginx负载均衡,两台为Apache的web服务器
1.首先源码安装nginx
配置nginx负载均衡和nginx负载均衡调度算法为加权轮询,加权轮询比例为1:3,下列代码有注释。
vim /usr/local/nginx/conf/nginx.conf
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;
upstream webservers { #添加nginx反向代理
server 192.168.189.130 weight=1; #nginx需要代理的服务器,可以根据情况加端口
server 192.168.189.131 weight=3; #服务器2
}
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 / {
proxy_pass http://webservers; #反向代理设置为web群组
}
为两台服务器创建测试页面
采用Apache服务,路径:
/var/www/html vim /var/www/html/index.html
web1 web2
测试访问nginx的ip,并测试负载均衡和加权轮询
可以看到1:3的web1/2出现次数
nginx负载均衡的日志:
tail -f /usr/local/nginx/logs/access.log
原文:https://www.cnblogs.com/security-guard/p/12071618.html