【问题标题】:Rails + Passenger on Heroku: How to set expiry date or a maximum age in the HTTP headers for static resources?Heroku 上的 Rails + Passenger:如何在静态资源的 HTTP 标头中设置到期日期或最长期限?
【发布时间】:2016-04-19 09:49:41
【问题描述】:

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

由于静态资产由乘客/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

【问题讨论】:

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


    【解决方案1】:

    我可以这样解决:

    创建nginx.conf.erb 文件:

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

    nginx.conf.erbserver 块内,指示 Nginx 在我们的资产目录下的文件被请求时生成适当的标头:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2013-12-06
      • 2015-11-08
      • 2010-10-26
      • 2013-04-12
      • 1970-01-01
      相关资源
      最近更新 更多