Kong, 502 Bad Gateway with localhost Service

I am facing to an issue with Kong, i created my service and my route exactly like the 5min quickstart of the Kong’s documentation but i only change some parameters to setup the service with my own api working on localhost:3000.

So there is my setup:

  • Kong running on docker (Docker is running on the same machine than my service)
  • http://localhost:3000/users which is my service always returning JSON
    {“status”: “OK”}

there is what i did to create my service.

I created my service with parameters :

{
	"name": "users",
	"url": "http://localhost:3000",
	"host": "127.0.0.1"
}

then i created the Route with body:

{
	"hosts": [ "127.0.0.1"],
	"methods": ["GET"],
	"paths": ["/"],
	"name": "users"
}

then i tried if everything is working with http://localhost:8080/users with header:
“Host:127.0.0.1”

and i got the reply:

502 Bad Gateway : An invalid response was received from the upstream
server

The docker’s Logs (this one hosting kong) :

2019/03/04 09:28:36 [error] 36#0: *63824 connect() failed (111:
Connection refused) while connecting to upstream, client: 172.18.0.1,
server: kong, request: “GET /users HTTP/1.1”, upstream:
http://127.0.0.1:3000/users”, host: “127.0.0.1”

So we can see Kong trying to access the good endpoint ‘127.0.0.1:3000/’ but can’t access to it.

Maybe Kong trying to access is own container localhost but i don’t know, and i don’t know how to fix it, maybe you can help me!

Related Stackoverflow topic: Kong, 502 Bad Gateway with localhost Service - Stack Overflow

It definitely looks like your docker kong cannot access your upstream running on host machine’s localhost:3000. There are a variety of docker solutions to this problem. The docker forums provide some guidance (like https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/10 and https://forums.docker.com/t/how-to-access-the-host-machines-localhost-127-0-0-1-from-docker-container/40699).

Hope this helps.

Rob

The 502 Bad Gateway error usually happens when there are some network errors between computer and internet severs . This means that a server (not necessarily a web server) acting as a gateway or proxy and received an invalid response from an upstream (or origin) server. In most cases the problem is not with you computer or your internet connection , it’s far more likely that it’s the website’s server instead. Fortunately, there are some common and effective solutions for analyzing and fixing most of the causes of 502 Bad Gateway Errors.

  • Perform a hard-refresh in your browser. On Macs, this is done by pressing Cmd + Shift + R.

  • This problem is due to poor IP communication between back-end computers, possibly including the Web server at the site you are trying to visit. Before analysing this problem, you should clear your browser cache completely.

  • If you get this problem for only some of the Web sites you try to visit then it is likely to be a problem at those sites i.e. one of their pieces of equipment is failing/overloaded. Contact the people at those sites.

  • If you are surfing the Web and see this problem for all Web sites you try to visit, then either 1) your ISP has a major equipment failure/overload or 2) there is something wrong with your internal Internet connection e.g. your firewall is not functioning correctly. In the first case, only your ISP can help you. In the second case, you need to fix whatever it is that is preventing you reaching the Internet.

  • In some cases, this error caused by low computer hard disk space, you can go check the free space of your computer hard disk. If there is really not enough free space left, clean your computer hard disk well.

  • Start your browser in Safe Mode. Running a browser in Safe Mode means to run it with default settings and without add-ons or extensions, including toolbars.

  • If your web application is configured to listen on a socket, ensure that the socket exists in the correct location and that it has the proper permissions

  • Finally, restart your computer/networking equipment. Some temporary issues with your computer and how it’s connecting to your network could be causing 502 errors, especially if you’re seeing the error on more than one website. In these cases, a restart would help.