一 负载均衡准备
准备三台虚拟机或服务器
A 192.168.1.1 (主服务器)
B 192.168.1.2
C 192.168.1.3
二 配置主服务器的Nginx配置文件
nginx 配置文件
user silent;
# nginx 进程数
worker_processes 2;
# 进程文件
pid /run/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for';
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
## 此处配置负载均衡的配置
upstream 192.168.1.1 {
server 192.168.1.2:9999;
server 192.168.1.3:9999;
}
server{
listen 9999;
server_name 192.168.1.1;
charset utf8;
location / {
proxy_pass http://192.168.1.1;
proxy_set_header host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_Forwarded_for;
}
}
}
三 配置负载均衡服务器的Nginx配置文件
被负载均衡的服务器主要是配置方向代理.
192.168.1.2上的nginx的配置,在nginx.conf的配置文件http中的末尾加上下面代码完成方向代理
server {
listen 9999;
server_name 192.168.1.2;
location / {
#proxy_pass 后面的网址为你要去反向代理的网址
proxy_pass http://192.168.1.2:10009/;
#proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
192.168.1.3上的nginx的配置,在nginx.conf的配置文件http中的末尾加上下面代码完成方向代理
server {
listen 9999;
server_name 192.168.1.3;
location / {
#proxy_pass 后面的网址为你要去反向代理的网址
proxy_pass http://192.168.1.3:10009/;
#proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}