Heroku上的Rails + Passenger:如何在静态资源的HTTP头中设置到期日期或最大年龄?

时间:2016-01-14 06:51:35

标签: ruby-on-rails caching heroku nginx passenger

我正在使用带有angularjs前端的Rails api,它只是作为public目录下的静态文件提供。我选择乘客作为app服务器,部署到heroku,除了缓存外,一切似乎都运行正常。

由于静态资产由passenger / nginx提供,我相信这与rails无关。但我不知道如何使其工作或在何处添加配置。

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

1 个答案:

答案 0 :(得分:4)

我能够像这样解决它:

创建nginx.conf.erb文件:

select unix_timestamp(received_at) from user_data2 limit 5;
OK
NULL
NULL
NULL
NULL
NULL

nginx.conf.erb 中的cp $(passenger-config about resourcesdir)/templates/standalone/config.erb nginx.conf.erb 块内,指示Nginx在请求我们的资产目录下的文件时生成适当的标头:

server

Nginx engine options传递给Procfile中的乘客:

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