我是Kong的新手,我正试图弄脏Kong。 我有一个环境,kong(0.10)和Cassandra(最新)在不同的docker容器上运行。我的操作系统是macOS -Sierra 10.12.4。
码头工人中的卡珊德拉:
docker run -d --name kong-database -p 9042:9042 cassandra:latest
Kong in docker :
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,我通过Kong Admin 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 :(得分:0)
尝试将upstream_url更改为http://httpbin.org
,然后更改以下curl请求:
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'
请注意网址末尾删除的斜杠。也许这会导致此错误,您可以修复它,而无需编辑API配置:)
答案 1 :(得分:0)
你添加的api有一个你没有在测试网址中指定的名字(example-api)
所以它应该是这样的:
curl -i -X GET --ur http://localhost:8000/example-api - 主持人&#39;主持人:example.com&#39;