Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressing rails assets and nginx gzip

Do I have to configure nginx to compress assets (gzip set to on) if I have compressed rails assets with rake assets:precompile? I mean does it make sense or not? Will performance better or worse? Thank you!

like image 295
ka8725 Avatar asked Dec 05 '12 23:12

ka8725


2 Answers

Yes, you should if you want to improve performance.

Simply add the following block to your site configuration:

location ~ ^/(assets)/  {
  root /path/to/public; # CHANGE THIS
  gzip_static on; # to serve pre-gzipped version
  expires max;
  add_header Cache-Control public;
}

Change the root path in the configuration. That is all there is to it.

RecommendationFromDocumentation™: http://guides.rubyonrails.org/asset_pipeline.html

like image 61
karlingen Avatar answered Sep 18 '22 19:09

karlingen


Do rake assets:precompile and you have to configure nginx for send gzip version of files, i use this configuration.

user www-data www-data;
worker_processes 4;

pid /var/run/nginx.pid;

events{
    worker_connections 2048;
    use epoll;
}

http{
    include mime.types;
    default_type application/octet-stream;

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    server_tokens off;
    server_name_in_redirect off;
    ignore_invalid_headers on;

    gzip off;
    sendfile on;

    upstream reverse-proxy{
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
        server 127.0.0.1:3003;
    }   

    server{
        listen 80;
        server_name _;
        root /home/www-data/my_website/public;

        client_max_body_size 10M;
        client_body_buffer_size 512k;

        location ~ ^/assets/ {
            gzip_static on;

            add_header Cache-Control public;
            expires 4w;
            gzip on;
            gzip_vary on;
            gzip_proxied any;
            gzip_disable "MSIE [1-6]\.";
            gzip_comp_level 6;
            gzip_types application/x-javascript text/css text/html image/x-icon image/png image/jpeg image/gif;
        }

        location / {
            try_files $uri @ruby;
        }

               location @ruby {
                        proxy_set_header  X-Real-IP  $remote_addr;
                        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header  Host $http_host;
                        proxy_redirect    off;

                        proxy_pass http://reverse-proxy;
                }

    }

}
like image 31
Jonathan Vukovich-Tribouharet Avatar answered Sep 22 '22 19:09

Jonathan Vukovich-Tribouharet