🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
Nginx HTTP负载均衡配置: http://nginx.org/en/docs/http/load_balancing.html 负载均衡方法(算法) RR(轮询) 依次转发 WRR(权重轮询) 最少连接数 IP-HASH 可能不均衡 环境准备:(3台机器) LBS(负载均衡服务器) IP:100 Nginx做负载均衡 WEB01 IP:111 Nginx作为HTTP服务 WEB02 IP:112 Nginx作为HTTP服务 Nginx配置文件修改 LBS(负载均衡服务器) http://nginx.org/en/docs/http/load_balancing.html 选择负载均衡算法 (轮询、权重轮询 最少连接数 IP-HASH) 配置文件 xxx.conf 如下 upstream shop { # 服务器组名称 #默认使用轮询 #ip_hash #least_conn server 192.168.91.161 weight=x; server 192.168.91.162 weight=y; } server { listen 80; server_name xxx.xxx.com; access_log /var/log/nginx/lbs.access.log; #访问日志 error_log /var/log/nginx/lbs.error.log; #错误日志 location / { proxy_pass http://shop; # 与 upstream 服务器组名一致 proxy_set_header Host $host; # Host lbs.1906.com proxy_set_header X-Real-IP $remote_addr; } } LBS的nginx配置例子: upstream shop { server 192.168.235.111; //两台web服务器的IP server 192.168.235.122; } server { listen 80; server_name shop.1906.com; //域名 access_log logs/lbs.1906.access.log; #访问日志 error_log logs/lbs.1906.error.log; #错误日志 location / { proxy_set_header Host $host; proxy_pass http://shop; } } web1的nginx配置: server { listen 80; server_name shop.1906.com; access_log logs/lbs.1906.access.log; #访问日志 error_log logs/lbs.1906.error.log; #错误日志 location / { root /wwwroot/1906shop/public; index index.php index.html index.htm; try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { root /wwwroot/1906shop/public; fastcgi_pass 127.0.0.1:9000; # PHP-FPM fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } web2的nginx配置: server { listen 80; server_name shop.1906.com; access_log logs/lbs.1906.access.log; #访问日志 error_log logs/lbs.1906.error.log; #错误日志 location / { root /wwwroot/1906shop/public; index index.php index.html index.htm; try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { root /wwwroot/1906shop/public; fastcgi_pass 127.0.0.1:9000; # PHP-FPM fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }