场景 : 第三方提供的接口不支持 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. 替换 “这里放你要转发的站点路径” 为你要转发的地址