【问题标题】:IIS URL Rewrite ~ Rewriting Paths to PortsIIS URL 重写 ~ 重写端口路径
【发布时间】:2018-09-06 16:03:53
【问题描述】:

我的情况如下:

我们在单个服务器上拥有一系列 CI 服务,例如 TeamCity、YouTrack 和 OctopusDeploy。目前,我们通过 DNS 名称加端口访问所有这些,例如:

TeamCity:      http://server.company.com
YouTrack:      http://server.company.com:1234
OctopusDeploy: http://server.company.com:5678/octopus

我目前正在调整它,以便我们可以通过 https 访问这些服务。为此,我通过使用 IIS URL 重写来处理 SSL 身份验证,将 IIS 服务器设置为反向代理。这已经有效,我现在可以通过 https://server.company.com 访问 TeamCity

但是,在我做这件事的同时,我还想使用 IIS URL Rewrite 来美化地址,并摆脱记住端口号的需要。我想要实现的是以下重定向:

https://server.company.com          -> http://server.company.com (already working)
https://server.company.com/youtrack -> http://server.company.com:1234
https://server.company.com/octopus  -> http://server.company.com:5678/octopus

我已经尝试添加以下规则:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="ReverseProxyInboundRule2" stopProcessing="true">
                    <match url="^(https://server.company.com/youtrack)(.*)$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="http://server.company.com:1234/{R:2}" />
                </rule>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
                        <add input="{PATH}" pattern="youtrack" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="http://server.company.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

...并在 IIS 管理器中对其进行了测试,它正确识别规则和条件,并返回正确的反向规则。

但是,当我尝试访问 https://server.company.com/youtrack 时,我被重定向到 http://server.company.com/youtrack 而不是我想要的 http://server.company.com:1234。这就像 ReverseProxyInboundRule2 根本没有评估。

我确信对此有一个合乎逻辑的解释,并有一种方法可以使它发挥作用。我一个人看不到。我可以做些什么来完成这项工作?


2018 年 3 月 30 日更新:

好的,所以我想出了一个部分有效的配置。如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="Reverse Proxy to TeamCity" stopProcessing="true">
                    <match url="^teamcity/(.*)" />
                    <action type="Rewrite" url="http://server.company.com/{R:1}" />
                </rule>
                <rule name="Reverse Proxy to YouTrack" stopProcessing="true">
                    <match url="^youtrack/(.*)" />
                    <action type="Rewrite" url="http://server.company.com:1234/issues/{R:1}" />
                </rule>
                <rule name="Reverse Proxy to Hub" stopProcessing="true">
                    <match url="^hub/(.*)" />
                    <action type="Rewrite" url="http://server.company.com.de:5678/hub/{R:1}" />
                </rule>
                <rule name="Reverse Proxy to UpSource" stopProcessing="true">
                    <match url="^upsource/(.*)" />
                    <action type="Rewrite" url="http://server.company.com.de:9876/{R:1}" />
                </rule>
                <rule name="Reverse Proxy to Octopus" stopProcessing="true">
                    <match url="^octopus/(.*)" />
                    <action type="Rewrite" url="http://server.company.com:5432/octopus/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

此配置将请求重定向如下:

https://server.company.com          -> http://server.company.com
https://server.company.com/youtrack -> http://server.company.com:1234/issues/
https://server.company.com/hub      -> http://server.company.com:5678/hub/
https://server.company.com/upsource -> http://server.company.com:9876
https://server.company.com/octopus  -> http://server.company.com:5432/octopus/

这非常适合八达通。

在 Hub 上,地址旁边有一个“连接不安全”通知,说“网站的某些部分(例如图像)不安全”。进入页面信息,它在“技术细节”下的“安全”选项卡中表示,连接只是部分加密的。但是,页面的其余部分似乎工作正常。

TeamCity 和 YouTrack 更异想天开。在 Chrome 上,TeamCity 可以正常工作(一段时间后,它会随机断开连接),但 YouTrack 会出现 404 错误,而在 Firefox 上,TeamCity 和 YouTrack 都以无法使用的“纯文本”形式显示。

与此同时,UpSource 在任一浏览器上都提供一个空白页面。

我已经在各种浏览器和机器上对此进行了交叉测试,得出的结论是,如果我已经在这些服务器上登录到这些服务的 http 版本,TeamCity、YouTrack 和 Hub 只能“有点”工作。如果我没有登录,那么我会收到 TeamCity 和 YouTrack 的 404 错误。至于 Hub,我在尝试使用“不支持 POST 方法”消息登录时收到 405 错误。

所以我上面写的配置的基本结果如下:

TeamCity: 404 Error
YouTrack: 404 Error
Hub:      405 Error on login
UpSource: Blank Page
Octopus:  Working

