nginx如何实现负载均衡
创建三个服务器,其中server1作为反向代理器,server2和server3作为服务器(返回相应资源) 当多次访问server1的时候,会轮回访问server2,server3;即如下图所示:
nginx.conf配置 负载,ssl证书,域名
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 102400;
events {
worker_connections 102400;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_min_length 8k;
gzip_comp_level 2;
client_max_body_size 1024m;
client_header_buffer_size 32k;
client_body_buffer_size 1024m;
server_names_hash_bucket_size 512;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
server_tokens off;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
proxy_read_timeout 36000;
#负载后台服务
upstream mbj {
server 192.168.00.01:9527 weight=1 fail_timeout=10s max_fails=1 ;
server 192.168.00.02:9527 weight=1 fail_timeout=10s max_fails=1 ;
}
#监听端口
server {
listen 80;
listen 443 ssl;
server_name 域名 域名服务器;
client_max_body_size 1024m;
client_body_buffer_size 2048m;
large_client_header_buffers 4 2048m;
ssl_certificate 证书文件名称;
ssl_certificate_key 私钥文件名称;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#后台服务配置,配置了这个location便可以通过http://域名/jeecg-boot/xxxx 访问
location /jeecg-boot {
proxy_pass http://upstream负载key/jeecg-boot/;
proxy_set_header Host ip;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#解决Router(mode: 'history')模式下,刷新路由地址不能找到页面的问题
location / {
root html/dist;
index index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html?s=$1 last;
break;
}
}
}
}