How to cache the gzip content in nginx? - nginx

when several clients request the same file, which is responsed by the gzip function in nginx . I hope that other responses could use the cached gzip content . How to config ?

There was a discussion of the same in NGINX forums.
I find that this suggestion makes the most sense. However, it mostly applies to when you do proxy with NGINX and not fastcgi cache.
Essentially you will ensure Accept-Encoding: gzip is sent to your backend to ensure that you always generate/cache gzipped content, and then use gunzip module for clients that don't request gzip encoding.

Related

nginx gzip - Caching

In the nginx config, I have enabled gzip compression on and I have not enabled any caching headers.
By default, whether nginx compress only the first client request and store the compressed files in the cache or does it compress for all the requests. Thanks.

Turning of gzip on nginx for some uris

I have this nodejs app behind nginx, let's call it A. Some requests to the app are proxied to another nginx/nodejs server (this is B, a backend API) by the nodejs app (not by nginx).
Currently, both nginx enable gzip compression, which is an issue. Responses from B to A are compressed, and A compresses them again, making the response unusable by the browser.
The ideal solution would be to tell A not to compress responses for requests transferred to B. Unfortunately, I can't get it to work. Here's my nginx config on A:
gzip on;
gzip_types application/json;
location / {
if ($request ~ /api/) {
gzip off;
}
# proxy to the node app
proxy_redirect off;
proxy_pass http://wtb-backoffice;
}
With that setup, proxied requests are still double-compressed. If I replace gzip off with return 404, surely my /api/smth requests return a 404, so the condition is right.
If I turn off compression on server B and enable it on A without the location condition to turn it off, the content is compressed once, and thus readable - which makes sense. But with the gzip off condition present, responses are received uncompressed.
So I conclude that the gzip off directive works only when the raw content is uncompressed. Otherwise it will stupidly compress it again. Does it make sense for someone, and how can I fix that?
BTW, before you suggest stopping proxying or using nginx for the proxy: A and B do not use the same authentication mechanisms, so the proxy controller on A does some magic and cannot be bypassed.

Nginx Configuration: Is it possible to enable Gzip with HTTPS

Hello I know Gzip over https/SSL is unsecured but my server runs just one blog that is a static file website with so there is no security risk.
So what I would like to do is use both https fro http_v2 and Gzip in my Nginx server configuration.
Does anyone know how to enable them both as it seems that Gzip by default only runs with http?
Thanks
The attacks allow attacker to guess the content (like cookies). If you don't store anything confidential in them, you can safely activate Gzip.

Replace text in compressed response from proxy

I would like to replace parts of response bodies coming from an upstream HTTP server, which I am reverse proxying with Ngninx.
There the HttpSubsModule with the sub_filter directive, but here's the problem
This module just works with plain text. If the response is compressed, it can't uncompress the response and will ignore this response. This module can be compatible with gzip filter module. But it will not work with proxy compressed response. You can disable the compressed response like this:
proxy_set_header Accept-Encoding "";
Hmph.
Are there any alternatives that would allow me to modify proxied content without loosing upstream compression?
We had a similar issue in our case and we used "gunzip" module from ngnix which helped us to uncompress the compressed response and update the response as per the requirement.

What is the canonical method for an HTTP client to instruct an HTTP server to disable gzip responses?

I thought this was a simple google search, but apparently I'm wrong on that.
I've seen that you should supply:
Accept-Encoding: gzip;q=0,deflate;q=0
in the request headers. However, the article that suggested it also noted that proxies routinely ignore that header. Also, when I supplied it to nginx, it still compressed the response message body.
http://forgetmenotes.blogspot.ca/2009/05/how-to-disable-gzip-compression-in.html
So, how do I tell a web server to disable compression on the response message body?
Many web servers ignore the 'q' parameter. The compressed version of a static resource is often cached and is returned whenever the request accepts it. To avoid getting compressed resources, use
Accept-Encoding: identity
The server should not serve you a compressed representation of the resource in this instance. Nor should any proxy. This is the default accepted encoding if none is given, but your client might add a default that accepts gzip, so explicitly providing 'identity' should do the trick.
Do you wish encoding to be disabled altogether?Then skip the Accept-Encoding header itself within http request headers.
Do you wish only gzip compression to be absent in the http response?Then skip gzip from the values list in the http request header.
Do you wish to prioritize different compression techniques that servers support? then use different values between 0 and 1 along-with q argument for each value in the Accept-Encoding http request header. (Currently you are using conflicting value and indicating by weight=0 that you don't know how you'll manage, but you want response to be encoded anyhow)
I think this mod for apache
http://httpd.apache.org/docs/2.2/mod/mod_deflate.html (2)
And this for Nginx
http://wiki.nginx.org/HttpGzipModule (1)
Sounds like what you need depending on which server you plan to use. The rest is up to you!
Please note the nginx module both allows shutting down decompression:
[edit] gzip
Syntax: gzip on | off
Default: off
Context: http
server
location
if in location
Reference: gzip
Enables or disables gzip compression.
And dealing with proxies:
[edit] gzip_proxied
Syntax: gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...
Default: off
Context: http
server
location
Reference: gzip_proxied
It allows or disallows the compression of the response for the proxy request in the dependence on the request and the response. The fact that, request proxy, is determined on the basis of line "Via" in the headers of request. In the directive it is possible to indicate simultaneously several parameters:
off - disables compression for all proxied requests
expired - enables compression, if the "Expires" header prevents caching
no-cache - enables compression if "Cache-Control" header is set to "no-cache"
no-store - enables compression if "Cache-Control" header is set to "no-store"
private - enables compression if "Cache-Control" header is set to "private"
no_last_modified - enables compression if "Last-Modified" isn't set
no_etag - enables compression if there is no "ETag" header
auth - enables compression if there is an "Authorization" header
any - enables compression for all requests
[edit] gzip_types
Best wishes!
Sources:
1) http://forum.nginx.org/read.php?11,96472,214303
2) http://httpd.apache.org/docs/2.2/mod/mod_deflate.html#inflate (Section Output Decompression)

Resources