如何在 Windows Azure 动态内容上启用 gzip HTTP 压缩 [英] How to enable gzip HTTP compression on Windows Azure dynamic content

查看:37
本文介绍了如何在 Windows Azure 动态内容上启用 gzip HTTP 压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我的 Windows Azure 托管的 WCF Restful 服务上启用 gzip HTTP 压缩失败,该服务仅从 GET 和 POST 请求返回 JSON.

I've been trying unsuccessfully to enable gzip HTTP compression on my Windows Azure hosted WCF Restful service which returns JSON only from GET and POST requests.

我尝试了很多事情,我很难将所有这些都列出来,现在我意识到我一直在处理相互矛盾的信息(关于旧版本的 azure 等),所以认为最好从一个干净的石板开始!

I have tried so many things that I would have a hard time listing all of them, and I now realise I have been working with conflicting information (regarding old version of azure etc) so think it best to start with a clean slate!

我正在使用 Visual Studio 2008,使用适用于 Visual Studio 的 2010 年 2 月工具.

I am working with Visual Studio 2008, using the February 2010 tools for Visual Studio.

因此,根据以下link..

... HTTP 压缩现已启用.我使用了以下页面上的建议(仅限 URL 压缩建议).

.. HTTP compression has now been enabled. I've used the advice at the following page (the URL compression advice only)..

http://blog.smarx.com/posts/iis-在windows-azure中压缩

<urlCompression doStaticCompression="true" 
         doDynamicCompression="true"
         dynamicCompressionBeforeCache="true" 
/>

...但我没有压缩.我不知道 urlCompressionhttpCompression 之间有什么区别也无济于事.我试图找出但无济于事!

.. but I get no compression. It doesn't help that I don't know what the difference is between urlCompression and httpCompression. I've tried to find out but to no avail!

Visual Studio 的工具在支持压缩的 Azure 版本之前发布的事实会不会有问题?我在某处读到过,使用最新的工具,您可以在发布时选择要使用的 Azure OS 版本......但我不知道这是否属实,如果是,我找不到在哪里选择.我可以使用启用 http 前的版本吗?

Could, the fact that the tools for Visual Studio were released before the version of Azure which supports compression, be a problem? I have read somewhere that, with the latest tools, you can choose which version of Azure OS you want to use when you publish ... but I don't know if that's true, and if it is, I can't find where to choose. Could I be using a pre-http enabled version?

我也试过 Blowery http 压缩模块,但没有结果.

I've also tried blowery http compression module, but no results.

是否有人对如何实现这一目标有任何最新建议?即与 Azure 操作系统的当前版本相关的建议.

Does any one have any up-to-date advice on how to achieve this? i.e. advice that relates to the current version of the Azure OS.

干杯!

史蒂文

更新:我编辑了上面的代码以修复 web.config 片段中的类型.

Update: I edited the above code to fix a type in the web.config snippet.

更新 2: 使用下面答案中显示的 whatsmyip URL 测试响应表明我的 service.svc 的 JSON 响应返回时没有任何压缩,但返回的是静态 HTML 页面 使用 gzip 压缩返回.任何有关如何获取 JSON 响应以进行压缩的建议将不胜感激!

Update 2: Testing the responses using the whatsmyip URL shown in the answer below is showing that my JSON responses from my service.svc are being returned without any compression, but static HTML pages ARE being returned with gzip compression. Any advice on how to get the JSON responses to compress will be gratefully received!

更新 3: 尝试了一个大于 256KB 的 JSON 响应,以查看问题是否是由于 JSON 响应比下面的评论中提到的要小.不幸的是,响应仍未压缩.

Update 3: Tried a JSON response larger than 256KB to see if the problem was due to the JSON response being smaller than this as mentioned in comments below. Unfortunately the response is still un-compressed.

推荐答案

嗯,这花了很长时间 ......但我终于解决了这个问题,我想为任何人发布答案还有谁在挣扎.解决方案非常简单,我已经验证它确实有效!!

Well it took a very long time ... but I have finally solved this, and I want to post the answer for anyone else who is struggling. The solution is very simple and I've verified that it does definitely work!!

编辑您的 ServiceDefinition.csdef 文件以将其包含在 WebRole 标记中:

Edit your ServiceDefinition.csdef file to contain this in the WebRole tag:

    <Startup>
      <Task commandLine="EnableCompression.cmd" executionContext="elevated" taskType="simple"></Task>
    </Startup>

在您的网络角色中,创建一个文本文件并将其另存为EnableCompression.cmd"

In your web-role, create a text file and save it as "EnableCompression.cmd"

EnableCompression.cmd 应包含以下内容:

EnableCompression.cmd should contain this:

%windir%system32inetsrvappcmd set config /section:urlCompression /doDynamicCompression:True /commit:apphost
%windir%system32inetsrvappcmd set config  -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']" /commit:apphost

……就是这样!完毕!这可以为 web-role 返回的 json 启用动态压缩,我想我在某处读到的它具有相当奇怪的 mime 类型,因此请确保准确复制代码.

.. and that's it! Done! This enables dynamic compression for the json returned by the web-role, which I think I read somewhere has a rather odd mime type, so make sure you copy the code exactly.

这篇关于如何在 Windows Azure 动态内容上启用 gzip HTTP 压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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