Search code examples
deploymentnginxunicorn

Get NGINX to serve .gz compressed asset files with unicorn


I want activate gzip compression in my nginx and unicorn:

I have this in my rails app in config/unicorn.rb:

working_directory "/home/user/project.com/current"
shared_path  = '/home/user/project.com/shared'
pid "#{shared_path}/pids/unicorn.pid"
stderr_path "#{shared_path}/log/unicorn.log"
stdout_path "#{shared_path}/log/unicorn.log"
listen '/tmp/unicorn.project.sock'
worker_processes 2
timeout 30

I have this in my nginx.conf in my rails app:

upstream unicorn {
 server unix:/tmp/unicorn.project.sock fail_timeout=0;
}

 server {
       listen 80 default;
       root ~/project.com/current/public;
       try_files $uri/index.html $uri @unicorn;

       location @unicorn {
                           proxy_pass http://unicorn;
                         }
 error_page 500 502 503 504 /500.html;
}

How can I enable for this config sth like:

  gzip_static on;
  expires max;
  add_header Cache-Control public;

Thank you!


Solution

  • Add to server { } block in your config:

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

    Checkout Rails guides for additional information.