2017-04-19 27 views
0

我對Kong很陌生,並且試圖讓我對Kong的手變得很髒。 我有一個環境,其中kong(0.10)和Cassandra(最新)在單獨的碼頭集裝箱上運行。我的操作系統是macOS-Sierra 10.12.4。Kong(0.10)API設置和用法

卡桑德拉在泊塢窗

docker run -d --name kong-database -p 9042:9042 cassandra:latest 

港在泊塢窗

docker run -d --name kong \ 
    --link kong-database:kong-database \ 
    -e "KONG_DATABASE=cassandra" \ 
    -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ 
    -e "KONG_PG_HOST=kong-database" \ 
    -p 8000:8000 \ 
    -p 8443:8443 \ 
    -p 8001:8001 \ 
    -p 7946:7946 \ 
    -p 7946:7946/udp \ 
    kong 

按照該Kong documentation,我通過香港的管理員API添加我的第一個API(獲取IP) 。

curl -i -X POST \ 
    --url http://localhost:8001/apis/ \ 
    --data 'name=example-api' \ 
    --data 'hosts=example.com' \ 
    --data 'upstream_url=http://httpbin.org/ip' 
HTTP/1.1 201 Created 
Date: Wed, 19 Apr 2017 23:39:13 GMT 
Content-Type: application/json; charset=utf-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Credentials: false 
Server: kong/0.10.1 

{"http_if_terminated":true,"id":"9bab7ba9-f1f2-4b4d-aa9d-9966da17f94b","retries":5,"preserve_host":false,"created_at":1492645153409,"upstream_connect_timeout":60000,"upstream_url":"http:\/\/httpbin.org\/ip","upstream_send_timeout":60000,"https_only":false,"upstream_read_timeout":60000,"strip_uri":true,"name":"example-api","hosts":["example.com"]} 

當我執行下面的命令來測試它,

curl -i -X GET --url http://localhost:8000/ --header 'Host: example.com' 

我得到這個,

HTTP/1.1 404 NOT FOUND 
Date: Wed, 19 Apr 2017 23:40:48 GMT 
Content-Type: text/html; charset=UTF-8 
Content-Length: 233 
Connection: keep-alive 
Server: gunicorn/19.7.1 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Credentials: true 
Via: kong/0.10.1 
X-Kong-Upstream-Latency: 590 
X-Kong-Proxy-Latency: 93 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> 
<title>404 Not Found</title> 
<h1>Not Found</h1> 
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p> 

然而,當我直接curl http://httpbin.org/ip,我得到我的IP。

如何配置Kong以獲得相同的結果?

回答

0

嘗試將upstream_url更改爲http://httpbin.org,然後將下面捲曲請求:

curl -i -X GET --url http://localhost:8000/ip --header 'Host: example.com' 

我得到了相同的消息今天才知,這是不行的,當你在最後一個斜槓。也許有一些東西會產生這種情況,當你用這個upstream_url添加API時。

編輯: 隨着你的配置,也許要求應該是這樣的:

curl -i -X GET --url http://localhost:8000 --header 'Host: example.com' 

請注意在URL的末尾刪除斜槓。這也許會導致這個錯誤,你可以修復它,而不必編輯你沒有在測試網址

指明您的API配置:)你添加

0

您的API有一個名稱(例如,API)它應該是這樣的:

捲曲-i -X GET --url http://localhost:8000/example-api --header「主持人:example.com」

+0

我不記得有必要在請求URL –

+0

明確指定的API名稱Matthias S,那麼kong proxy如何知道重定向你的調用的API? :) –

+0

我以爲主頭是爲了這個目的。還是在v10.x中有一些突破性改變?我可以通過Kong發送請求,而無需在請求URL中指定確切的api。那麼,至少0.9.x.另外快速入門指南並沒有引用此行爲 –