我目前正在开发一个大小很重要的API:我希望答案包含尽可能少的字节.我优化了我的
JSON答案,但rails仍然响应了很多奇怪的标题
HTTP/1.1 200 OK Server: Nginx/0.7.67 # Not from Rails,so ok. Date: Wed,25 Apr 2012 20:17:21 GMT # Date does not matter. We use ETag Can I remove this? ETag: "678ff0c6074b9456832a710a3cab8e22" # Needed. Content-Type: application/json; charset=utf-8 # Also needed. Transfer-Encoding: chunked # The alternative would be Content-Length,so ok. Connection: keep-alive # Good,less TCP overhead. Status: 200 OK # Redundant! How can I remove this? X-UA-Compatible: IE=Edge,chrome=1 # Completely unneded. Cache-Control: no-cache # Not needed. X-Request-Id: c468ce87bb6969541c74f6ea761bce27 # Not a real header at all. X-Runtime: 0.001376 # Same goes for this X-Rack-Cache: invalidate,pass # And this.
所以有很多不必要的HTTP标头.我可以在我的服务器(Nginx)中过滤它们,但有没有办法直接在rails中停止它?
解决方法
您可以使用一个Rack中间件来完成此操作.有关在一个中执行此操作的示例,请参阅
https://gist.github.com/02c1cc8ce504033d61bf.
将其添加到您的应用配置时,使用类似config.middleware.insert_before(ActionDispatch :: Static,:: HeaderDelete)的内容
您希望在运行rake中间件时显示的列表中的第一项之前插入它,在我的例子中是ActionDispatch :: Static.
如果你之前没有在Rails上下文中暴露过Rack,那么http://guides.rubyonrails.org/rails_on_rack.html可能会有所帮助.