IIS URL 重写 ~ 重写端口路径
IIS URL Rewrite ~ Rewriting Paths to Ports
我的情况如下:
我们在单个服务器上有一系列 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 Rewrite 来处理 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 管理器中对其进行了测试,它可以正确识别规则和条件,并且 returns 正确的向后规则。
但是,当我尝试访问 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 上,地址旁边有一个 "Connection is not secure" 通知,说 "Parts of the web site (such as images) are not secure"。进入页面信息,它在技术详细信息下的安全选项卡中表示连接仅部分加密。但是,页面的其余部分似乎工作正常。
TeamCity 和 YouTrack 更加异想天开。在 Chrome 上,TeamCity 工作正常(一段时间直到随机断开连接),但 YouTrack 出现 404 错误,而在 Firefox 上,TeamCity 和 YouTrack 都以无法使用的 "text-only" 形式显示。
同时,UpSource 在任一浏览器上提供了一个空白页面。
我已经在各种浏览器和机器上对此进行了交叉测试,得出的结论是,如果我已经在这些服务器上登录了这些服务的 http 版本,TeamCity、YouTrack 和 Hub 只能 "sort of" 工作.如果我没有登录,那么我会收到 TeamCity 和 YouTrack 的 404 错误。至于 Hub,我在尝试使用 "POST-Method not supported" 消息登录时收到 405 错误。
所以我上面白写配置的基本结果如下:
TeamCity: 404 Error
YouTrack: 404 Error
Hub: 405 Error on login
UpSource: Blank Page
Octopus: Working
好吧,经过大量来回,我找到了所有这些服务的工作配置:
- 设置 http IIS 反向代理
- 需要在IIS中安装以下模块:
- URL 重写(通过 https://www.iis.net/downloads/microsoft/url-rewrite)
- 应用程序请求路由(通过 https://www.iis.net/downloads/microsoft/application-request-routing)
- WebSocket 协议(va 服务器管理器:Web 服务器 (IIS) > Web 服务器 > 应用程序开发 > WebSocket 协议)
- 动态内容压缩(通过服务器管理器:Web 服务器 (IIS) > Web 服务器 > 性能 > 动态内容压缩)
- 为 URL 重定向创建一个网站
- 使用有效证书为该网站创建 https 绑定
- 在 [服务器] > 压缩下禁用动态内容压缩
- 转到 [服务器] > 应用程序请求路由 > 服务器代理设置和 select ☑ “启用代理”
- 确保设置了以下值:
- HTTP 版本:直通
- Time-out: 120
- 为以下 header 保留客户端 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 中更改 TeamCity 和 OctopusDeploy 的基础 url >设置 > 服务,现在一切正常。
嗯,几乎完美。每当服务器重新启动时,我都需要手动重新启动 UpSource,因为我还没有找到使用 --J-Dbundle.websocket.compression.enabled=false 参数将 upsource 注册为服务的方法,但除此之外, 一切正常。
我的情况如下:
我们在单个服务器上有一系列 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 Rewrite 来处理 SSL 身份验证,将 IIS 服务器设置为反向代理。这已经有效,我现在可以通过 https://server.company.com
不过,趁着我也想用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 管理器中对其进行了测试,它可以正确识别规则和条件,并且 returns 正确的向后规则。
但是,当我尝试访问 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 上,地址旁边有一个 "Connection is not secure" 通知,说 "Parts of the web site (such as images) are not secure"。进入页面信息,它在技术详细信息下的安全选项卡中表示连接仅部分加密。但是,页面的其余部分似乎工作正常。
TeamCity 和 YouTrack 更加异想天开。在 Chrome 上,TeamCity 工作正常(一段时间直到随机断开连接),但 YouTrack 出现 404 错误,而在 Firefox 上,TeamCity 和 YouTrack 都以无法使用的 "text-only" 形式显示。
同时,UpSource 在任一浏览器上提供了一个空白页面。
我已经在各种浏览器和机器上对此进行了交叉测试,得出的结论是,如果我已经在这些服务器上登录了这些服务的 http 版本,TeamCity、YouTrack 和 Hub 只能 "sort of" 工作.如果我没有登录,那么我会收到 TeamCity 和 YouTrack 的 404 错误。至于 Hub,我在尝试使用 "POST-Method not supported" 消息登录时收到 405 错误。
所以我上面白写配置的基本结果如下:
TeamCity: 404 Error
YouTrack: 404 Error
Hub: 405 Error on login
UpSource: Blank Page
Octopus: Working
好吧,经过大量来回,我找到了所有这些服务的工作配置:
- 设置 http IIS 反向代理
- 需要在IIS中安装以下模块:
- URL 重写(通过 https://www.iis.net/downloads/microsoft/url-rewrite)
- 应用程序请求路由(通过 https://www.iis.net/downloads/microsoft/application-request-routing)
- WebSocket 协议(va 服务器管理器:Web 服务器 (IIS) > Web 服务器 > 应用程序开发 > WebSocket 协议)
- 动态内容压缩(通过服务器管理器:Web 服务器 (IIS) > Web 服务器 > 性能 > 动态内容压缩)
- 为 URL 重定向创建一个网站
- 使用有效证书为该网站创建 https 绑定
- 在 [服务器] > 压缩下禁用动态内容压缩
- 转到 [服务器] > 应用程序请求路由 > 服务器代理设置和 select ☑ “启用代理”
- 确保设置了以下值:
- HTTP 版本:直通
- Time-out: 120
- 为以下 header 保留客户端 IP:X-Forwarded-For
- 内存缓存持续时间:60
- 查询字符串支持:忽略查询字符串
- 响应缓冲区:4096
- 响应缓冲区阈值:0
- 需要在IIS中安装以下模块:
- 设置URL重写及相关设置
- 在[Server] > URL Rewrite > View Server Variables 添加以下服务器变量
- HTTP_X_FORWARDED_HOST
- HTTP_X_FORWARDED_SCHEMA
- HTTP_X_FORWARDED_PROTO
- 配置web.config如下:
- 在[Server] > URL Rewrite > View Server Variables 添加以下服务器变量
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 中更改 TeamCity 和 OctopusDeploy 的基础 url >设置 > 服务,现在一切正常。
嗯,几乎完美。每当服务器重新启动时,我都需要手动重新启动 UpSource,因为我还没有找到使用 --J-Dbundle.websocket.compression.enabled=false 参数将 upsource 注册为服务的方法,但除此之外, 一切正常。