Using cowboy_static to serve gzip compressed files - erlang

We are using cowboy version 1.0.3 and we would like to serve compressed gzip versions of our files. We set the flag as noted in the documentation:
{compress, true}
But we notice that files served by the cowboy_static module are not being compressed, do we need to set up our own handler for this or are we missing something ?

Related

Rails sending gzip or uncompressed file

As per rails guides, compression is on atuomatically for assets unless specifically defined differently config.assets.gzip. These, and the uncompressed files are being generated on the application's shared/assets folder.
Context: nginx server version 1.14 + Phusion Passegner 6.0 running
How can I tell whether the call to the server is invoking the .gz vs uncompressed asset (assuming the client browser is not using the cached version)?
In the client request, there will be the Accept-Encoding header
It may contain smth like gzip, deflate, br
In this case, gzipped resource will be sent if there's one available. In the response headers there will be Content-Encoding: gzip then

Do I need use Gzip in a Rails 4 project?

I'm running some audits in my open-source project, BTC-Stores, and some times Chrome shows me that I need "Enable Gzip compression".
Sometime ago I read "High Performance Websites", from Steve Souders, and I already know the basic concepts about how make your page load faster. My project is using Ruby 2.0.0 and Rails 4.
I want know, Rails 4 already have a "gzip like compression" or I need to activate it by some gem or config? If you can, please link some good articles about Rails 4 performance and how to optimize it.
Here's some more about using gzip with Rails!
From http://guides.rubyonrails.org/asset_pipeline.html:
4.1.2 GZip Compression
When files are precompiled, Sprockets also creates a gzipped (.gz)
version of your assets. Web servers are typically configured to use a
moderate compression ratio as a compromise, but since precompilation
happens once, Sprockets uses the maximum compression ratio, thus
reducing the size of the data transfer to the minimum. On the other
hand, web servers can be configured to serve compressed content
directly from disk, rather than deflating non-compressed files
themselves.
Nginx is able to do this automatically enabling gzip_static:
location ~ ^/(assets)/ { root /path/to/public; gzip_static on; #
to serve pre-gzipped version expires max; add_header Cache-Control
public; } This directive is available if the core module that provides
this feature was compiled with the web server. Ubuntu packages, even
nginx-light have the module compiled. Otherwise, you may need to
perform a manual compilation:
./configure --with-http_gzip_static_module If you're compiling nginx
with Phusion Passenger you'll need to pass that option when prompted.
A robust configuration for Apache is possible but tricky; please
Google around. (Or help update this Guide if you have a good example
configuration for Apache.)
Also, the following may be of interest!
http://api.rubyonrails.org/classes/ActiveSupport/Gzip.html
Hope this helps!
Yes you need to use Gzip in a Rails 4 or any other project. You do that in the production server and not on your local.
Duplicated question: Compressing rails assets and nginx gzip (with nginx server)

Get Rails pipeline to gzip xml assets

I'm using the rails asset pipeline for my rails site. The assets:precompile task properly processes and creates gzip versions of my css and js files automatically.
I've now added some xml assets. The asset pipeline lets me reference the files just as I do for binary files like images. However, the precompile task does not create gzip versions of the xml files even if I explicitly put them in "config.assets.precompile". My xml files are large assets and would get about 3 times faster with gzip, so this is an important optimization for my site. Is there any way to configure the pipeline to gzip files with additional extensions like ".xml"?
Notes:
I'm using the asset_sync gem to move all my static assets to the
CloudFront CDN, so I can't rely on my webserver (NGINX) to
automatically gzip the files for me. asset_sync will upload a
gzipped version of a file if the asset-pipeline created one, but it
won't create gzipped versions for you. I could theoretically fork
asset_sync and hack it to gzip xml files before the upload or to
insert an additional task between precompile and the sync tasks, but
I would prefer to avoid maintaining a fork of the gem.
I've also thought tricking the asset pipeline by using a .js
extension instead of .xml, but I'd prefer a less hacky solution.
If there's no clean way to just configure gzip on an additional file
type, is there a way to add a custom "postprocessing" filter on files
where I could add gzip manually? I've seen guides on how to add
custom preprocessors but not a post processor on the final hashed
asset file.
This option was added to Rails 4. XML is compressed automatically and Rails exposes a configuration option, zip_files, for specifying which file types to compress.
https://github.com/rails/rails/commit/7cfd1bd76a41bea528c945d821a9fbc3902af479

