Disable unzipping the zipped response in Ngnix - nginx

I have 5 micro-services that are up and running. One of them is an nginx server that acts as a gateway( reverse proxy for other services). There is another service called 'web' that is an nginx server that serves all the client side static bundles. I have enabled gzipping in the web nginx server. But when the compressed response comes through the gateway nginx server, it decompresses the files and sends them back to the client. I tried setting gzip off and gunzip off in gateway nginx server but it is not working.
Here is the configuration of the web-nginx server:
gzip on;
gzip_comp_level 3;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_min_length 100;
gzip_buffers 4 32k;
Here is the configuration for the gateway ngnix server:
gzip off;
gunzip off;
Any kind of help is appreciated.

You need to add gzip_proxied any; to the backend nginx servers (serving static files)
Compress data even for clients that are connecting to us via proxies,
identified by the "Via" header (required for CloudFront/Cloudflare).
The default value is off which disables compression for all proxied requests, ignoring other parameters; For more info checkout the nginx docs

I found the mistake that, i failed to forward the header using proxy_pass from proxy server to actual server. With help of above answer. it worked.

Related

HTTP/1.1 request response is not compressed by nginx with gzip_http_version 1.1

The nginx documentation for gzip_http_version says:
[gzip_http_version] Sets the minimum HTTP version of a request required to compress a response.
The problem is that when I configure nginx with gzip_http_version 1.1 and make a request with HTTP/1.1 and Accept-Encoding: gzip the response is not compressed.
However, this problem disappears when I configure nginx with gzip_http_version 1.0 (with the rest of the config untouched).
Am I misunderstanding something of how this option works? I'm using nginx 1.14.0 on Ubuntu 18.
User error.
The nginx server from which I was requesting the resource was behind a separate nginx proxy. The real fix was configuring nginx proxy server with proxy_http_version 1.1 (which by default is 1.0).
The gzip_http_version works as described in documentation.

Setup NGINX as client-side content caching server

I'm trying to setup NGINX as local caching server, to cache all of the web content I load, and then return it back to me, to speed up webpages loading. Official NGINX docs offer proxy_cache directive to solve this problem. I set up my server as proxy, by example. Here's my config:
worker_processes 1;
events {}
http {
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g;
server {
location / {
proxy_cache my_cache;
proxy_cache_revalidate on;
proxy_cache_min_uses 1;
proxy_cache_use_stale error timeout updating http_500 http_502
http_503 http_504;
proxy_cache_background_update on;
proxy_cache_lock on;
proxy_pass http://localhost:1416;
}
}
}
NGINX starts fine with this config, but when I set up a proxy in Firefox to http://localhost:1416, it starts saying that proxy server refuses connection. I want to know, what's my misconfigurations there and am I following a right way of setting up NGINX as local client-side caching server?
The configuration you have here:
proxy_pass http://localhost:1416;
means that, for each request that matches location /, Nginx will proxy it to localhost:1416. As your server block doesn't specify a port, Nginx will listen on the default of 80.
Unless you have something configured elsewhere, nothing is listening on port 1416. You could test this with:
curl --proxy http://localhost:1416 http://www.example.com/
which will likely fail with:
Failed to connect to localhost port 1416: Connection refused
While Nginx is often used as a reverse proxy, it is not a good choice for a general purpose caching proxy. Consider, for example, Apache Traffic Server or Squid.

Configuring gzip with nginx

I am using nginx for the first time and have some confusions regarding configurations. I have a nginx as load balancer and backends as nginx as well. With my understanding I have configured mod_security module on the load balancer as its the entry point. I have also added required response headers on the load balancer. Now I have to enable the gzip for nginx. Confusion is where it should be configured? Load balancer or the backend nginx servers?
You can configure gzip globally in /etc/nginx/nginx.conf or just for one site in e.g. /etc/nginx/sites-available/your-site.
The configuration could like this:
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
It depends.
For dynamic gzipping (e.g. HTML output of your site/app)
If your load balancer is a powerful machine, then you may want to do gzip on the load balancer, in order to reduce CPU usage elsewhere.
If you have some modsecurity rules that require inspecting the response body, and gzipping is done in the nodes, then that would mean that modsecurity needs to ungzip backend response/inspect/re-gzip (and thus cause processing overhead) or those rules would simply not work. That's another case when you want to gzip in load balancer.
In all other cases, I assume gzipping on the nodes would be better.
For static files
.. it's best to rely on static gzip (pre-compress your assets). However, since you have many backends, it means pre-compressing assets on each.
If your backends are different websites/apps (that means, you're not doing actual load balancing), it's not an issue.
If your backends are actual nodes of the same app, then you can do max gzip on each node, and "proxy cache" results on the load balancer.

Can nginx do transparent compression of websocket connections?

I have nginx acting as reverse-proxy for an ASP.net / Kestrel back-end server.
I'd like nginx to do gzip compression (ie, permessage-deflate / rfc7692) for my Websocket connections, but I can't find any config options for that.
Can nginx do that? Are there any plugins to make it work? If no, is there something else I can use?
just edit your nginx config. It is the same for static sites, proxies or websockets
gunzip on; // be sure to include this. This enables runtime decompression for clients that do not accept gzip
gzip on; //enables gzip for request
gzip_proxied any; //enables compression on proxies
gzip_types *; //compress everything there is
if this does not work, try compressing the messages befor sending them through the socket.
You can read more about it here: https://docs.nginx.com/nginx/admin-guide/web-server/compression/

Nginx and compressing components with gzip

I'm trying to improve page speed on a site and using "Yslow" and "Page Speed" to monitor the speeds. I am being told by both to "compress components with gzip" and given a listing of a number of CSS and JavaScript files, for example
/css/styles.css?v=6.5.5
/jquery.flexslider.js
/4878.js
/6610.js
/homepage.css?v=6.5.5
Our hosting have informed us that nginx is doing the gzip compression on ALL assets, even if it reverse proxies back to Apache and the folllowing values from the nginx site-enable files, which is enabled at a virtual host level, confirms this:
gzip on;
gzip_disable msie6;
gzip_static on;
gzip_comp_level 9;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
Is there a reason these tools are not picking up by the compression or is it in fact they are not being compressed at all and we need to get our hosting to add something extra?
your hosting provider claims that the requests leave nginx compressed that leaves as potential problem causes:
there's a proxy/cache/virusscanner somewhere on the network path between the nginx server and your client that strips out the compression.
your browser saves an uncompressed version of the asset, and yslow/pagespeed ends up using that (if so make sure you trying it with an empty browser-cache should fix it)
you're hosting provider's claim is false (but the posted config bit seems ok to me )
the problem could be a proxy or cache inbetween the nginx server and your browser that strips out the compression.
Some things to try:
Try checking the url's with on online checker for gzip like http://www.whatsmyip.org/http-compression-test/ or http://www.dnsqueries.com/en/check_http_gzip.php
check locally what the result of curl --compressed --head <your-asset-url> is (you should see a Content-Type: gzip if the response coming in is compressed)

Resources