Search code examples
keycloakapache-apisix

Apache apisix routing configuration


I am new to apache APISIX, I want to configure the routing in the Apache APISIX gateway. First I have followed the APISIX official document. In that document, they have used "httpbin.org:80" for the upstream server. and it works for me, If I set the upstream new upstream server which is run in my localhost(127.0.0.1) it does not work for me. it throws a bad gateway error(502)

If anyone knows the answer to fix this issue, please let me know.

{
    "methods": [
        "GET"
    ],
    "host": "example.com",
    "uri": "/anything/*",
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "httpbin.org:80": 1
        }
    }
}

The above routing configuration is working for me. Here is the API GATEWAY (http://127.0.0.1:9080/anything/*) routes the request to http://httpbin.org:80/anything/*)

{
    "methods": [
        "GET"
    ],
    "host": "example.com",
    "uri": "/anything/*",
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "127.0.0.1:3001": 1
        }
    }
}

In the above configuration, I have configured the routing to service and that service is running on my local machine, and that port is 30001. Now if I call the API (http://127.0.0.1:9080/anything/*) it does not route my request to the server (http://127.0.0.1:3001/anything/*), instead it throws a bad gateway error.

const http = require('http')

const hostname = '127.0.0.1'
const port = 3001

const server = http.createServer((req, res) => {
  res.statusCode = 200
  res.setHeader('Content-Type', 'text/plain')
  res.end('Hello World\n')
})

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`)
})

Here the above code is my backend server, which is running as an upstream server.

If you know the answer to debug the bad gateway exception, kindly let me know.


Solution

  • I have faced the exact issue.

    I'm assuming you are deploying APISIX with docker-compose or docker (with is recommended in their official documentation).

    The docker applications are running on a docker bridge network named apisix. This is why your localhost application is not reachable by APISIX.