레이블이 IIS인 게시물을 표시합니다. 모든 게시물 표시
레이블이 IIS인 게시물을 표시합니다. 모든 게시물 표시

2021년 11월 11일 목요일

이미지나 동영상같은 리소스 파일을 특정 도메인에서만 불러쓸 수 있게 설정했습니다. (Windows Server 2016,IIS 10의 URL Rewrite이용)

 


갑자기 파일서버의 네크워크 트래픽이 크게 증가 할 때가 있습니다.

분석을 해보면 운영 중인 서비스 사이트에서의 참조가 아닌 외부에서의 참조(img,video태그의 URL)가 원인으로 판명되는 경우가 다발합니다. 출처를 밝힌 참조라면 홍보에 도움이 되니 감당하지만 그렇지 않은 무단(출처 명기가 없으므로)참조의 경우는 제한을 해야합니다.

(네크워크의 계약된 대역폭을 넘게 되면 추가 요금이 발생합니다.)


특정 도메인에서만 참조하게 하려면 리퍼러(REFERER)를 체크하면 되는데 이미지나 동영상같은 리소스 파일에서는 불가능합니다.

웹어플리케이션에서 리소스파일을 읽고 리퍼러(REFERER)를 체크해서 스트림으로 출력하는 방법도 있지만 병목현상을 일으키기 딱 좋기 때문에 다수의 유저가 이용하는 서비스에는 맞지 않습니다. 


IIS의 확장모듈인  "URL Rewrite(URL 재작성)"을 이용했습니다.


Match URL(대상)
Requested URLMatches the Pattern
UsingRegular Expressions
Pattern(.*)
Ignore caseCheck
대소 구분없이 모든 URL을 대상으로 함

리소스파일만 있는 파일서버이므로 모든 URL을 대상으로 했습니다.

필요한 경우 특정확장자에만 적용하도록 패턴(Pattern)을 작성해도 되겠습니다.

Conditions(상태)
Condition Input{HTTP_REFERER}
Check if input stringDoes Not Match the Pattern
Pattern^http(s)?://(www\.)?sample.com
Ignore caseCheck
HTTP_REFERER가 sample.com이 아니라면

sample2.com, sample3.com도 허가하려면 같은 방식으로 추가하고 Logical grouping을 Match All로 설정하면 됩니다.

만약 반대로 sample.com, sample2.com, sample3.com에서의 참조만 제한하려면

Logical grouping을 Match Any로 하고 각각의 Condition의 Check if input string를 Match the Pattern으로 하면 되겠습니다.

Action(처리)
Action typeAbort Request
요청에 대한 처리를 중지


물론 위의 설정은 web.config 파일을 직접 편집으로도 가능합니다.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
  <rewrite>
   <rules>
    <rule name="RefererForAccess" enabled="true" stopProcessing="true">
     <match url="(.*)" />
     <conditions>
      <add input="{HTTP_REFERER}" pattern="^http(s)?://(www\.)?sample.com" negate="true" />
      <add input="{HTTP_REFERER}" pattern="^http(s)?://(www\.)?sample2.com" negate="true" />
      <add input="{HTTP_REFERER}" pattern="^http(s)?://(www\.)?sample3.com" negate="true" />
     </conditions>
     <action type="AbortRequest" />
    </rule>
   </rules>
  </rewrite>
 </system.webServer>
</configuration>


HTTP의 엑세스를 HTTPS로 리다이렉트 시켰습니다.(Windows Server 2016,IIS 10의 URL Rewrite이용)

 


설정할 때마다 매번 찾아봤던 내용이라 정리를 해둡니다.


IIS의 확장모듈 "URL Rewrite(URL 재작성)"을 이용했습니다.

IIS의 기본 모듈이 아니라서 추가로 설치해야 하는데,

웹 플랫폼 인스톨러를 이용해서 설치하거나 

https://www.microsoft.com/web/downloads/platform.aspx


Web Platform Installer : The Official Microsoft IIS Site

HomeDownloadsMicrosoft Supported DownloadsWeb Platform Installer Install this extension or view additional downloads  OverviewThe Microsoft Web Platform Installer - WebPI provides a simplified installation workflow for installing common open source web ap

www.microsoft.com

설치 파일을 직접 다운로드해서 설치할 수 있습니다.

https://www.iis.net/downloads/microsoft/url-rewrite


URL Rewrite : The Official Microsoft IIS Site

Install this extension or view additional downloads  OverviewIIS URL Rewrite 2.1 enables Web administrators to create powerful rules to implement URLs that are easier for users to remember and easier for search engines to find. By using rule templates, re

www.iis.net


"URL Rewrite(URL 재작성)"은 IIS 7.0 버전 이후로 지원됩니다.

"URL Rewrite(URL 재작성)"을 이용하지 않고도 기본 리다이렉트기능을 이용해서 고정된 URL로 이동시키는 것은 가능합니다.

