upyun / slardar

Updating your upstream list and run lua scripts without reloading Nginx.
494 stars 111 forks source link

router supprot? #22

Open liuyangc3 opened 7 years ago

liuyangc3 commented 7 years ago

I found slardar use domain as skey to locate backend servers.

How to router www.abc.com/foo and www.abc.com/bar to different backend servers, like foo to ["127.0.0.1:8080","127.0.0.2:8080"], bar to ["127.0.0.3:8080", "127.0.0.4:8080"]

yejingx commented 7 years ago

Change skey to ngx.var.uri.

liuyangc3 commented 7 years ago

thanks your reply, how to deal with some uri like these config

server_name www.abc.com;
location ~ /.*\.jsp$ { proxy_pass  up1 } 
location ~ /.*\.html$ { proxy_pass  up2 }
location ~ /begin/(path1)|(path2)/.*\.action$ { proxy_pass  up3 }

in location case 3, it will be very hard to write the skey.

jiangsuwwj1 commented 7 years ago

@yejingx

How to change skey to ngx.var.uri

yejingx commented 7 years ago

How to change skey to ngx.var.uri

Replace this line with local skey = ngx.var.uri

jiangsuwwj1 commented 7 years ago

非常感谢,另外最近在使用以及在对slardar做一些改造的时候遇到一些问题,能否加一下微信方便沟通呢?我微信号是:jiangsuwwj thank you very much, recently i encountered some problem when using 'slardar', could you leave a contact for communicating?

yejingx commented 7 years ago

@jiangsuwwj1 我的邮箱: yejingx@gmail.com

jiangsuwwj1 commented 7 years ago

@yejingx 收到,另外这里还有一个问题想要问一下。如何设置默认的动态代理呢?这里举个例子: 例:) Nginx作为负载均衡器,IP为192.168.122.1监听端口80,访问http://192.168.122.1自动代理到后端server,以下是Nginx的配置。

http { ... upstream backend { server 192.168.122.173:8080 weight=1 max_fails=3 fail_timeout=3; server 192.168.122.65:8080 weight=1 max_fails=3 fail_timeout=3; ...
} server { listen 80; server_name localhost; location / { proxy_pass http://backend; } ... }

如果用slardar动态负载来做的话, 该怎么做呢? 我这里考虑了几种方案,不知道对不对, 第一种方案:使用Host来区分,默认slardar使用Host也就是域名来找到对应的服务,这里追加域名指向nginx 服务器,然后追加一条consul记录, key为域名。 第二种方案:使用IP来区分,这里追加一条consul记录,key为nginx的IP也就是192.168.122.1。

不知道以上的理解对不对, 另外想问一下是否还有更好的解决方案呢?

yejingx commented 7 years ago

如何设置默认的动态代理呢?

要做到『默认』是否要考虑 Host 为空和 Host 不在你的列表里两种情况?

如果是的话,建议在 balancer.lua 里做修改:

local my_hosts = {
    hostA = 1,
    hostB = 1,
    hostC = 1,
}
local skey = ngx.var.host
if not skey or not my_hosts[skey] then
    skey = 'defalt_host'
end

同样地,用 ngx.var.uri 作为 skey 也是类似的做法。

jiangsuwwj1 commented 7 years ago

@yejingx 明白了,多谢