11

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..

Community
  • 1
  • 1
Egy Mohammad Erdin
  • 3,402
  • 6
  • 33
  • 57

3 Answers3

48

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)”;
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Rahul Arora
  • 1,036
  • 9
  • 8
  • i'm not sure, but i find my nginx conf in /etc/nginx/nginx.conf... thanks for your help – Egy Mohammad Erdin Feb 27 '11 at 14:04
  • 2
    `text/html` is included in `gzip_types` by default and will show a warning if you include it again. – doublesharp Apr 25 '17 at 15:49
  • I was wondering why just `gzip on` didn't take care of this by itself and the answer is that only `text/html` is on by default. https://www.nginx.com/resources/admin-guide/compression-and-decompression/ – jrjohnson Aug 29 '17 at 17:11
2

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 "";
}

Ezequiel García
  • 2,616
  • 19
  • 12
  • note that that gzip_static is not available in nginx by default. http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html – Oren Mazor Jun 29 '16 at 20:14
0

You need to use the nginx HTTP gzip or the nginx HTTP gzip static module. The static module would be helpful for content like your JavaScript libraries that rarely changes, saving you needless re-compression for every client.

sarnold
  • 102,305
  • 22
  • 181
  • 238