代理详解 - 玩转Kong网关

Kong侦听四个端口的请求,默认情况是:

8000:此端口是Kong用来监听来自客户端的HTTP请求的,并将此请求转发到您的上游服务。这也是本教程中最主要用到的端口。

8443:此端口是Kong监听HTTP的请求的端口。该端口具有与8000端口类似的行为,但是它只监听HTTPS的请求,并不会产生转发行为。可以通过配置文件来禁用此端口。

8001:用于管理员对KONG进行配置的端口。

8444:用于管理员监听HTTPS请求的端口。

在本文中,我们将介绍Kong的路由功能,并详细说明8000端口上的客户端请求如何根据请求头、URI或HTTP被代理到配置中的上游服务。

先注册个API,然后跟着COPY几个命令玩玩:

入门示例

先做一个最简单的转发。当访问8000端口时,自动转发到http://api01.bitspaceman.com:8000/news/qihoo

1、先创建两个Service:

curl -i -X POST \
  --url http://localhost:8001/services/ \
  --data 'name=example-service' \
  --data 'url=http://api01.bitspaceman.com:8000/news/qihoo'

curl -i -X POST \
  --url http://localhost:8001/services/ \
  --data 'name=163-service' \
  --data 'url=http://3g.163.com/touch/jsonp/sy/recommend'

2、然后,添加两个Route:

curl -i -X POST \
  --url http://localhost:8001/services/example-service/routes \
  --data 'hosts[]=news.com'

curl -i -X POST \
  --url http://localhost:8001/services/163-service/routes \
  --data 'paths[]=/news'  \
  --data 'hosts[]=news.com'

3、最后,访问一下:

根据hosts=news.com做转发
根据hosts=news.com和paths=/news做转发

原理,等同于Nginx的location。后面介绍下详细的用法。


hosts属性

可以设置多个host,像下面这样:

$ curl -i -X POST http://localhost:8001/routes/ \
    -H 'Content-Type: application/json' \
    -d '{"hosts":["example.com", "foo-service.com"]}'

# 或者

$ curl -i -X POST http://localhost:8001/routes/ \
    -d 'hosts[]=example.com' \
    -d 'hosts[]=foo-service.com'

也可以使用通配符:

{
    "hosts": ["*.example.com", "service.com"]
}


paths属性

可以设置多个path:

{
    "paths": ["/service", "/hello/world"]
}

还可以使用正则表达式:

{
    "paths": ["/users/\d+/profile", "/following"]
}

给正则设置优先级:

[
    {
        "paths": ["/status/\d+"],
        "regex_priority": 0
    },
    {
        "paths": ["/version/\d+/status/\d+"],
        "regex_priority": 6
    },
    {
        "paths": ["/version"],
        "regex_priority": 3
    },
]

优先级别如下:
1、/version
2、/version/\d+/status/\d+
3、/status/\d+

如何捕获正则分组?

如下面一个path:

/version/(?<version>\d+)/users/(?<user>\S+)

支持这样一个请求:

/version/1/users/john

还可以被插件使用:

local router_matches = ngx.ctx.router_matches
-- router_matches.uri_captures is:
-- { "1", "john", version = "1", user = "john" }

Path添加字符的方式 ?

$ curl -i -X POST http://localhost:8001/routes \
    --data-urlencode 'uris[]=/status/\d+'


preserve_host属性

当使用代理的时候,Kong的默认(false)是将上游请求的Host头设置为API的upstream_url属性的主机名。

{
    "name": "my-api",
    "upstream_url": "http://my-api.com",
    "hosts": ["service.com"],
}

客户端请kong的请求头:

GET / HTTP/1.1
Host: service.com

设置为false,kong将从upstream_url中提取主机名作为HOST的值去请求上游服务。

GET / HTTP/1.1
Host: my-api.com

设置为true,客户端请求的HOST通过kong透传到上游服务,而不是从upstream_url提取。

GET / HTTP/1.1
Host: service.com


strip_uri属性

指定uri前缀去匹配一个API,但是不包含在上游的请求中。这个参数接收一个boolean的值。

urisstrip_uri客户端请求上游请求
/mockbinfalse/some_pathnot proxied
/mockbinfalse/mockbin/mockbin
/mockbinfalse/mockbin/some_path/mockbin/some_path
/mockbintrue/some_pathnot proxied
/mockbintrue/mockbin/
/mockbintrue/mockbin/some_path/some_path


method属性

就是GET、POST、PUT、DELETE等等,不多说了。

转载于:https://my.oschina.net/u/1404949/blog/3039501

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值