0

I am trying to create a APISIX gateway for my microservice. I have used the docker to install APISIX gateway using the apisix documentation Getting started (https://apisix.apache.org/docs/apisix/getting-started/README/) Version 3.3.0. It works for route:

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
  "id": "getting-started-ip",
  "uri": "/ip",
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "httpbin.org:80": 1
    }
  }
}'

and curl "http://127.0.0.1:9080/ip" it returns the expected response.

Now i want to change the upstream to localhost:XXXX. I have my dummy api server running at localhost:XXXX (not on docker)

if i do curl "http://localhost:XXXX/api/v1/test_server/testing" it returns back { success: true} as json

now i want to change the apisix upstream as the dummy api server i.e. localhost:XXXX, so what did was change the upstream:

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
  "id": "1",
  "uri": "/api/v1/test_server/testing",
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "localhost:XXXX": 1
    }
  }
}'

but when i try to do curl "http://127.0.0.1:9080/api/v1/test_server/testing" it shows

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>openresty</center>
<p><em>Powered by <a href="https://apisix.apache.org/">APISIX</a>.</em></p></body>
</html>

and in the docker log of APISIX it shows

connect() failed (111: Connection refused) while connecting to upstream, client: localhost, server: _, request: "GET /api/v1/test_server/testing HTTP/1.1", upstream: "http://localhost:XXXX/api/v1/test_server/testing", host: "<host ip and port>"

Can you help me why it not able to stablish connection to my local server.

Thanks in advance

PalMaxone
  • 32
  • 8
  • Is the gateway running in a container, but the backend outside a container? [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) discusses this situation in detail. – David Maze May 23 '23 at 13:45
  • Yes, the APISIX gateway is running in docker container but my backend server is running outside the container. is there a way or i have to run my backend server inside the container also. @DavidMaze – PalMaxone May 23 '23 at 13:54
  • configuring nginx, you can troubleshoot this issue. – Bennison J Jun 09 '23 at 11:00

2 Answers2

1
  1. You deploy APISIX inside a Docker container.
  2. You have one service running outside of the Docker container.
  3. You can't use "localhost:xxxx" inside Docker container, because APISIX will try to access the localhost inside the Docker container instead of your host.

This question is related to your Docker knowledge, check From inside of a Docker container, how do I connect to the localhost of the machine?

Zhiyuan Ju
  • 119
  • 5
0

I was having the same issue, this might be network issue related to docker networking try to dockerize your dummy api server and run it in the same network of apisix configured in the docker-compose file

as 502 is usually because you can’t access your upstream service through apisix. In docker, if you want to access your host service, you can change the upstream hostname from localhost to host.docker.internal if you are using macOS, or 172.17.0.1 if you are using Linux And try to see if you can access it through APISIX

Ahmed Alsum
  • 26
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Blue Robin May 24 '23 at 01:58
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 24 '23 at 12:05
  • replacing localhost with `host.docker.internal` while creating route worked for me – PalMaxone May 29 '23 at 09:27