下面都是在http{}里面进行配置
第一种配置:加权轮询,按服务器的性能给予权重
#负责压缩数据流
gzip on;
gzip_min_length 1000;
gzip_types text/plain text/css application/x-javascript;
#设定负载均衡的服务器列表
#weigth参数表示权值,权值越高被分配到的几率越大
upstream hello{
server 192.168.68.43:8080 weight=1;
server 192.168.68.45:8080 weight=1;
}
server {
#侦听的80端口
listen 80;
server_name localhost;
#设定查看Nginx状态的地址
location /nginxstatus{
stub_status on;
access_log on;
auth_basic "nginxstatus";
auth_basic_user_file htpasswd;
}
#匹配以jsp结尾的,tomcat的网页文件是以jsp结尾
location / {
index index.jsp;
proxy_pass http://hello; #在这里设置一个代理,和upstream的名字一样
}
}
第二种配置:ip_hash轮询方法,不可给服务器加权重
upstream lb {
server 192.168.196.130 fail_timeout=20s;
server 192.168.196.132 fail_timeout=20s;
ip_hash;
}
server {
listen 80;
server_name safexjt.com www.safexjt.com;
index index.html index.htm index.php;
location / {
proxy_pass http://lb;
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
include proxy.conf;
}
}