• IIS 反向代理 支持 CORS 跨域


    场景 :  第三方提供的接口不支持 CORS 的情况

    之前用的nginx来解决这个问题,其实 IIS 也可以来玩玩

    1.先决条件,安装以下两个IIS Module

      1.1  Application Request Routing

            

            https://www.iis.net/downloads/microsoft/application-request-routing

      1.2  URL Rewrite

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

            安装完成会多一个这个

            

    2. 添加服务器变量,以支持负载均衡场景

         

       依次添加下面的服务器变量

         HTTP_THE_HOST
         HTTP_THE_SCHEME
         HTTP_X_FORWARDED_HOST
         HTTP_X_FORWARDED_PROTO

    3. 创建 web.config 文件, 放入 IIS 站点根目录

       

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="OPTIONS" patternSyntax="Wildcard" stopProcessing="true">
              <match url="*" />
              <conditions logicalGrouping="MatchAny">
                <add input="{REQUEST_METHOD}" pattern="OPTIONS" />
              </conditions>
              <action type="CustomResponse" statusCode="204" subStatusCode="0" statusReason="" statusDescription="" />
            </rule>
            <rule name="INIT_SERVER_VARIABLE_FROM_PROXY" stopProcessing="false">
              <match url=".*" />
              <serverVariables>
                <set name="HTTP_THE_SCHEME" value="{HTTP_X_FORWARDED_PROTO}" replace="true" />
                <set name="HTTP_THE_HOST" value="{HTTP_X_FORWARDED_HOST}" replace="true" />
              </serverVariables>
              <action type="None" />
            </rule>
            <rule name="INIT_SERVER_VARIABLE_DEFAULT" stopProcessing="false">
              <match url=".*" />
              <conditions trackAllCaptures="true">
                <add input="{HTTPS}s" pattern="on(s)|offs" />
              </conditions>
              <serverVariables>
                <set name="HTTP_THE_SCHEME" value="http{C:1}" replace="false" />
                <set name="HTTP_THE_HOST" value="{HTTP_HOST}" replace="false" />
              </serverVariables>
              <action type="None" />
            </rule>
            <rule name="DocumentServerRewrite" enabled="true">
              <match url="^(.*)" />
              <conditions trackAllCaptures="true">
                <add input="{HTTPS}s" pattern="on(s)|offs" />
              </conditions>
              <serverVariables>
                <set name="HTTP_X_FORWARDED_PROTO" value="{HTTP_THE_SCHEME}" />
                <set name="HTTP_X_FORWARDED_HOST" value="{HTTP_THE_HOST}/documentserver-virtual-path" />
              </serverVariables>
              <action type="Rewrite" url="https://这里放你要转发的站点路径/{R:1}" logRewrittenUrl="false" />
            </rule>
    
          </rules>
          <outboundRules>
            <clear />
            <rule name="Set Access_Control_Allow_Origin for OPTIONS response">
              <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
              <action type="Rewrite" value="{HTTP_ORIGIN}" />
            </rule>
            <rule name="Set Access-Control-Allow-Methods for OPTIONS response" patternSyntax="Wildcard">
              <match serverVariable="RESPONSE_Access-Control-Allow-Methods" pattern="*" negate="false" />
              <action type="Rewrite" value="{HTTP_Access-Control-Request-Method}" />
            </rule>
            <rule name="Set Access-Control-Allow-Headers for OPTIONS response" patternSyntax="Wildcard">
              <match serverVariable="RESPONSE_Access-Control-Allow-Headers" pattern="*" negate="false" />
              <action type="Rewrite" value="{HTTP_Access-Control-Request-Headers}" />
            </rule>
            <rule name="Set Access-Control-Allow-Credentials for OPTIONS response" patternSyntax="Wildcard">
              <match serverVariable="RESPONSE_Access-Control-Allow-Credentials" pattern="*" negate="false" />
              <action type="Rewrite" value="true" />
            </rule>
            <rule name="Set Access-Control-Max-Age for OPTIONS response" patternSyntax="Wildcard">
              <match serverVariable="RESPONSE_Access-Control-Max-Age" pattern="*" negate="false" />
              <action type="Rewrite" value="3600" />
            </rule>
    
    
          </outboundRules>
    
        </rewrite>
      </system.webServer>
    </configuration>
    4. 替换 “这里放你要转发的站点路径” 为你要转发的地址

      

  • 相关阅读:
    sass的安装
    git上传项目到github教程
    v-if 和v-show的区别
    es5实现数组去重
    原生js实现选中所有的checkbox
    拨打手机号
    H5页面打开小程序
    h5 网页 直接唤起淘宝app,并跳转到对应商品页面
    webstorm配置git
    elementUi 日历添加可选区间(只能选择一个月的时间段)
  • 原文地址:https://www.cnblogs.com/gaocong/p/13267065.html
Copyright © 2020-2023  润新知