如何使用 IIS 作为反向代理

官方提供了可选模块 ARR:

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

可惜此模块未能支持中文,安装此模块前需要先安装:

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

且后续反向代理配置也主要在此模块中进行配置。

安装后在 IIS 管理器的根节点上可以看到此模块

 

进入此模块,在右侧找到 Server Proxy Settings... 进入:

在页面中可勾选 Enable Proxy,在右侧点击应用即完成启用,此页面还有一个 Reverse rewrite host in response headers 的选项,如果遇到问题可以考虑禁用此选项(未研究此选项的具体作用)

 

 另外还有一个配置在根节点的“配置管理器”中

 

 可以将 preserveHostHeader 设为 True,暂未知其作用

 

 

 然后进入需要配置的网站打开 URL 重写 模块,即可正常添加反向代理规则

以下使用 web.config 做一个简单示例,以下实现了 https 的自动跳转,以及将 cloudreve 路径指向 5212 端口的一个网站(然而 cloudreve 并不支持虚拟目录部署,这是一个失败的示例)

另外使用 IIS 作为反向代理的话,有一个请求筛选功能会在请求响应中生效,会导致一些文件类型被过滤(如 *.cs、*.csproj),请求大小被限制(默认 28.6MB,最大 4GB)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
    <rewrite>
            <rules>
                <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
                </rule>
                <rule name="Rerwite" stopProcessing="true">
                    <match url="cloudreve.*" />
                    <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="http://localhost:5212/{R:0}" />
                </rule>
            </rules>
        </rewrite>
        <security>
            <requestFiltering allowDoubleEscaping="true">
                <fileExtensions>
<!-- 移除所有文件扩展名筛选 -->
                    <clear />
                </fileExtensions>
<!-- 设置请求内容最大大小(字节),最大 4294967295(4GB) --> <requestLimits maxAllowedContentLength="60000000" /> </requestFiltering> </security> </system.webServer> </configuration>

参考:https://docs.cloudreve.org/getting-started/install#fan-xiang-dai-li 

posted @ 2022-07-23 08:38  不是豆豆  阅读(1462)  评论(0编辑  收藏  举报
友情链接:迷途