Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to deflate js file in nginX?

I’m looking for "how to compress load time js file" and I try the solution of my question (I’m using Extjs).

My friend suggest this too. But, it use Apache as web server. Anybody know how to do the trick in NGINX??

My hosting uses nginx as web server and i don’t know anything about web server configuration.

sorry, if my english bad..

like image 278
Egy Mohammad Erdin Avatar asked Feb 27 '11 07:02

Egy Mohammad Erdin


People also ask

Does Nginx compress?

By default, NGINX compresses responses only with MIME type text/html . To compress responses with other MIME types, include the gzip_types directive and list the additional types. gzip_types text/plain application/xml; To specify the minimum length of the response to compress, use the gzip_min_length directive.

Does nginx support gzip?

You can configure Nginx to use gzip to compress the files it serves on the fly. Those files are then decompressed by the browsers that support it upon retrieval with no loss whatsoever, but with the benefit of a smaller amount of data to transfer between the web server and browser.

How do I know if gzip is enabled nginx?

Double click on the file and select headers. Under 'Response headers' you are looking for the 'Connection-Encoding' field, it will say gzip if it is enabled.


2 Answers

If you do not know anything about web server configuration, I am assuming you also do not know how/where to edit the config file.

The nginx conf file is located at /etc/nginx/nginx.conf (verified in Ubuntu 12.04)

By default, nginx gzip module is enabled. So check from this service whether it is enabled on not using an online tool like this.

If it is disabled, add this before server {...} entry in nginx.conf

# output compression saves bandwidth
gzip  on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;

# make sure gzip does not lose large gzipped js or css files
# see http://blog.leetsoft.com/2007/07/25/nginx-gzip-ssl.html
gzip_buffers 16 8k;

# Disable gzip for certain browsers.
gzip_disable “MSIE [1-6].(?!.*SV1)”;
like image 110
Rahul Arora Avatar answered Oct 06 '22 23:10

Rahul Arora


I make this configuration in my nginx.config you need

gzip  on;
location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
  gzip_static on;
  expires     1w;
  add_header  Cache-Control public;
  add_header  Last-Modified "";
  add_header  ETag "";
}
location ~*  \.(jpg|jpeg|png|gif|ico|css|js|svg)$ {
  gzip_static on;
  expires     1w;
  add_header  Cache-Control public;
  add_header  Last-Modified "";
  add_header  ETag "";
}

like image 21
Ezequiel García Avatar answered Oct 06 '22 23:10

Ezequiel García