Rails + Heroku 上的乘客:如何在 HTTP headers 中为静态资源设置到期日期或最长期限?

Rails + Passenger on Heroku: How to set expiry date or a maximum age in the HTTP headers for static resources?

我将 Rails api 与 angularjs front-end 一起使用,它只是作为 public 目录下的静态文件。我选择 passenger 作为应用程序服务器,部署到 heroku,除了缓存之外一切似乎都运行良好。

由于静态资产是由passenger/nginx提供的,我相信这与rails无关。但是我不知道如何让它工作或在哪里添加配置。

请求静态文件时的响应headers(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

我是这样解决的:

创建 nginx.conf.erb 文件:

cp $(passenger-config about resourcesdir)/templates/standalone/config.erb nginx.conf.erb

nginx.conf.erb中的server块中,指示Nginx在请求我们的资产目录下的文件时生成适当的headers:

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

在 Procfile 中将 Nginx engine options 传递给乘客:

web: bundle exec passenger start -p $PORT --max-pool-size 3 --nginx-config-template nginx.conf.erb