Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NgInx as reverse proxy with Kong

Tags:

nginx

kong

I wanna use Kong as my API Gateway, running in a Docker container. Each request must go first through a NgInx server and if the requested uri matches example.com/api it must result in the api, registered inside Kong.

To achieve this I've added my API to Kong with the following command:

curl -i -X POST --url ipnumber:8001/apis -d 'name=my-api' -d `enter code here`'upstream_url=http://httpbin.org' -d 'hosts=example.com' -d 'uris=/api/my-api'

By executing the following command I get the correct answer, so I suppose Kong is working correctly.

curl -i -X GET --url ipnumber:8000/api/my-api --header 'Host: example.com'

My NgInx configuration looks like this:

upstream kong {
  server 127.0.0.1:8000;
}

location /api {
   proxy_pass: http://kong;
}

In my host file I've configured the IP of the NgInx server with the domain example.com.

The problem is: when I'm browsing to the example.com/api/my-api or even example.com/my-api the result is a 404 error page of NgInx.

When I browse to ipnumber:8000/api/my-api it results in a message of Kong saying there's no api matching the given values, which is correct because the hostname isn't example.com

I'm looking to this problem already a long time but I have not been able to fix it. I was looking also to https://getkong.org/docs/0.10.x/configuration/#custom-nginx-configuration-embedding-kong but I'm not sure if I have to do it that way because I've already my own nginx configuration.

Thanks in advance for your feedback.

like image 204
ByTheWay Avatar asked Jul 06 '17 22:07

ByTheWay


People also ask

Is Kong a reverse proxy?

Kong and Traefik are both reverse proxies which can be used to route incoming traffic to all of your services running inside your Kubernetes cluster.

Does Kong use nginx?

Kong is built on NGINX, and uses Lua to implement its API functionality, whereas the API Management Module relies completely on native, high‑performance capabilities that are implemented as NGINX Plus modules.

Can Nginx be used as reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.

Is Kong a proxy server?

The Kong Server, built on top of NGINX, is the server that will actually process the API requests and execute the configured plugins to provide additional functionalities to the underlying APIs before proxying the request upstream. for proxying. This is where Kong listens for HTTP traffic.


1 Answers

You need to tell NGINX to forward the Host header upstream to Kong. You can do that with proxy_set_header like so:

location /api {
   proxy_pass: http://kong;
   proxy_set_header Host $host;
}
like image 188
dustin.schultz Avatar answered Sep 19 '22 01:09

dustin.schultz