Programing

Windows Azure 동적 콘텐츠에서 gzip HTTP 압축을 활성화하는 방법

lottogame 2020. 12. 25. 08:49
반응형

Windows Azure 동적 콘텐츠에서 gzip HTTP 압축을 활성화하는 방법


GET 및 POST 요청에서만 JSON을 반환하는 Windows Azure 호스팅 WCF Restful 서비스에서 gzip HTTP 압축을 사용하도록 설정하는 데 실패했습니다.

나는 너무 많은 것을 시도하여 그것들을 모두 나열하기가 어려울 것이며, 이제 충돌 정보 (예전 버전의 azure 등)로 작업하고 있음을 깨달았으므로 깨끗한 슬레이트로 시작하는 것이 가장 좋습니다!

저는 Visual Studio 용 2010 년 2 월 도구를 사용하여 Visual Studio 2008로 작업하고 있습니다.

따라서 다음 링크 에 따르면 ..

.. HTTP 압축이 활성화되었습니다. 다음 페이지의 조언을 사용했습니다 (URL 압축 조언 만 해당) ..

http://blog.smarx.com/posts/iis-compression-in-windows-azure

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

..하지만 압축이 안 돼요. urlCompressionhttpCompression 의 차이점을 모르는 것은 도움이되지 않습니다 . 알아 내려고했지만 아무 소용이 없습니다!

압축을 지원하는 Azure 버전 이전에 Visual Studio 용 도구가 출시되었다는 사실이 문제일까요? 최신 도구를 사용하면 게시 할 때 사용할 Azure OS 버전을 선택할 수 있다는 내용을 읽었습니다 ...하지만 그게 사실인지 모르겠습니다. 그렇다면 어디에서 찾을 수 없습니다. 선택합니다. http 이전 버전을 사용할 수 있습니까?

나는 또한 blowery http 압축 모듈을 시도했지만 결과가 없습니다.

이를 달성하는 방법에 대한 최신 조언이있는 사람이 있습니까? 즉, 현재 버전의 Azure OS와 관련된 조언입니다.

건배!

스티븐

업데이트 : web.config 스 니펫의 유형을 수정하기 위해 위 코드를 편집했습니다.

업데이트 2 : 아래 답변에 표시된 whatsmyip URL을 사용하여 응답을 테스트하면 내 service.svc의 JSON 응답이 압축없이 반환되지만 정적 HTML 페이지 gzip 압축으로 반환됩니다. 압축 할 JSON 응답을 얻는 방법에 대한 모든 조언은 감사하게받을 것입니다!

업데이트 3 : 아래 주석에 언급 된대로 JSON 응답이 이보다 작기 때문에 문제가 발생했는지 확인하기 위해 256KB보다 큰 JSON 응답을 시도했습니다. 불행히도 응답은 여전히 ​​압축되지 않았습니다.


그런데 그것은했다 매우 오랜 시간이 ...하지만 결국이 문제를 해결 한, 나는 고투하고 다른 사람에 대한 대답을 게시 할. 해결책은 매우 간단하며 확실히 작동하는지 확인했습니다 !!

WebRole 태그에이를 포함하도록 ServiceDefinition.csdef 파일을 편집하십시오.

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

웹 역할에서 텍스트 파일을 만들고 "EnableCompression.cmd"로 저장합니다.

EnableCompression.cmd는 다음을 포함해야합니다.

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

.. 그리고 그게 다야! 끝난! 이것은 웹 역할에 의해 반환 된 json에 대한 동적 압축을 가능하게합니다. 제가 어딘가에서 읽은 것은 다소 이상한 MIME 유형이 있으므로 코드를 정확하게 복사하십시오.


적어도 저는 이것에 대해 혼자가 아닙니다. 거의 1 년이 지난 지금도 여전히 어리석은 PITA입니다.

문제는 MIME 유형 불일치입니다. WCF는 Content-Type: application/json; charset=UTF-8. 해당 페이지의 중간 쯤에 있는 기본 IIS 구성 에는 압축 가능한 MIME 유형으로 포함되지 않습니다.

이제 <httpCompression>web.config에 섹션을 추가하고 여기에 application / json을 추가하고 싶을 수 있습니다. 그러나 그것은 좋은 시간을 낭비하는 나쁜 방법 일뿐 <httpCompression>입니다. applicationHost.config 수준 에서만 요소를 변경할 수 있습니다 .

그래서 두 가지 가능한 해결책이 있습니다. 먼저 기본 구성에서 압축 가능한 MIME 유형을 사용하도록 WCF 응답을 변경할 수 있습니다. text/json작동하므로 이것을 서비스 메소드에 추가하면 동적 압축이 제공됩니다.WebOperationContext.Current.OutgoingResponse.ContentType = "text/json";

또는 appcmd 및 시작 작업을 사용하여 applicationHost.config 파일을 변경할 수 있습니다. 이것은 (다른 것들 중에서) 이 스레드 에서 논의 됩니다. 해당 시작 작업을 추가하고 dev 패브릭에서 실행하면 한 번 작동합니다. 두 번째는 이미 구성 요소를 추가했기 때문에 실패합니다. 별도의 csdef 파일로 두 번째 클라우드 프로젝트를 생성하여 devfabric이 해당 시작 스크립트를 실행하지 않도록했습니다. 그래도 다른 해결책이있을 수 있습니다.

