I'm running an Apache server on localhost and I also set up Kong inside a docker container on localhost. My server has a simple endpoint at orgs/ - It just returns a JSON object when I access it through the browser.
I've added it to Kong and this is what it looks like:
$ curl -i -X GET --url http://localhost:8001/apis/localhost
HTTP/1.1 200 OK
Date: Thu, 02 Mar 2017 04:27:16 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.9.9
{"upstream_url":"http:\/\/localhost","created_at":1488254631000,"id":"6a17a041-247a-4041-b3d6-ebe6bab22e18","name":"localhost","preserve_host":false,"strip_request_path":false,"request_host":"localhost"}
However, I'm getting a 502 error when I try to access it through Kong:
$ curl -v 'http://localhost:8000/orgs' --header 'Host: localhost'
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /orgs HTTP/1.1
> Host: localhost
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 502 Bad Gateway
< Date: Thu, 02 Mar 2017 04:28:43 GMT
< Content-Type: text/plain; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Server: kong/0.9.9
<
An invalid response was received from the upstream server
* Connection #0 to host localhost left intact
Note: If I use my IP address instead of localhost I get a 301 error instead.
I apologize, I can't find all of these because I'm using docker and I can't run the commands. Any tips would be appreciated!
$ kong version) 0.9.9$ kong start --vv)<KONG_PREFIX>/logs/error.log)Assuming the commands shown are ran from the host.
{"upstream_url":"http:\/\/localhost"} means localhost from the point of view of your docker container, ie, kong's container, which is obviously wrong because your service isn't hosted on this container, but on your host.
What you have to do is to get your host's IP address with ip a and give that IP as the upstream_url
I mentioned that I have tried that and I get a 301 error.
Here is the API after changing the request_host and upstream_url:
$ curl -i -X GET --url http://localhost:8001/apis/localhost
HTTP/1.1 200 OK
Date: Thu, 02 Mar 2017 17:42:19 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.9.9
{"upstream_url":"http:\/\/X.X.X.X","created_at":1488254631000,"id":"6a17a041-247a-4041-b3d6-ebe6bab22e18","name":"localhost","preserve_host":false,"strip_request_path":false,"request_host":"X.X.X.X"}
And here is what happens when I try to call it.
$ curl -v 'http://localhost:8000/orgs' --header 'Host: X.X.X.X'
* Trying 127.0.0.1...
* Connected to localhost (X.X.X.X) port 8000 (#0)
> GET /orgs HTTP/1.1
> Host: X.X.X.X
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Thu, 02 Mar 2017 17:35:39 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 307
< Connection: keep-alive
< Server: Apache/2.4.18 (Ubuntu)
< Location: http://X.X.X.X/orgs/
< X-Kong-Upstream-Latency: 1
< X-Kong-Proxy-Latency: 0
< Via: kong/0.9.9
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://X.X.X.X/orgs/">here</a>.</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at X.X.X.X Port 80</address>
</body></html>
* Connection #0 to host localhost left intact
Is my setup wrong?
Looks like the 301 is coming from your Apache server. What do you get when you execute the below on the container hosting the Apache server?
curl -v 'http://localhost/orgs' --header 'Host: X.X.X.X'
@pratimsc thank you for pointing that out! That also gave me the 301, but after a bit of experimentation I found that adding a trailing slash to orgs fixed it.
Sorry to comment on an old thread but: if the upstream service is hosted on the same docker network as kong's, why should I provide the IP of it?
Correct me if I am wrong but aren't 2 docker containers in a shared network supposed to be able to talk to each others seamlessly through "localhost"?
Sorry to comment on an old thread but: if the upstream service is hosted on the same docker network as kong's, why should I provide the IP of it?
Correct me if I am wrong but aren't 2 docker containers in a shared network supposed to be able to talk to each others seamlessly through "localhost"?
localhost in this case is kong container and that is not correct. But if you put your service-name that will be fine
Most helpful comment
Assuming the commands shown are ran from the host.
{"upstream_url":"http:\/\/localhost"}meanslocalhostfrom the point of view of your docker container, ie, kong's container, which is obviously wrong because your service isn't hosted on this container, but on your host.What you have to do is to get your host's IP address with
ip aand give that IP as theupstream_url