在 IIS 中为 Flask 应用程序设置代理 headers

Setting up proxy headers in IIS for flask application

我在 windows 运行 http://localhost:3333 上有一个 Flask 应用程序。我希望域 example.com 使用 IIS 代理此应用程序。我已经使用以下设置设置了 IIS。

flask 中有一个路由,我用它来为忘记的密码生成 URL,就像这样 url_for('/password_reset',_external=True)

当我通过 example.com 访问页面时,在我使用 redirect() 的地方一切正常。但是无论我有 url_for(),URL 看起来都像 http://localhost:3333/password_reset 而不是 http://example.com/password_reset。如何让url_for()正确使用外网域名

编辑 1: 当前 web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpRedirect enabled="false" destination="http://localhost:3333" />
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:3333/{R:1}" logRewrittenUrl="true" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:3333/(.*)" />
                    <action type="Rewrite" value="http{R:1}://example.com/{R:2}" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
        <httpProtocol>
            <customHeaders>
                <add name="Host" value="{HOST}" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
    <system.web>
    <httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0" />
    <pages validateRequest="false" />
        <identity impersonate="false" />
    </system.web>
</configuration>

编辑 2: 解决方案:我对保护器 headers 执行了此操作,现在在使用 url_for().

时保留了原始主机
"C:\Windows\System32\inetsrv\appcmd.exe" set config -section:system.webServer/proxy /preserveHostHeader:"True" /commit:apphost

您可以使用 iis 反向代理模块,因为您需要安装 arr:

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

https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing

以下是示例规则:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
       
        <rewrite>
            <rules>
                <clear />
               
                <rule name="ReverseProxyInboundRule2" enabled="false" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://127.0.0.1:5000/{R:1}" />
                </rule>
            </rules>
            <outboundRules>
               
                <rule name="ReverseProxyOutboundRule2" preCondition="ResponseIsHtml1" enabled="false">
                    <match filterByTags="A, Form, Img" pattern="^http(s)?://127.0.0.1:5000/(.*)" />
                    <action type="Rewrite" value="http{R:1}://example.com/{R:2}" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
       
        <urlCompression doDynamicCompression="false" />
    </system.webServer>
</configuration>

根据编辑,解决方案有效。 我对保护器 headers 执行了此操作,现在在使用 url_for().

时保留了原始主机
"C:\Windows\System32\inetsrv\appcmd.exe" set config -section:system.webServer/proxy /preserv