使用nginx針對URL實現負載均衡或者說接口定向分發

這裏只提供了一種方式,針對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定向轉發實現負載均衡

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章