New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kong inside docker container on localhost can't find API also on localhost #2149

Closed
bkjohnson opened this Issue Mar 2, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@bkjohnson
Copy link

bkjohnson commented Mar 2, 2017

Summary

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.

Steps To Reproduce

  1. Run a server on localhost which has a simple GET endpoint that returns a JSON object
  2. Set up Kong inside a docker container on localhost.
  3. Add localhost API
  4. Try to access a localhost API and receive error

Additional Details & Logs

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 ($ kong version) 0.9.9
  • Kong debug-level startup logs ($ kong start --vv)
  • Kong error logs (<KONG_PREFIX>/logs/error.log)
  • Kong configuration (registered APIs/Plugins & configuration file)
  • Operating System Linux Mint 18.1
@Vermeille

This comment has been minimized.

Copy link
Contributor

Vermeille commented Mar 2, 2017

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

@bkjohnson

This comment has been minimized.

Copy link
Author

bkjohnson commented Mar 2, 2017

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?

@pratimsc

This comment has been minimized.

Copy link

pratimsc commented Mar 3, 2017

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'

@bkjohnson

This comment has been minimized.

Copy link
Author

bkjohnson commented Mar 4, 2017

@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.

@bkjohnson bkjohnson closed this Mar 4, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment