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


I can't send HTTP request to local docker container (accessible via through Kong gateway.

Kong Gateway components:

  • Service test_service: url=;
  • Route test_route: paths[]=/test, name=test_route;

Request to Kong proxy:

$ curl -X GET http://localhost:8000/test
  "message":"An invalid response was received from the upstream server",

Long Version

I created a simple Express app that responds to HTTP requests with "Foo" and "Bar", and dockerized it.



const express = require('express');

const app = express();
var port = 4002;

app.use("/foo", (req, res) => {

app.use("/bar", (req, res) => {

app.use((req, res) => {
  res.send("Test unknown path");

app.listen(port, () => {
  console.log(`Foo-bar listening on port ${port}`)


FROM node:lts-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "index.js"]

After building and running it, I can send HTTP requests to "localhost:4002" and correctly get the responses.


$ docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED              STATUS                 PORTS                                             NAMES
fce1cc4e7b24   mikyll/test-service:latest   "docker-entrypoint.s…"   About a minute ago   Up About a minute>4002/tcp                            nice_blackwell

$ curl -X GET http://localhost:4002
Test unknown path

$ curl -X GET http://localhost:4002/foo

$ curl -X GET http://localhost:4002/bar

Kong Setup

I followed the instructions to setup Kong Gateway on Docker and manage services and routes:

  1. I run Kong Gateway container:
curl -Ls | bash
  1. Tested the connection:
$ curl --head localhost:8001
HTTP/1.1 200 OK
  1. Created a service:
$ curl -i -s -X POST http://localhost:8001/services --data name=test_service --data url=''
HTTP/1.1 201 Created

Service JSON:

  1. Created a route:
$ curl -i -X POST http://localhost:8001/services/test_service/routes --data 'paths[]=/test' --data name=test_route

Route JSON:



When I try to send an HTTP request, I get the error "An invalid response was received from the upstream server.":

$ curl -X GET http://localhost:8000/test
  "message":"An invalid response was received from the upstream server",


Is my configuration (service/route) wrong? Am I missing some setup step?


  • The problem is mainly how you connect Kong with your service on Docker.
    The service means that Kong will call (localhost) with port 4002, basically calling the Kong container itself, not you Expressjs app.

    To fix the issue, run both your ExpressJS app and Kong on the same user-defined network and call the app with Docker's service name (nice_blackwell, in this instance).
    Maybe create a compose file just for easier management.