HTTP의 엑세스를 HTTPS로 이동시키는 것도 웹사이트 2개를 만들어 도메인의 HTTP(80)와 HTTPS(443)를 각각 설정하여  HTTP 사이트에 들어오는 모든 엑세스를 기본 리다이렉트를 이용해 도메인의 HTTPS 사이트로 보내는 방식입니다.

하지만 전체 URI 및 파라미터를 유지한 상태로 이동시킨다던지의 상세 처리를 지원하지 않아 결국은 웹어플리케이션 내에서 처리(개발)했었습니다.


기본 리다이렉트기능을 이용: 고정된 URL로 이동시키는 것은 가능

http://sample.com
http://sample.com/sample.asp
http://sample.com/sample.asp?p=1
→
https://sample.com


"URL Rewrite(URL 재작성)" 이용: 각각의 URI와 파라메터를 유지하면서 이동시키는 것 같은 상세 설정도 가능

http://sample.com                                        → https://sample.com    
http://sample.com/sample.asp                   → https://sample.com/sample.asp  
http://sample.com/sample.asp?p=1           → https://sample.com/sample.asp?p=1 


"URL Rewrite(URL 재작성)"에 2가지의 룰을 추가해서 HTTP의 엑세스를 HTTPS로 보낼 수 있었습니다.

1. HTTPS redirect : 리다이렉트 처리
2. Remove double slash : https://sample.com// URL끝에 슬래쉬가 두개 들어가는 문제 해결 
Rmove로 이름을 잘못 입력한 상태로 캡쳐를 해버렸습니다... 뭐 중요한건 아니므로..


1. HTTPS redirect : 리다이렉트 처리


Match URL(대상)
Requested URLMatches the Pattern
UsingRegular Expressions
Pattern(.*)
Ignore caseCheck
대소 구분없이 모든 URL을 대상으로 함
Conditions(상태)
Condition Input{HTTPS}
Check if input stringMatches the Pattern
Pattern^OFF$
Ignore caseCheck
Server Variables의 HTTPS가 OFF상태인지 확인
Action(처리)
Action typeRedirect
Redirect URLhttps://{HTTP_HOST}/{REQUEST_URI}
Append query stringUncheck
Redirect typePermanent (301)
{HTTP_HOST}/{REQUEST_URI}를 그대로 유지하며 https로 301리다이렉트


리다이렉션
301 ( Moved Permanently )새로운 URL로 영구적으로 변경되었음을 알림
클라이언트에 새로운 URL을 사용하도록 알림
302 ( Found )일시적으로 다른 URL로 전환함을 알림
클라이언트는 기존 URL을 그대로 유지
크롤러가 색인화를 하지 않을 가능성이 있음



2. Remove double slash : https://sample.com// URL끝에 슬래쉬가 두개 들어가는 문제 해결 


Match URL(대상)
Requested URLMatches the Pattern
UsingRegular Expressions
Pattern(.*)
Ignore caseCheck
대소 구분없이 모든 URL을 대상으로 함
Conditions(상태)
Condition Input{UNENCODED_URL}
Check if input stringMatches the Pattern
Pattern(.*)//(.*)
Ignore caseCheck
원본 URL에 /가 두개인지 확인
Action(처리)
Action typeRedirect
Redirect URL{C:1}/{C:2}
Append query stringUncheck
Redirect typePermanent (301)
원본 URL의 //(슬래쉬가 두개)를 기준으로 앞{C:1}, 뒤{C:2}로 잘라 /(슬래쉬1개)로 재작성한 URL로 301리다이렉트


물론 위의 설정은 web.config 파일을 직접 편집으로도 가능합니다.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
  <rewrite>
   <rules>
    <rule name="HTTPS redirect" stopProcessing="true">
     <match url="(.*)" />
     <conditions>
      <add input="{HTTPS}" pattern="^OFF$" />
     </conditions>
     <action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" appendQueryString="false" />
    </rule>
    <rule name="Rmove double slash" stopProcessing="true">
     <match url="(.*)" />
     <conditions>
      <add input="{UNENCODED_URL}" pattern="(.*)//(.*)" />
     </conditions>
     <action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
    </rule>
   </rules>
  </rewrite>
 </system.webServer>
</configuration>


정규식(Regular Expressions)과 mod_rewrite의 문법은 매번 고생을 합니다만 다행히도 이번은 간단한 설정이었습니다.




1、2、QWER! in OSAKA (大阪 2025/4/10(木) Yogibo META VALLEY)

https://www.creativeman.co.jp/event/qwer-osaka/ QWER - CREATIVEMAN PRODUCTIONS 主催:LIVET 企画:ソニー・ミュージックソリューションズ 招聘:クリエイティブマンプロダクション 制作協力:クリエイテ...