NgInx as a reverse proxy with Kong

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

To do this, I 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 running the following command, I am getting the correct answer, so I assume Kong is working correctly.

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

      

My NgInx config looks like this:

upstream kong {
  server 127.0.0.1:8000;
}

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

      

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

The problem is this: when I go through example.com/api/my-api or even example.com/my-api, the result is a 404 NgInx error page.

When I look at ipnumber: 8000 / api / my-api, this results in a Kong post which says the api does not match the specified values, which is true because the hostname is not example.com

I've been looking for this issue for a long time now, but I haven't been able to fix it. I looked also at https://getkong.org/docs/0.10.x/configuration/#custom-nginx-configuration-embedding-kong , but I'm not sure if I need to do this because I already have my own nginx configuration.

Thanks in advance for your feedback.

+3


source to share


1 answer


You must tell NGINX to forward the Host header upstream to Kong. You can do it with the proxy_set_header

following:



location /api {
   proxy_pass: http://kong;
   proxy_set_header Host $host;
}

      

0


source







All Articles