2017-07-06 339 views
3

我想使用Kong作爲我的API網關,在Docker容器中運行。每個請求都必須先通過NgInx服務器,如果請求的URI與example.com/api匹配,則必須導致在Kong內註冊的api。NgInx作爲Kong的反向代理

要做到這一點,我將我的API來港使用下面的命令:

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' 

通過執行以下命令我得到正確的答案,所以我想香港工作正常。

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

我的nginx的配置是這樣的:

upstream kong { 
    server 127.0.0.1:8000; 
} 

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

在我的主機文件我已經與域example.com配置Nginx的服務器的IP。

問題是:當我瀏覽example.com/api/my-api或甚至example.com/my-api時,結果是NgInx的404錯誤頁面。

當我瀏覽到ipnumber:8000/API /我的API,它會導致香港的消息說有沒有API匹配給定值,這是正確的,因爲主機名不是example.com

我這個問題已經很長時間了,但我還沒有能夠解決這個問題。我也在尋找到https://getkong.org/docs/0.10.x/configuration/#custom-nginx-configuration-embedding-kong,但我不確定是否必須這樣做,因爲我已經擁有了自己的nginx配置。

在此先感謝您的意見。

回答

0

您需要告訴NGINX將Host頭向上遊轉發給Kong。你可以用proxy_set_header這樣做:

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