Browser Caching with Rails 4.0.2 with Nginx and unicorn

15 views
Skip to first unread message

himanshu saxena

unread,
May 29, 2014, 4:22:27 AM5/29/14
to rubyonra...@googlegroups.com

I am facing an issue with rails application deployed on Amazon with nginx and unicorn. I am not using any controller or action to render a view, rather I am using a simple html page inside a sub folder of public folder named html_app. And all the css, js and images are kept inside that html_app folder with the same name. see below:
public/html_app/js/test.js
public/html_app/css/test.css
public/html_app/img/test.png
public/html_app/index.html

In order to use browser caching, I have given below configuration for nginx in config/nginx.conf, see below:

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

server {
listen 80;
server_name x.x.x.x;
root /home/ec2-user/cache_app/public;

# location ^~ /home/ec2-user/cache_app/public {
# gzip_static on;
# expires max;
# add_header Cache-Control public;
# }

location ^~ /.(jpg|jpeg|gif|css|png|js|ico)/ {
# gzip_static on;
expires 1d;
# add_header Cache-Control public;
# add_header Last-Modified "";
# add_header ETag on;
# break;
}

try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}

error_page 500 502 503 504 /500.html;
client_max_body_size 20M;
keepalive_timeout 10;

}

Everything is working except, expires in location block, if I define expires globally, it works, but not inside the location.

Please suggest, where am I wrong ?

Thanks

mike2r

unread,
May 29, 2014, 8:51:54 PM5/29/14
to rubyonra...@googlegroups.com
I'm a little rusty on this, but i think it's your regex.  you definitely need a $ sign.  as it's written, it will match only the filenames .jpg, .peg, etc (filenames that actually have only those four characters).  I also seem to remember the ^~ is used when specifying directory entries vs regex expressions.  The following might work

location ~* \.(jpg|jpeg|gif|css|png|js|ico)$ {

 
Reply all
Reply to author
Forward
0 new messages