Nginx根据URL实现负载均衡

2018-03-20 21:35:01
926次阅读
0个评论
这里只提供了一种方式,针对location进行接口的定向分发。
已最简单的配置说清楚接口定向分发,对于其他配置不做讲解。
比如请求两个URL:
1)、www.000.com/sale

2)、www.000.com/matchmaker


#user  nobody;  
worker_processes  1;  
  
events {  
    worker_connections  1024;  
}  
  
http {  
    include       mime.types;  
    default_type  application/octet-stream;  
    sendfile        on;  
    keepalive_timeout  65;  
    upstream sale {  
        server 192.168.1.100:8000 max_fails=2;  
     }  
  
    upstream matchmaker {  
        server 192.168.1.200:8080 max_fails=2;  
     }  
  
    server {  
        listen       80;  
        server_name  www.000.com;  
        location /sale {  
            root /www  
            proxy_pass  http://sale;  
        }  
  
        location /matchmaker {  
             root /www  
             proxy_pass http://matchmaker;  
        }  
    }  
}
说明:
当请求http://www.000.com/sale到达时,监听端口80端口的域名www.000.com根据location匹配到sale,然后根据字段proxy_pass  http://sale去找到对应的upstream,这时请求就会到达192.168.1.100:8000这台机器。
就做到了根据url定向转发实现负载均衡


收藏00

登录 后评论。没有帐号? 注册 一个。