Kong / kong

🦍 The Cloud-Native API Gateway and AI Gateway.
https://konghq.com/install/#kong-community
Apache License 2.0
38.89k stars 4.78k forks source link

Kong + Gzip doesn't work #1200

Closed ErwannRobin closed 3 years ago

ErwannRobin commented 8 years ago

Probably a newbie question here, but I am struggling with it for 2 days and can't find the answer...

So: I can't manage to make Kong accept the "Accept-Encoding: gzip" header. Whenever I make a request with the "Accept-Encoding: gzip" header, I have a correct 200 OK status, but my answer is empty ... And gzip on the openresty nginx server seems already activated as it already answer "Accept-Encoding: gzip" in the response header...

The problem is that this header is automatically set by browsers, so I can't demo my API if I setup a Kong proxy. So how to make Kong accept (or ignore) this header ?

ErwannRobin commented 8 years ago

Ok, I manage to make it work by deactivating the "response-transformer" plugin. Any idea what is the link between those 2 things ?

ErwannRobin commented 8 years ago

What does "BC" means ?

ahmadnassri commented 8 years ago

@diwann just labels we use to track issues infernally...

we are actively looking at the gzipping questions for Kong.

shashiranjan84 commented 8 years ago

@thefosk @Tieske can this be closed or we waiting for 0.9.0 release?

subnetmarco commented 8 years ago

This is caused when the plugin tries to access the body of the request/response, and it's unable to read it because it's gzipped.

@Diwann what plugin configuration did you have for the Response Transformation plugin?

ErwannRobin commented 8 years ago

I have the following settings:

curl 'https://kong.XXXX/plugins' -X PUT -H 'Content-Type: application/json' --data '{"api_id":"XXXXX-7edf-41dd-9115-16f0e324c882","name":"response-transformer","config":{"remove":{"json":"links"}}}'

However, I haven't checked yet if it's still the case with Konk 0.9

ErwannRobin commented 8 years ago

Yes, I just checked, and it's still the case with 0.9.0 (and "next")

ErwannRobin commented 7 years ago

ok, I finally found a solution: in order to access to the json structure, the response-transformer plugin need to access the body of the response from the backend. But this body is compressed as soon as the client accept gzip encoding. So the trick is to make Kong and the backend to communicate in plain text, and to handle the gzip compression on Nginx side. For that, you need to hide the "Accept-Encoding" header when the query come in, which you can do in the nginx configuration file, under the "location /" section, with:

proxy_set_header Accept-Encoding ""; 

Then, you need to activate the gzip compression on nginx side, under the "server" section, with:

gzip on;
gzip_types text/plain application/json;

🎉

Tieske commented 7 years ago

@Diwann thx for reporting back with the solution you found!

subnetmarco commented 7 years ago

This is great - @Diwann if you submit a PR I will be happy to tweak and merge it.

mloskot commented 3 years ago

A variation to @Diwann 's suggestion for the nginx side from https://github.com/Kong/kong/issues/1200#issuecomment-257940259 is to use the request-transformer plugin on the Kong side and replace the Accept-Encoding header to force Accept-Encoding:identity. By the way, the outbound compression is still not available as per https://github.com/Kong/kong/issues/409

vivekt333 commented 1 year ago

ok, I finally found a solution: in order to access to the json structure, the response-transformer plugin need to access the body of the response from the backend. But this body is compressed as soon as the client accept gzip encoding. So the trick is to make Kong and the backend to communicate in plain text, and to handle the gzip compression on Nginx side. For that, you need to hide the "Accept-Encoding" header when the query come in, which you can do in the nginx configuration file, under the "location /" section, with:

proxy_set_header Accept-Encoding ""; 

Then, you need to activate the gzip compression on nginx side, under the "server" section, with:

gzip on;
gzip_types text/plain application/json;

🎉

I have the nginx-kong.conf file with 3 server blocks kong, kong_gui, kong_admin i have to add proxy_set_header Accept-Encoding ""; in server_name kong location / block ?

when i restart the kong it overrides those directive, i see the kong.conf have a nginx inject block but i didn't find a way to inject in location / block