nginx配置负载均衡需要有两个关键配置
在upstream中配置具体负载均衡信息
通过proxy_pass 来引用已经配置好的负载均衡信息
在http{}部分引入mylb.conf文件
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
豌豆资源搜索网站https://55wd.com 广州vi设计公司http://www.maiqicn.com
mylb.conf内容如下
server {
listen 8000;
server_name localhost;
location / {
proxy_pass http://springboot;
proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
upstream springboot{
server 192.168.43.85:8764 max_fails=1 fail_timeout=10s;
server my.com:8764 max_fails=1 fail_timeout=10s ;
hash $cookie_jsessionid;
}
主要配置为
-
server下面的listen,表示监听的端口
-
location标识匹配的路径
-
proxy_pass路径匹配以后的upstream名字
-
upstream里面配置的为负载均衡的具体内容
-
其中server 后面配置的为负载到的服务的机器ip和端口
-
max_fails:失败重试次数
-
fail_timeout:超时时间
-
hash $cookie_jsessionid;根据jsession做会话保持。会对jsessionid做一个hash,每次路由到同一台后端服务上