Rails何时响应“传输编码"和“内容长度"? [英] When does Rails respond with 'transfer-encoding' vs. 'content-length'?

查看:91
本文介绍了Rails何时响应“传输编码"和“内容长度"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Rails版本4.1.7/Nginx上构建API,以响应来自iOS应用程序的请求.我们在客户端上看到了一些奇怪的缓存,我们认为这与Rails发回的响应中的微小差异有关.我的问题...

I'm building an API on Rails version 4.1.7/Nginx that responds to request from an iOS app. We're seeing some weird caching on the client and we think it has something to do with a small difference in the response that Rails is sending back. My questions...

1)我想了解为什么,对于完全相同的请求(仅更改了Authorization标头值),Rails有时会发回transfer-encoding: chunkedContent-Length: <number>?我认为这可能与响应大小有关,但是在示例响应中,我在下面粘贴了标题的示例中,正文中返回的数据完全相同.

1) I want to understand why, for the exact same request (with only the Authorization header value changed), Rails sends back transfer-encoding: chunked sometimes and Content-Length: <number> sometimes? I thought that maybe it had something to do with the response size, but in the example responses whose headers I've pasted below, the data returned in the body is EXACTLY the same.

2)有没有办法强迫它使用Content-Length?我们认为这将解决我们iOS应用程序中的缓存问题.

2) Is there a way to force it to use Content-Length? We think that that will fix our caching issues in our iOS app.

响应#1

HTTP/1.1 200 OK
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/json; charset=utf-8
Date: Wed, 18 Mar 2015 00:59:31 GMT
ETag: "86f277ea63295460d4f3bed9a073eaa2"
Server: nginx/1.6.2
Status: 200 OK
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: dd36f139-1986-4da6-9645-4438d41e74b0
X-Runtime: 0.123865
X-XSS-Protection: 1; mode=block
transfer-encoding: chunked
Connection: keep-alive

请求#2

HTTP/1.1 200 OK
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/json; charset=utf-8
Date: Wed, 18 Mar 2015 00:59:36 GMT
ETag: "86f277ea63295460d4f3bed9a073eaa2"
Server: nginx/1.6.2
Status: 200 OK
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: 0cfd7705-157b-41b5-aa36-739bc6f8302e
X-Runtime: 0.092672
X-XSS-Protection: 1; mode=block
Content-Length: 2234
Connection: keep-alive

推荐答案

根据HTTP 1.1,这两个响应均有效,因此您需要修复可同时处理这两个客户端代码.尝试修复服务器,使其表现为不会触发客户端中的错误,这是一个坏主意. 下一代Nginx的行为可能会有所不同,您的用户甚至可能具有更改传输的代理,可能仅在他们漫游并使用其他提供程序时才会发生.

Both responses are valid according to HTTP 1.1, so you need to fix your client code that it can handle both. It is a bad idea to try to fix the server so that that it behave in a way that it does not trigger a bug in the client. The next version of nginx may behave differently, you users may even have proxies that change the transfer, maybe only when they do roaming and use a different provider.

如果您想在标题上进行一些指纹识别,ETag标题可能会为您提供帮助,因为无论响应如何,响应内容都不会更改时,ETag应该保持不变.

If you want to do some finger-printing on the header, the ETag-header may help you, as the ETag should stay constant when the content of the response is not changed, regardless of the transfer.

服务器通常在调用动态页面时分块发送,因为这样一来,服务器就不需要为整个页面创建缓冲区,而不必等到所有页面都生成为止.

The server typically sends in chunks when it calls a dynamic page, because it then does not need to create a buffer for the whole page and wait till all of the page is generated.

如果服务器已经具有缓冲区,则通常会一口气发送响应,例如,因为它在高速缓存中或内容在文件中并且不大.一次性发送效率更高,另一方面,额外的数据副本以缓冲输出需要更多的内存,效率也较低.因此服务器甚至可以根据可用内存来决定.

The server often send the response in one go if it has the buffer already, for example because it is in cache or the content is on a file and is not to big. Sending in one go is more efficient, on the other hand, an extra copy of the data to buffer the output needs more memory and is less efficient. So the server may even decide this according to the available memory.

这篇关于Rails何时响应“传输编码"和“内容长度"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