IIS 等效于“proxy_set_header X-Forwarded-Proto https;”

Posted

技术标签:

【中文标题】IIS 等效于“proxy_set_header X-Forwarded-Proto https;”【英文标题】:IIS Equivalent of "proxy_set_header X-Forwarded-Proto https;" 【发布时间】:2016-01-08 15:43:20 【问题描述】:

nginx 中这种配置的 IIS 等效项是什么?

proxy_set_header X-Forwarded-Proto https;

我在 Windows 服务器上运行 JetBrains YouTrack,使用 IIS 作为终止 SSL 代理,并在尝试登录时收到此错误:

HTTP ERROR 405

Problem accessing /hub/auth/login. Reason:

    HTTP method POST is not supported by this URL
Powered by Jetty://

我的 web.config 如下所示:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Reverse Proxy" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="(.*)" />
          <!-- Redirect all requests to non-HTTPS site. -->
          <action type="Rewrite" url="http://my.youtrack.site/R:1" logRewrittenUrl="true" />
        </rule>
      </rules>
    </rewrite>
    <handlers>
      <clear />
      <!-- No other handlers required. Must clear them otherwise ASP.NET might try to intercept *.svc paths etc. -->
      <add name="Rewrite" path="*" verb="*" modules="RewriteModule" resourceType="Unspecified" />
    </handlers>
  </system.webServer>
</configuration>

我正在尝试遵循以下来源的解决方案:https://confluence.jetbrains.com/display/YTD65/Linux.+JAR+in+Nginx+Web+Server,但适用于 IIS

【问题讨论】:

【参考方案1】:

找到https://confluence.jetbrains.com/display/YTD65/Configuring+Proxy#ConfiguringProxy-IISreverseproxy后解决

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Reverse Proxy" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="(.*)" />
                    <!-- Redirect all requests to non-HTTPS site. -->
                    <action type="Rewrite" url="http://my.youtrack.site/R:1" logRewrittenUrl="true" />
                    <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>
            </rules>
            <allowedServerVariables>
                <add name="HTTP_X_FORWARDED_HOST" />
                <add name="HTTP_X_FORWARDED_SCHEMA" />
                <add name="HTTP_X_FORWARDED_PROTO" />
            </allowedServerVariables>
        </rewrite>
        <handlers>
            <clear />
            <!-- No other handlers required. Must clear them otherwise ASP.NET might try to intercept *.svc paths etc. -->
            <add name="Rewrite" path="*" verb="*" modules="RewriteModule" resourceType="Unspecified" />
        </handlers>
    </system.webServer>
</configuration>

【讨论】:

【参考方案2】:

我能够使用以下脚本将 X-Forwarded-Proto 标头添加到应用程序请求路由转发的所有请求中:

Set-WebConfiguration -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/rewrite/allowedServerVariables" -Value (@name="HTTP_X_FORWARDED_PROTO")
Set-WebConfiguration -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/rewrite/globalRules" -value @name='ForwardedHttps'
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/rewrite/globalRules/rule[@name='ForwardedHttps']/serverVariables" -name "." -value @name='HTTP_X_FORWARDED_PROTO';value='https'
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/rewrite/globalRules/rule[@name='ForwardedHttps']/conditions" -name "." -value @input='HTTPS';pattern='On'

为了使代码正确处理 HTTP/HTTPS 重定向,可能还需要设置以下配置设置:

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.webServer/proxy" -name "preserveHostHeader" -value "True"
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.webServer/proxy" -name "reverseRewriteHostInResponseHeaders" -value "False"

【讨论】:

以上是关于IIS 等效于“proxy_set_header X-Forwarded-Proto https;”的主要内容,如果未能解决你的问题,请参考以下文章

无法将该规则转化为等效的IIS格式,因为有不支持的标记:E

apache配置的等效值是多少

在 IIS7 中使用 WAS 时 global.asax Application_Start 等效项是啥

Nginx反向代理下IIS获取真实IP

IIS 相当于 Apache 中的 VirtualHost

OWIN 自主机中等效的会话变量