[英]Caching with Rails app and Chrome Browser
I expect the app should respond with code 304 instead of 200. But even IF-NONE-MATCH equals ETAG it doesn't happen. 我希望应用程序应使用代码304而不是200进行响应。但是即使IF-NONE-MATCH等于ETAG,也不会发生。
I use 'Cache-Control: no-cache' to not store a response in cache to be validated each time. 我使用“ Cache-Control:no-cache”将响应不存储在每次都要验证的缓存中。 Otherwise Chrome uses it's disc cache, which is unacceptable. 否则,Chrome会使用其光盘缓存,这是不可接受的。
Request: 请求:
GET /api/v4/record/11728 HTTP/1.1
Host: host.domain.com
Connection: keep-alive
Authorization: Basic YWRtaW467Uc2Zs0eTIwMTM=
Origin: https://host.domain.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Accept: application/json, text/plain, */*
DNT: 1
Referer: https://host-ui.domain.com/some_page
Accept-Encoding: gzip, deflate, sdch, br
If-None-Match: W/"39dcd8467e47701a69c617333f7b6dac"
If-Modified-Since: Thu, 13 Apr 2017 16:09:25 GMT
Name
Response: 响应:
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,PATCH,HEAD,OPTIONS
Access-Control-Allow-Origin: https://host-ui.domain.com
Cache-Control: no-cache
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Thu, 13 Apr 2017 16:20:31 GMT
ETag: W/"39dcd8467e47701a69c617333f7b6dac"
Last-Modified: Thu, 13 Apr 2017 16:09:25 GMT
Server: nginx/1.8.1 + Phusion Passenger 4.0.60
Status: 200 OK
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Powered-By: Phusion Passenger 4.0.60
X-Request-Id: ab87433e-62bd-437f-ad7c-0e1d3f95257b
X-Runtime: 0.209121
X-XSS-Protection: 1; mode=block
transfer-encoding: chunked
Connection: keep-alive
In the application common action looks like: 在应用程序中,常见操作如下所示:
def action
record = Model.find(params['id'])
if stale?(record)
hard_work_result = to_do_somethig
render json: {
success: 0,
result: hard_work_result
}
end
end
There seems to be a history behind this but short story: gzip is messing with Weak ETags 这似乎有一段历史,但故事很短:gzip与弱ETag混为一谈
Weak ETag: ETag: W/"8763458...
弱ETag: ETag: W/"8763458...
Strong ETag: ETag: "8763458...
强力ETag: ETag: "8763458...
You can test that this is the problem if you use cURL and disable gzip from Accept-Encoding
(or use modheaders in chrome: Disable gzip compression in chrome ) 如果您使用cURL并从Accept-Encoding
禁用gzip(或在chrome中使用modheaders:在chrome中禁用gzip压缩 ),则可以测试这是问题所在
Getting no 304 response in Chrome/Safari but via curl 在Chrome / Safari中无法通过卷曲获得304响应
https://masa331.github.io/2016/01/06/roda-etag-caching-gotcha.html https://masa331.github.io/2016/01/06/roda-etag-caching-gotcha.html
Weak ETAGs in Rails? Rails中的ETAG弱吗?
When running from nginx itself, the problem did not occur. 从nginx本身运行时,没有发生此问题。 The first link above suggests that if you are having the problem from within nginx then adding an etag on;
上面的第一个链接建议,如果您在nginx中遇到问题,请在上面添加一个etag on;
after the gzip on;
gzip on;
之后gzip on;
fixes the problem. 解决问题。 Specifically though weak etags were not returned. 特别是尽管没有返回弱的etags。 Running from within nginx would seem to be the only option if you want gzip enabled. 如果要启用gzip,从nginx内部运行似乎是唯一的选择。
my versions 我的版本
Server: nginx/1.10.2 + Phusion Passenger 5.1.2`)
ii ruby-rails 2:4.2.6-1
ii ruby2.3 2.3.1-2~16.04
Ive managed to work around the problem when running in passenger standalone by editing the nginx.conf.erb and disabling gzip. 我已经通过编辑nginx.conf.erb并禁用gzip来在乘客独立运行时设法解决了该问题。 To do this, you first need to get the template of the conf file: 为此,您首先需要获取conf文件的模板:
passenger start --debug-nginx-config
this will put a file called nginx.conf.erb in your current directory. 这将在当前目录中放置一个名为nginx.conf.erb的文件。 Then you can edit this file to say: 然后,您可以编辑此文件以说:
gzip off;
and then start passenger again with this file 然后用这个文件重新开始乘客
passenger start --nginx-config-template nginx.conf.erb
see here for details of downloading and using the nginx.conf.erb 有关下载和使用nginx.conf.erb的详细信息, 请参见此处
In either case you should be able to see that weak etags are not returned 无论哪种情况,您都应该能够看到未返回弱标签
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.