On Fri, Aug 7, 2020 at 3:34 PM vincixu <[email protected]> wrote:

> This example is about how to route request.
>

```
-- plugin

function _M.access(conf, ctx)
    local client_city = ctx.var.geoip_city
    -- your code
end
```

that is the way how to use it in the plugin. I think it is an easy way.

Here is the way we get the cookie item [1] from `vars`, we can use the same
way.

[1] https://github.com/apache/apisix/blob/master/apisix/core/ctx.lua#L76


> I think before this, we should talk about how to convert "geoip" to the
> "geoip_city": using plugin or other ways.
>
> YuanSheng Wang <[email protected]> 于2020年8月7日周五 下午12:38写道:
>
> > Here are two examples:
> >
> > ```
> > # route
> > $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -i -d '
> > {
> >     "uri": "/index.html",
> >     "vars": [
> >         ["geoip_city", "==", "beijing"],
> >     ],
> >     "upstream": {
> >         "type": "roundrobin",
> >         "nodes": {
> >             "127.0.0.1:80": 1
> >         }
> >     }
> > }'
> > ```
> >
> > ```
> > -- plugin
> >
> > function _M.access(conf, ctx)
> >     local client_city = ctx.var.geoip_city
> >     -- your code
> > end
> > ```
> >
> > On Fri, Aug 7, 2020 at 9:53 AM YuanSheng Wang <[email protected]>
> wrote:
> >
> > > > > only get geo_city info  when the geoip condition is set, we don't
> > need
> > > > > calculate ip geo information for every request.
> > > > agreed, geo_ip should not calculated ervery request.
> > >
> > > please take a look at the `cookie` of `vars` [1]. we should use the
> same
> > > way.
> > >
> > > If we refer to geoip variables, the GeoIP library will be called to get
> > > the value.
> > > If we do not refer to these variables, the GeoIP library will never be
> > > called.
> > >
> > > > The request condition can not only be placed on the `route`, but the
> > > plugin
> > > > also has this capability.
> > >
> > > `vars` is the easiest way to support both `route` and `plugin`.
> > >
> > > [1]
> https://github.com/apache/apisix/blob/master/apisix/core/ctx.lua#L76
> > >
> > >
> > >
> > >> > integrate maxmind code into lua-resty-radixtree, only when the route
> > >> with
> > >> > geo condition is matched , then we calculate the geo ip info.
> > >> so why not take geo_ip as a plugin?
> > >> Plugin orchestration is particularly well suited for this.
> > >> The request condition can not only be placed on the route, but the
> > plugin
> > >> also has this capability.
> > >>
> > >> Thanks,
> > >> Ming Wen, Apache APISIX & Apache SkyWalking
> > >> Twitter: _WenMing
> > >>
> > >>
> > >> Li Ling(Lien) <[email protected]> 于2020年8月6日周四 下午7:29写道:
> > >>
> > >> > IMO,the the geoip-route is a feature of *route* module,it shouldn't
> > >> > involved with vars module,
> > >> > and only get geo_city info  when the geoip condition is set, we
> don't
> > >> need
> > >> > calculate ip geo information for every request.
> > >> >
> > >> > here is my solution:
> > >> >   integrate maxmind code into lua-resty-radixtree, only when the
> route
> > >> with
> > >> > geo condition is matched , then we calculate the geo ip info.
> > >> >
> > >> > YuanSheng Wang <[email protected]> 于2020年8月6日周四 下午6:00写道:
> > >> >
> > >> > > >   1、I want route user‘s request from country request into
> > different
> > >> > > upstream
> > >> > > host.
> > >> > >
> > >> > > I think AIPSIX needs to support this feature. it is a useful
> feature
> > >> for
> > >> > > the user.
> > >> > > We do not need to update `resty-radixtree`, only update `ctx.vars`
> > is
> > >> > > enough.
> > >> > >
> > >> > > for example, a way to get the city: `ctx.vars.geo_city` .
> > >> > >
> > >> > > Then the users can use it in `route` or `plugin`, both of the way
> > are
> > >> > fine.
> > >> > >
> > >> > > On Thu, Aug 6, 2020 at 8:55 AM Ming Wen <[email protected]>
> wrote:
> > >> > >
> > >> > > > Optional features should be implemented by plugins,
> > >> > > > and the core should be keep simple.
> > >> > > >
> > >> > > > Thanks,
> > >> > > > Ming Wen, Apache APISIX & Apache SkyWalking
> > >> > > > Twitter: _WenMing
> > >> > > >
> > >> > > >
> > >> > > > YuanSheng Wang <[email protected]> 于2020年8月6日周四 上午8:28写道:
> > >> > > >
> > >> > > > > On Thu, Aug 6, 2020 at 7:39 AM Ming Wen <[email protected]>
> > >> wrote:
> > >> > > > >
> > >> > > > > > IMO, implement geo_ip as plugin is better, not in the route.
> > >> > > > > >
> > >> > > > >
> > >> > > > > I would like to implement it in `route`.
> > >> > > > >
> > >> > > > > It may be a condition, we can support it in `vars'.
> > >> > > > >
> > >> > > > >
> > >> > > > > >
> > >> > > > > > And https://luarocks.org/modules/agladysh/lua-geoip is not
> a
> > >> good
> > >> > > > > > approach,
> > >> > > > > > lua-resty-maxminddb[1] is good for APISIX.
> > >> > > > > >
> > >> > > > >
> > >> > > > > this library is better. agree +1
> > >> > > > >
> > >> > > > >
> > >> > > > > >
> > >> > > > > > [1] https://github.com/anjia0532/lua-resty-maxminddb
> > >> > > > > >
> > >> > > > > > Thanks,
> > >> > > > > > Ming Wen, Apache APISIX & Apache SkyWalking
> > >> > > > > > Twitter: _WenMing
> > >> > > > > >
> > >> > > > > >
> > >> > > > > > JinChao Shuai <[email protected]> 于2020年8月6日周四
> > 上午1:47写道:
> > >> > > > > >
> > >> > > > > > > I also agree with the second approach and to `Yansheng
> Wang`
> > >> that
> > >> > > > users
> > >> > > > > > can
> > >> > > > > > > use their own GeoIP database.
> > >> > > > > > >
> > >> > > > > > > YuanSheng Wang <[email protected]> 于2020年8月5日周三
> > 下午11:40写道:
> > >> > > > > > >
> > >> > > > > > > > Of course, the second way is better for APISIX.
> > >> > > > > > > >
> > >> > > > > > > > The `lua-geoip` is under Dual license, we need to
> confirm
> > >> if we
> > >> > > can
> > >> > > > > use
> > >> > > > > > > > this library.
> > >> > > > > > > >
> > >> > > > > > > > The users needs to download the GeoIP database by
> himself,
> > >> > Apache
> > >> > > > > > APISIX
> > >> > > > > > > > can
> > >> > > > > > > > not ship with the GeoIP data(under LGPL 2.1).
> > >> > > > > > > >
> > >> > > > > > > >
> > >> > > > > > > > On Wed, Aug 5, 2020 at 9:16 PM Li Ling(Lien) <
> > >> > [email protected]>
> > >> > > > > > wrote:
> > >> > > > > > > >
> > >> > > > > > > > > rencently I am thinking about have Geo routing in
> > APISIX,
> > >> > > > > > > > > scenario:
> > >> > > > > > > > >     1、I want route user‘s request from country request
> > >> intoto
> > >> > > > > > different
> > >> > > > > > > > > upstream host.
> > >> > > > > > > > >     2、When users from outside China redirect to
> > >> international
> > >> > > > > > > > homepage(many
> > >> > > > > > > > > video sites like youku.com they ban users from
> outside
> > >> > china).
> > >> > > > > > > > >
> > >> > > > > > > > >
> > >> > > > > > > > > here is two solution I digged into:
> > >> > > > > > > > >
> > >> > > > > > > > > 1、intergrate nginx module
> > >> > > > > > > https://github.com/leev/ngx_http_geoip2_module
> > >> > > > > > > > > and
> > >> > > > > > > > > do variable matching.
> > >> > > > > > > > >      disadvantage: we need calculate the geo data from
> > ip
> > >> > info
> > >> > > > for
> > >> > > > > > > every
> > >> > > > > > > > > request,which is so inefficient.
> > >> > > > > > > > >
> > >> > > > > > > > >
> > >> > > > > > > > > 2、https://luarocks.org/modules/agladysh/lua-geoip
> > >> > > > > > > > >      intergrate this lua module into
> > lua-resty-radixtree,
> > >> > when
> > >> > > > > > geo_ip_*
> > >> > > > > > > > > parameter is set in the routes, we calculate the ip
> > >> country
> > >> > > info
> > >> > > > > from
> > >> > > > > > > > geoip
> > >> > > > > > > > > library.so  that  it wont calculate the geo data for
> > each
> > >> > > > request.
> > >> > > > > > > > >
> > >> > > > > > > >
> > >> > > > > > > >
> > >> > > > > > > > --
> > >> > > > > > > >
> > >> > > > > > > > *MembPhis*
> > >> > > > > > > > My GitHub: https://github.com/membphis
> > >> > > > > > > > Apache APISIX:
> https://github.com/apache/incubator-apisix
> > >> > > > > > > >
> > >> > > > > > >
> > >> > > > > > >
> > >> > > > > > > --
> > >> > > > > > > Thanks,
> > >> > > > > > > Janko
> > >> > > > > > >
> > >> > > > > >
> > >> > > > >
> > >> > > > >
> > >> > > > > --
> > >> > > > >
> > >> > > > > *MembPhis*
> > >> > > > > My GitHub: https://github.com/membphis
> > >> > > > > Apache APISIX: https://github.com/apache/incubator-apisix
> > >> > > > >
> > >> > > >
> > >> > >
> > >> > >
> > >> > > --
> > >> > >
> > >> > > *MembPhis*
> > >> > > My GitHub: https://github.com/membphis
> > >> > > Apache APISIX: https://github.com/apache/incubator-apisix
> > >> > >
> > >> >
> > >>
> > >
> > >
> > > --
> > >
> > > *MembPhis*
> > > My GitHub: https://github.com/membphis
> > > Apache APISIX: https://github.com/apache/incubator-apisix
> > >
> >
> >
> > --
> >
> > *MembPhis*
> > My GitHub: https://github.com/membphis
> > Apache APISIX: https://github.com/apache/incubator-apisix
> >
>


-- 

*MembPhis*
My GitHub: https://github.com/membphis
Apache APISIX: https://github.com/apache/incubator-apisix

Reply via email to