최신 정보

이전 단락에서 개별 프로젝트에 대한 제 제안은 실제로 좋은 생각이 아닙니다. 비멱 등성 시작 작업은 매우 나쁜 생각입니다. 언젠가 Azure 패브릭이 역할을 다시 시작하기로 결정하고 시작 작업이 실패하고 재활용 루프에 들어가기 때문입니다. 한밤중 일 가능성이 높습니다. 대신 이 SO 스레드 에서 논의한대로 시작 작업을 멱등 적으로 만드십시오 .


처음 배포 한 후 문제가있는 로컬 개발 패브릭을 처리하기 위해 CMD 파일에 적절한 명령을 추가하여 구성을 재설정했습니다. 또한 일부 (모든?) 경우에 기본적으로 0으로 표시되기 때문에 여기서는 압축 수준을 구체적으로 설정하고 있습니다.

REM Remove old settings - keeps local deploys working (since you get errors otherwise)
%windir%\system32\inetsrv\appcmd reset config -section:urlCompression
%windir%\system32\inetsrv\appcmd reset config -section:system.webServer/httpCompression 

REM urlCompression - is this needed?
%windir%\system32\inetsrv\appcmd set config -section:urlCompression /doDynamicCompression:True /commit:apphost
REM Enable json mime type
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']" /commit:apphost

REM IIS Defaults
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='text/*',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='message/*',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/x-javascript',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='*/*',enabled='False']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='text/*',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='message/*',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/javascript',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='*/*',enabled='False']" /commit:apphost

REM Set dynamic compression level to appropriate level.  Note gzip will already be present because of reset above, but compression level will be zero after reset.
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"[name='deflate',doStaticCompression='True',doDynamicCompression='True',dynamicCompressionLevel='7',dll='%%Windir%%\system32\inetsrv\gzip.dll']" /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression -[name='gzip'].dynamicCompressionLevel:7 /commit:apphost

MS의이 기사는 JSON http://msdn.microsoft.com/en-us/library/windowsazure/hh974418.aspx 스크립트 방법 입니다.

It deals with many of the issues mentioned e.g. being able to handle Azure recycle etc


Just had an issue with this regarding the error type 183, and I found a solution. So if anybody else is experiencing this here goes:

Here's the error I got:

User program "F:\approot\bin\EnableCompression.cmd" exited with non-zero exit code 183. Working Directory is F:\approot\bin.

And here's the code that fixed it for me:

REM   *** Add a compression section to the Web.config file. ***
%windir%\system32\inetsrv\appcmd set config /section:urlCompression /doDynamicCompression:True /commit:apphost >> "%TEMP%\StartupLog.txt" 2>&1

REM   ERRORLEVEL 183 occurs when trying to add a section that already exists. This error is expected if this
REM   batch file were executed twice. This can occur and must be accounted for in a Windows Azure startup
REM   task. To handle this situation, set the ERRORLEVEL to zero by using the Verify command. The Verify
REM   command will safely set the ERRORLEVEL to zero.
IF %ERRORLEVEL% EQU 183 DO VERIFY > NUL

REM   If the ERRORLEVEL is not zero at this point, some other error occurred.
IF %ERRORLEVEL% NEQ 0 (
   ECHO Error adding a compression section to the Web.config file. >> "%TEMP%\StartupLog.txt" 2>&1
   GOTO ErrorExit
)

REM   *** Add compression for json. ***
%windir%\system32\inetsrv\appcmd set config  -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']" /commit:apphost >> "%TEMP%\StartupLog.txt" 2>&1
IF %ERRORLEVEL% EQU 183 VERIFY > NUL
IF %ERRORLEVEL% NEQ 0 (
   ECHO Error adding the JSON compression type to the Web.config file. >> "%TEMP%\StartupLog.txt" 2>&1
   GOTO ErrorExit
)

REM   *** Exit batch file. ***
EXIT /b 0

REM   *** Log error and exit ***
:ErrorExit
REM   Report the date, time, and ERRORLEVEL of the error.
DATE /T >> "%TEMP%\StartupLog.txt" 2>&1
TIME /T >> "%TEMP%\StartupLog.txt" 2>&1
ECHO An error occurred during startup. ERRORLEVEL = %ERRORLEVEL% >> "%TEMP%\StartupLog.txt" 2>&1
EXIT %ERRORLEVEL%

Solution found at http://msdn.microsoft.com/en-us/library/azure/hh974418.aspx


Yes, you can choose the OS you want, but by default, you'll get the latest.

Compression is tricky. There are lots of things that can go wrong. Are you by chance doing this testing behind a proxy server? I believe IIS by default doesn't send compressed content to proxies. I found a handy tool to test whether compression is working when I was playing with this: http://www.whatsmyip.org/http_compression/.

It looks like you have doDynamicCompression="false"... is that just a typo? You want that to be on if you're going to get compression on JSON you return from a web service.

ReferenceURL : https://stackoverflow.com/questions/2775261/how-to-enable-gzip-http-compression-on-windows-azure-dynamic-content

반응형