Kong偵聽四個端口的請求,默認(rèn)情況是:
8000:此端口是Kong用來監(jiān)聽來自客戶端的HTTP請求的捞挥,并將此請求轉(zhuǎn)發(fā)到您的上游服務(wù)。這也是本教程中最主要用到的端口忧吟。
8443:此端口是Kong監(jiān)聽HTTP的請求的端口砌函。該端口具有與8000端口類似的行為,但是它只監(jiān)聽HTTPS的請求瀑罗,并不會產(chǎn)生轉(zhuǎn)發(fā)行為胸嘴〕樱可以通過配置文件來禁用此端口斩祭。
8001:用于管理員對KONG進(jìn)行配置的端口。
8444:用于管理員監(jiān)聽HTTPS請求的端口乡话。
在本文中摧玫,我們將介紹Kong的路由功能,并詳細(xì)說明8000端口上的客戶端請求如何根據(jù)請求頭绑青、URI或HTTP被代理到配置中的上游服務(wù)诬像。
先注冊個API,然后跟著COPY幾個命令玩玩:
入門示例
先做一個最簡單的轉(zhuǎn)發(fā)闸婴。當(dāng)訪問8000端口時坏挠,自動轉(zhuǎn)發(fā)到http://api01.bitspaceman.com:8000/news/qihoo。
1邪乍、先創(chuàng)建兩個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、最后榜配,訪問一下:
原理否纬,等同于Nginx的location。后面介紹下詳細(xì)的用法蛋褥。
hosts屬性
可以設(shè)置多個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屬性
可以設(shè)置多個path
:
{
"paths": ["/service", "/hello/world"]
}
還可以使用正則表達(dá)式:
{
"paths": ["/users/\d+/profile", "/following"]
}
給正則設(shè)置優(yōu)先級:
[
{
"paths": ["/status/\d+"],
"regex_priority": 0
},
{
"paths": ["/version/\d+/status/\d+"],
"regex_priority": 6
},
{
"paths": ["/version"],
"regex_priority": 3
},
]
優(yōu)先級別如下:
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屬性
當(dāng)使用代理的時候淫茵,Kong的默認(rèn)(false)是將上游請求的Host頭設(shè)置為API的upstream_url屬性的主機名溃论。
{
"name": "my-api",
"upstream_url": "http://my-api.com",
"hosts": ["service.com"],
}
客戶端請kong的請求頭:
GET / HTTP/1.1
Host: service.com
設(shè)置為false,kong將從upstream_url中提取主機名作為HOST的值去請求上游服務(wù)痘昌。
GET / HTTP/1.1
Host: my-api.com
設(shè)置為true钥勋,客戶端請求的HOST通過kong透傳到上游服務(wù),而不是從upstream_url提取辆苔。
GET / HTTP/1.1
Host: service.com
strip_uri屬性
指定uri前綴去匹配一個API算灸,但是不包含在上游的請求中。這個參數(shù)接收一個boolean的值驻啤。
uris | strip_uri | 客戶端請求 | 上游請求 |
---|---|---|---|
/mockbin | false | /some_path | not proxied |
/mockbin | false | /mockbin | /mockbin |
/mockbin | false | /mockbin/some_path | /mockbin/some_path |
/mockbin | true | /some_path | not proxied |
/mockbin | true | /mockbin | / |
/mockbin | true | /mockbin/some_path | /some_path |
method屬性
就是GET菲驴、POST、PUT骑冗、DELETE等等赊瞬,不多說了。