NgInx as reverse proxy with Kong

2019-03-28 18:47发布

问题:

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.

回答1:

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;
}


标签: nginx kong