- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 ASP.NET 网站,它通常通过 HTTPS 在 IIS 上运行。现在我想把它放在一个反向代理后面,它会为我做 SSL 终止。问题是来自反向代理的请求在 HTTP:80 上,当我像往常一样创建重定向响应时,响应的 Location
header 设置为 http://...
而不是 https://...
我知道反向代理在 header 中传递 X-Forwarded-Proto
并且我通常可以在创建重定向响应和重定向 URL 时检查该 header 。但是,如果可能的话,我想避免在代码周围散布检查 X-Forwarded-Whatever
并且在编码时还要考虑一件事,并且还必须仔细检查当它部署在代理后面时,一切是否仍然有效。
最佳答案
我发现的最佳方法是使用 IIS URL 重写模块并设置 HTTPS
和 SERVER_PORT
header ,这样被代理的站点甚至不知道它在代理后面工作。我在 Web.config
中使用了以下配置:
<rewrite>
<allowedServerVariables>
<add name="HTTPS" />
<add name="SERVER_PORT" />
</allowedServerVariables>
<rules>
<rule name="backend" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" />
</conditions>
<serverVariables>
<set name="HTTPS" value="on" />
<set name="SERVER_PORT" value="443" />
</serverVariables>
<action type="None" />
</rule>
</rules>
</rewrite>
上面的部分指出,只要传入请求有 X-Forwarded-Proto
设置为https,那么它应该设置服务器变量HTTPS
和 SERVER_PORT
模仿通过 HTTPS 发出的请求。有了这个配置,我就不必为 X-Forwarded-Proto
做任何检查了。在我自己的代码中。
请注意,URL 重写模块是在管道末尾调用的,值得注意的是,它在 HTTP 重定向模块之后运行。如果您使用此模块或任何其他 IIS 级机制进行重定向,构造的 URL 仍将具有错误的方案。到目前为止,我的解决方法是重新实现所有 IIS 级重定向以使用 URL 重写模块,而不是其他任何东西。
另请注意,通常 <allowedServerVariables>
无法从应用程序级别设置部分 Web.config
文件。您需要解锁该部分以便应用程序可以访问,或者您需要直接在根网站的配置中配置此部分。这是我用于在 Web 节点上安装和配置 URL 重写的脚本:
Param(
[string] $package = 'rewrite_2.0_rtw_x64.msi'
)
Start-Process -Wait msiexec -ArgumentList @('/i', $package, '/quiet', '/qn', '/norestart')
$applicationHost = "C:\Windows\System32\inetsrv\config\applicationHost.config"
$config = [xml](Get-Content $applicationHost)
$webServer = $config.configuration.configSections.sectionGroup | where {$_.name -eq "system.webServer"}
$rewrite = $webServer.sectionGroup | where {$_.name -eq "rewrite"}
$serverVariables = $rewrite.section | where {$_.name -eq "allowedServerVariables"}
$serverVariables.SetAttribute("overrideModeDefault", "Allow")
$config.Save($applicationHost)
关于asp.net - 在 SSL 终止反向代理后面运行时如何在 IIS 上创建重定向响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26904930/
我是一名优秀的程序员,十分优秀!