让NGINX用独角兽提供.gz压缩资产文件

时间:2012-03-21 09:58:08

标签: deployment nginx unicorn

我希望在我的nginx和unicorn中激活gzip压缩:

我在 config / unicorn.rb中的rails应用程序中有这个:

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

我在我的rails app中的 nginx.conf 中有这个:

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

如何启用此配置,例如:

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

谢谢!

2 个答案:

答案 0 :(得分:5)

在配置中添加server { }块:

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

结帐Rails guides了解更多信息。

答案 1 :(得分:1)

这就是我在ggin的nginx.conf中所拥有的:

gzip on;
gzip_buffers 16 8k;
gzip_comp_level 9;
gzip_http_version 1.0;
gzip_min_length 0;
gzip_types text/plain text/css image/x-icon image/png image/jpg image/jpeg text/js text/php application/javascript application/x-javascript;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable     "MSIE [1-6]\.";

您还可以记录gzip压缩:

log_format main
        '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $bytes_sent '
        '"$http_referer" "$http_user_agent" '
        '"$gzip_ratio"';