HTTP_IF_MODIFIED_SINCE header not passed to Rails

2019-06-05 12:29发布

问题:

I am trying to use the HTTP_IF_MODIFIED_SINCE header in my app to determine if resources are stale/fresh and render 200/304 in those cases. In my dev environment everything works fine but I can't for the life of me get it to work in production.

I am using Passenger 3.0.11 and Nginx 1.0.13.

As you see below, I tried proxy_pass_header, proxy_set_header, passenger_pass_header and passenger_set_cgi_param. The last one actually sets a HTTP_IF_MODIFIED_SINCE header but it is empty... Any help/ideas would be greatly appreciated!

My config:

server {
  listen             80 default_server;
  root               /home/rails/myapp/current/public;
  passenger_enabled  on;
  charset            utf-8;

  proxy_pass_header If-Modified-Since;
  proxy_set_header If-Modified-Since $http_if_modified_since;
  passenger_pass_header If-Modified-Since;
  passenger_set_cgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;

  if (-f $document_root/system/maintenance.html) {
    rewrite ^(.*)$ /system/maintenance.html break;
  }

  location ~ \.(aspx|jsp|cgi)$ {
    return 410;
  }

  location ~ ^/(assets)/ {
    # http://guides.rubyonrails.org/asset_pipeline.html#server-configuration
    # gzip_static on;
    expires     1y;
    add_header  Cache-Control public;

    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
}

回答1:

to get it working with non-standard headers, containing underscores, do this inside your http or server block in the nginx.conf file:

underscores_in_headers on;
ignore_invalid_headers off;

and in the server block:

proxy_pass_header HTTP_IF_MODIFIED_SINCE

This can be useful if you have legacy HTTP headers which you need to deal with, and which contain underscores.



回答2:

This was a user error after all. I sent the header to the app in the wrong format (IF_MODIFIED_SINCE instead of If-Modified-Since). After fixing that, it worked without any of the extra directives.