how to compress javascript files served from my rails app running on apache/passenger?

I'm using Apache 2.2, Passenger 3.0.2, Rails 3.
I've managed to compress the css file by modifying the deflate.conf file. However, when I run the firefox addon yslow, it still says the javascript files are uncompressed.
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript text/javascript application/x-javascript
</IfModule>
Above, I'm using all three variants: "application/javascript text/javascript application/x-javascript" but no luck.
Here's the message from yslow:
Grade D on Compress components with
gzip
There are 3 plain text components that
should be sent compressed
* http://myhost.dyndns.org:8080/javascripts/jquery-1.4.2.min.js?...
* http://myhost.dyndns.org:8080/javascripts/jquery.validate.min.js?...
* http://myhost.dyndns.org:8080/javascripts/rails.js?...
Jammit is an industrial strength asset packaging library for Rails, providing both the CSS and JavaScript concatenation and compression that you'd expect, as well as YUI Compressor and Closure Compiler compatibility, ahead-of-time gzipping, built-in JavaScript template support, and optional Data-URI / MHTML image and font embedding.
http://documentcloud.github.com/jammit/
Assuming that you are using capistrano, add a deployment task that compresses our javascript files in-place on the production server.
Check this:
http://blog.jcoglan.com/2007/05/26/make-capistrano-compress-your-javascript-and-css-automatically/

Compressing a JSON response from a Rails app

We have an app that queries for locations for a customer. We're getting to the point where some customers could have upwards of 10,000 locations. The JSON response for this can get quite large, over 1mb sometimes.
I'm wondering first off the best way to compress this. We have apache in front of a Rails app running in trinidad with JRuby. Can I just set mod_deflate to always compress any responses that are application/json? How might I go about doing this?
Next, what is the browser support for gzip'd json? When I gzip a sample response of 200k it goes down to 30k. That's a significant savings. We're really like to be able to minimize the size of that response without having to minimize the number of locations returned.
In general, for newer versions of Rails, you can do it by adding
use Rack::Deflater
before the "run" line in the config.ru file. This will work perfectly with browsers/clients that support gzip. We use it in production on major websites.
Note for JRuby users: This assumes that your Rails app is launched through Rack, which it often isn't for JRuby. You need a recent version of JRuby-Rack and configure it in Warbler to run in 'rack' mode instead of 'rails' mode.
If the browser supports gzip'd/deflated data, then JSON will go through it just fine. AJAX data is just a regular HTTP request that was done on behalf of a script, rather than a human. At the HTTP level, there's absolutely zero difference between transferring some HTML or a JSON string - it's just data.
For Googlers... [I'm running Apache 2.2.16 and don't care about IE6]
JSON Responses with Content-Encoding = gzip didn't happen until I edit mod_deflate.conf to include this:
AddOutputFilterByType DEFLATE application/json
You can check server response headers with Firefox / Firebug / Net tab
First, make sure you have apache's mod_deflate installed by running this command.
a2enmod deflate
If this command installed it, restart apache. If not, you're good for now.
service apache2 restart
In apache2.conf usually located at /etc/apache2 append this line to the end of the file. This will include a file that we'll create in a little.
Include mod_deflate.conf
Next, we'll edit the mod_deflate.conf with our options:
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|bz2|sit|rar)$ \
no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
#Skip browsers with known problems
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
The first block of code disables gzipping exes, gzs, pdfs.. etc
The second block of code skips deflation from browsers that don't support it.
Finally, restart apache again
service apache2 restart
The settings were copied from the link below:
http://www.howtoforge.com/apache2_mod_deflate

Resources