【问题讨论】:

    标签: iis url-rewriting reverse-proxy


    【解决方案1】:

    好的,经过多次反复,我找到了所有这些服务的工作配置:

    • 设置 http IIS 反向代理
      • 以下模块需要安装在 IIS 中:
      • 为 URL 重定向创建网站
      • 使用有效证书为该网站创建 https 绑定
      • 在 [服务器] > 压缩下禁用动态内容压缩
      • 进入【服务器】>应用请求路由>服务器代理设置,选择☑“启用代理”
      • 确保设置了以下值:
        • HTTP 版本:通过
        • 超时:120
        • 为以下标头保留客户端 IP:X-Forwarded-For
        • 内存缓存持续时间:60
        • 查询字符串支持:忽略查询字符串
        • 响应缓冲区:4096
        • 响应缓冲区阈值:0
    • 设置 URL 重写和相关设置
      • 在 [Server] > URL Rewrite > View Server Variables 中添加以下服务器变量
        • HTTP_X_FORWARDED_HOST
        • HTTP_X_FORWARDED_SCHEMA
        • HTTP_X_FORWARDED_PROTO
      • 如下配置 web.config:

    web.config:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <clear />
                    <rule name="Reverse Proxy to TeamCity" stopProcessing="true">
                        <match url="^teamcity(.*)" />
                        <action type="Rewrite" url="http://server.company.com{R:1}" />
                    </rule>
                    <rule name="Reverse Proxy to Hub" stopProcessing="true">
                        <match url="^hub(.*)" />
                        <action type="Rewrite" url="http://server.company.com:8082/hub{R:1}" />
                        <serverVariables>
                            <set name="HTTP_X_FORWARDED_HOST" value="{HTTP_HOST}" />
                            <set name="HTTP_X_FORWARDED_SCHEMA" value="https" />
                            <set name="HTTP_X_FORWARDED_PROTO" value="https" />
                        </serverVariables>
                    </rule>
                    <rule name="Reverse Proxy to YouTrack" stopProcessing="true">
                        <match url="^youtrack(.*)" />
                        <action type="Rewrite" url="http://server.company.com:8080/youtrack{R:1}" />
                        <serverVariables>
                            <set name="HTTP_X_FORWARDED_HOST" value="{HTTP_HOST}" />
                            <set name="HTTP_X_FORWARDED_SCHEMA" value="https" />
                            <set name="HTTP_X_FORWARDED_PROTO" value="https" />
                        </serverVariables>
                    </rule>
                    <rule name="Reverse Proxy to UpSource" stopProcessing="true">
                        <match url="^upsource(.*)" />
                        <action type="Rewrite" url="http://server.company.com:8081/upsource{R:1}" />
                        <serverVariables>
                            <set name="HTTP_X_FORWARDED_HOST" value="{HTTP_HOST}" />
                            <set name="HTTP_X_FORWARDED_SCHEMA" value="https" />
                            <set name="HTTP_X_FORWARDED_PROTO" value="https" />
                        </serverVariables>
                    </rule>
                    <rule name="Reverse Proxy to Octopus" stopProcessing="true">
                        <match url="^octopus(.*)" />
                        <action type="Rewrite" url="http://server.company.com:8888/octopus{R:1}" />
                    </rule>
                    <rule name="Reverse Proxy to Collaboration General" stopProcessing="true">
                        <match url="(.*)" />
                        <action type="Rewrite" url="http://server.company.com/{R:1}" />
                    </rule>
                </rules>
            </rewrite>
            <security>
                <requestFiltering>
                    <requestLimits maxAllowedContentLength="300000000" maxUrl="6144" maxQueryString="4096" />
                </requestFiltering>
            </security>
        </system.webServer>
    </configuration>
    
    • 如下配置 Hub、YouTrack 和 UpSource
      • 注意:以hub.bat/youtrack.bat/upsource.bat开头的命令需要在相应服务的[Installation Directory]\bin中的相应文件上执行。

    命令:

    upsource.bat stop
    youtrack.bat stop
    hub.bat stop
    
    hub.bat configure --listen-port 8082 --base-url https://server.company.com/hub
    youtrack.bat configure --listen-port 8080 --base-url=https://server.company.com/youtrack--hub-url=https://server.company.com/hub/hub
    upsource.bat configure --listen-port 8081 --base-url=https://server.company.com/upsource --hub-url=https://server.company.com/hub/hub
    
    hub.bat start
    youtrack.bat start
    upsource.bat start --J-Dbundle.websocket.compression.enabled=false
    

    注意:我不知道为什么,但是 Hub 在其基地址后附加了一个额外的 /hub,这就是为什么 UpSource 的 hub-url 设置以 /hub/hub 结尾的原因。

    之后,我需要做的就是将重定向 URL 添加到服务的允许重定向 URL 列表中,并在 Hub > Settings > Services 中更改 TeamCity 和 OctopusDeploy 的基础 URL,现在一切正常。

    嗯,几乎完美。每当服务器重新启动时,我都需要手动重新启动 UpSource,因为我还没有找到一种方法来使用 --J-Dbundle.websocket.compression.enabled=false 参数将 upsource 注册为服务,但除此之外,一切完美运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多