NgInx作为Kong的反向代理 [英] NgInx as reverse proxy with Kong
问题描述
我想使用Kong作为在Docker容器中运行的API网关.每个请求必须首先通过NgInx服务器,如果请求的uri与example.com/api相匹配,则必须生成在Kong内注册的api.
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.
为此,我已使用以下命令将API添加到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'
通过执行以下命令,我会得到正确的答案,所以我认为Kong可以正常工作.
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'
我的NgInx配置如下:
My NgInx configuration looks like this:
upstream kong {
server 127.0.0.1:8000;
}
location /api {
proxy_pass: http://kong;
}
在我的主机文件中,我已使用example.com域配置了NgInx服务器的IP.
In my host file I've configured the IP of the NgInx server with the domain example.com.
问题是:当我浏览到example.com/api/my-api甚至example.com/my-api时,结果是NgInx的404错误页面.
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.
当我浏览到ipnumber:8000/api/my-api时,它会显示一条消息,说Kong没有匹配给定值的api,这是正确的,因为主机名不是example.com
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
很长一段时间以来,我一直在寻找这个问题,但是我无法解决它.我也在寻找 https://getkong.org /docs/0.10.x/configuration/#custom-nginx-configuration-embedding-kong ,但由于我已经拥有自己的nginx配置,因此我不确定是否必须这样做.
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.
在此先感谢您的反馈.
推荐答案
您需要告诉NGINX将Host标头转发到Kong.您可以像这样使用proxy_set_header
来做到这一点:
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的反向代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!