I'm using Rails api with an angularjs front-end which is served simply as static files under public
directory. I've chosen passenger as the app server, deployed to heroku and everything seems to be working fine except for caching.
Since static assets are served by passenger/nginx, I believe this has nothing to do with rails. But I have no idea how to get it working or where to add configurations.
Response headers when requesting a static file (application-a24e9c3607.js):
Connection: keep-alive
Content-Length: 0
Date: Thu, 14 Jan 2016 06:45:31 GMT
Etag: "5696ce02-43102"
Last-Modified: Wed, 13 Jan 2016 22:21:54 GMT
Server: nginx/1.8.0
Via: 1.1 vegur
I was able to solve it like this:
create nginx.conf.erb file:
cp $(passenger-config about resourcesdir)/templates/standalone/config.erb nginx.conf.erb
Inside server
block in nginx.conf.erb, instruct Nginx to generate appropriate headers when a file under our assets directory is requested:
server {
# ....
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
}
Pass Nginx engine options to passenger in Procfile:
web: bundle exec passenger start -p $PORT --max-pool-size 3 --nginx-config-template nginx.conf.erb