nginx配置

配置

进入nginx\conf目录,编辑nginx.conf,在#gzip on;这一行下开始配置

upstream  xu {   
    server    127.0.0.1:8080; 
    server    127.0.0.1:8090;  
} 

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            proxy_pass http://xu;
            index  index.html index.htm;
        }
        ...

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

配置两个地方,第一个是upstream,另一个是proxy_pass http:// xxx 
注意upstream 后面的名字需要和server中的location下的proxy_paxx http:// 后的内容相同

访问

因为配置的是listen 80; 所以在进行访问的时候直接访问的是80端口,如果你访问时带上了tomcat启动时的端口将得不到你想要的效果

等待响应时间配置

在location中进行参数配置

proxy_connect_timeout:与服务器连接的超时时间,默认60s
fail_timeout:当该时间内服务器没响应,则认为服务器失效,默认10s
max_fails:允许连接失败次数,默认为1

等待时间 = proxy_connect_timeout + fail_timeout * max_fails
  • 1
  • 2
  • 3
  • 4
  • 5

负载均衡策略

1.轮询 
这种是默认的策略,把每个请求按顺序逐一分配到不同的server

upstream  xu {   
    server    127.0.0.1:8080; 
    server    127.0.0.1:8090;  
} 
  • 1
  • 2
  • 3
  • 4

2.最少连接 
把请求分配到连接数最少的server

upstream  xu {
    least_conn;
    server    127.0.0.1:8080; 
    server    127.0.0.1:8090;  
} 
  • 1
  • 2
  • 3
  • 4
  • 5

3.权重 
使用weight来指定server访问比率,weight默认是1。

upstream  xu {   
    server    127.0.0.1:8080 weight=1; 
    server    127.0.0.1:8090 weight=2;  
}
  • 1
  • 2
  • 3
  • 4

4.ip_hash 
每个请求会按照访问ip的hash值分配,这样同一客户端连续的Web请求都会被分发到同一server进行处理,可以解决session的问题。

upstream  xu {
    ip_hash;
    server    127.0.0.1:8080; 
    server    127.0.0.1:8090;  
}
  • 1
  • 2
  • 3
  • 4
  • 5

ip_hash可以和weight结合使用。

posted @ 2018-09-14 11:42  年轻人的魄力  阅读(69)  评论(0编辑  收藏  举报