在弹性beanstalk的负载均衡器中通过IIS重写直接重定向到https

时间:2013-11-05 14:52:59

标签: asp.net iis url-rewriting amazon-web-services elastic-beanstalk

如何使用IIS的url重写模块强制用户在弹性beanstalk负载均衡器后面使用ssl?

4 个答案:

答案 0 :(得分:40)

由于一些原因,这比听起来更难。一,负载均衡器正在处理ssl,因此从负载均衡器传递的请求永远不会使用ssl。如果使用传统的重写规则,您将获得无限循环的重定向。另一个需要解决的问题是,如果AWS健康检查收到重定向响应,它将失败。

  1. 解决方案的第一步是创建healthcheck.html页面并将其设置在根目录中。内容是什么并不重要。
  2. 将负载均衡器设置为使用healthcheck.html文件进行运行状况检查。
  3. 在web.config的<system.webServer><rewrite><rules>部分添加以下重写规则:

    <rule name="Force Https" stopProcessing="true">
       <match url="healthcheck.html" negate="true" />
       <conditions>
           <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
       </conditions>
       <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
    </rule>
    
  4. 请注意,规则匹配除了我们的healthcheck文件之外的任何内容。这样可以确保负载均衡器的运行状况检查成功,并且不会错误地将服务器从负载中删除。

    负载均衡器在标头中传递X-Forwarded-Proto值,让我们知道请求是否通过https。如果该值不是https,则触发我们的规则,并使用https返回永久重定向。

答案 1 :(得分:13)

首先,我要感谢Ross的原始答案,它让我开始构建一个IIS URL重写规则,该规则通过使用我在网站后面使用的现有HTTP到HTTPS重定向规则为我工作AWS Elastic Load Balancer。

<rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
        <add input="{REMOTE_HOST}" pattern="localhost" negate="true" />
        <add input="{REMOTE_ADDR}" pattern="127.0.0.1" negate="true" />
        <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>

此规则允许您在Visual Studio中或在端口80上的服务器上本地访问您的站点,而无需通过HTTPS访问,因此您只需要在服务器上具有端口80的绑定。它不会受到其他人提到的事情的影响(重复的查询字符串等)。

我个人对健康检查没有任何问题,我不需要在服务器上创建弹性负载均衡器来ping的文件。我已将我的负载均衡器设置为TCP:80上的运行状况检查,并且可以正常运行。

答案 2 :(得分:1)

如果你使用ELB但是不能使用ALB,那么Luke的答案是完美的。对于ALB Ross Pace的回答是正确的。 但您也可以将两者结合使用,这样您就可以在本地访问该站点,而无需重定向到HTTPS。

<rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
    <match url="healthcheck.html" negate="true" />
        <conditions>
            <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true"/>
            <add input="{REMOTE_HOST}" pattern="localhost" negate="true"/>
            <add input="{REMOTE_ADDR}" pattern="127.0.0.1" negate="true"/>
            <add input="{HTTP_HOST}" pattern="localhost" negate="true"/>
        </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
</rule>

答案 3 :(得分:-1)

这适用于我的应用程序 - IIS 8.5,在AWS ALB后面将HTTP重定向到HTTPS。关键是添加appendQueryString =&#34; false&#34;防止重定向上的查询字符串重复。您可以根据需要添加用于运行状况检查和本地主机处理的陷阱。我不需要添加运行状况检查陷阱,因为我们将其添加到应用程序的web.config中,使其特定于应用程序。我们的运行状况检查是域上的默认应用程序,因此不受影响。

class RadiusInputError(Exception):
    pass

class Circle:
    def __init__(self,radius):
        self.radius=radius
        if type(self.radius) == "<class 'str'>":
            raise RadiusInputError

try:
    a = Circle('Hello')
except RadiusInputError:
    print("'Hello' is not a number.")