在Azure Application Gateway中将HTTP重定向到HTTPS

问题描述 投票:10回答:6

我已配置应用程序网关(AG)来执行SSL终止/卸载。 AG配置为仅侦听端口443以进行HTTPS连接。是否可以将HTTP重定向到HTTPS,而不必:

  • 创建一个新VM,其中包含一个Web服务器,该Web服务器可以重定向流量并将AG配置为在端口80上侦听其后端池中的新VM,或者
  • 还允许HTTP连接到我的应用程序VM并在我的应用程序代码中处理重定向

我希望我忽略了AG的旗帜/功能。

azure azure-resource-manager azure-application-gateway
6个回答
7
投票

要扩展@ jonathan-mas的答案,

这可以仅使用命令行完成(截至2017年12月)。我不喜欢Powershell方法(有限的可移植性),我更喜欢AZ CLI,因为它更直接地回答了这个问题。

  1. 为您的HTTP流量创建一个侦听器(例如FE-HTTP-80-Site)。这可以使用Azure门户或CLI完成。
  2. 为您的HTTPS流量创建一个侦听器(例如FE-HTTPS-443-Site)。这可以在Azure门户或CLI中完成。
  3. 创建重定向配置:

az network application-gateway redirect-config create \ --gateway-name AppGateway \ -g RSgroupAppGateway \ -n Redirect-Site-toHTTPS \ --type Permanent \ --include-path true \ --include-query-string true \ --target-listener FE-HTTPS-443-Site

  1. 为HTTP流量创建规则:

az network application-gateway rule create \ --gateway-name AppGateway \ -g RSgroupAppGateway \ -n Rule-HTTP-80-Site \ --rule-type Basic \ --http-listener FE-HTTP-80-Site \ --redirect-config Redirect-Site-toHTTPS

参考概念:https://docs.microsoft.com/en-us/azure/application-gateway/application-gateway-configure-redirect-powershell

CLI参考:https://docs.microsoft.com/en-us/cli/azure/


3
投票

现在,Azure Application Gateway产品支持此功能,无需任何其他工具或服务。它使用PowerShell配置为described in this link

相关的PoSH代码从引用中复制并粘贴,以便将端口80重定向到443:

# Get the application gateway
$gw = Get-AzureRmApplicationGateway -Name AdatumAppGateway -ResourceGroupName AdatumAppGatewayRG

# Get the existing HTTPS listener
$httpslistener = Get-AzureRmApplicationGatewayHttpListener -Name appgatewayhttplistener -ApplicationGateway $gw

# Get the existing front end IP configuration
$fipconfig = Get-AzureRmApplicationGatewayFrontendIPConfig -Name appgatewayfrontendip -ApplicationGateway $gw

# Add a new front end port to support HTTP traffic
Add-AzureRmApplicationGatewayFrontendPort -Name appGatewayFrontendPort2  -Port 80 -ApplicationGateway $gw

# Get the recently created port
$fp = Get-AzureRmApplicationGatewayFrontendPort -Name appGatewayFrontendPort2 -ApplicationGateway $gw

# Create a new HTTP listener using the port created earlier
Add-AzureRmApplicationGatewayHttpListener -Name appgatewayhttplistener2  -Protocol Http -FrontendPort $fp -FrontendIPConfiguration $fipconfig -ApplicationGateway $gw 

# Get the new listener
$listener = Get-AzureRmApplicationGatewayHttpListener -Name appgatewayhttplistener2 -ApplicationGateway $gw

# Add a redirection configuration using a permanent redirect and targeting the existing listener
Add-AzureRmApplicationGatewayRedirectConfiguration -Name redirectHttptoHttps -RedirectType Permanent -TargetListener $httpslistener -IncludePath $true -IncludeQueryString $true -ApplicationGateway $gw

# Get the redirect configuration
$redirectconfig = Get-AzureRmApplicationGatewayRedirectConfiguration -Name redirectHttptoHttps -ApplicationGateway $gw


# Add a new rule to handle the redirect and use the new listener
Add-AzureRmApplicationGatewayRequestRoutingRule -Name rule02 -RuleType Basic -HttpListener $listener -RedirectConfiguration $redirectconfig -ApplicationGateway $gw

# Update the application gateway
Set-AzureRmApplicationGateway -ApplicationGateway $gw 

2
投票

如果您处理后端的重定向,则可以使用App Gateway发送的X-Forwarded-Proto标头查看原始请求,如果是使用重定向规则的HTTP,则重定向。

阿帕奇

要在Apache上执行此操作,请将以下内容添加到.htaccess文件中

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}

及其

使用IIS rewrite module将此添加到您的web.config文件中

<rewrite xdt:Transform="Insert">
  <rules>
    <rule name="HTTPS rewrite behind App Gw rule" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="false" />
      <conditions>
        <add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="https://{SERVER_NAME}{URL}" />
    </rule>
  </rules>
</rewrite>

1
投票

你当然可以,但只有PowerShell知道我的知识。在ARM中执行此操作的说明位于documentation上。

我通常会在这里发布说明,但这涉及到一些步骤,这将是一个怪物帖子!


0
投票

在Win2k16 \ IIS10和模块2.0中,Scott对IIS的回答对我不起作用; AG代理返回上游服务器错误;尝试通过IIS管理器加载重写模块将导致格式错误的XML错误。

删除了插入转换,重定向开始工作。

   <rewrite>
        <rules>
            <rule name="HTTP To HTTPS Redirect Behind App Gtwy" stopProcessing="true">
                <match url="^(.*)$" ignoreCase="false" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" />
                </conditions>
                <action type="Redirect" url="https://{SERVER_NAME}{URL}" redirectType="Found" />
            </rule>
        </rules>
    </rewrite>

0
投票

请使用以下命令,它将适合您

** $ appgw = Get-AzureRmApplicationGateway -Name GatewayName -ResourceGroupName ResourcegroupName

$ myHTTPSListener = Get-AzureRmApplicationGatewayHttpListener -Name appGatewayHttpListener -ApplicationGateway $ appgw

$ myHTTPListener = Get-AzureRmApplicationGatewayHttpListener -Name appGatewayHttpListener -ApplicationGateway $ appgw

Add-AzureRmApplicationGatewayRedirectConfiguration -Name redirectHttptoHttps -RedirectType Permanent -TargetListener $ myHTTPSListener -IncludePath $ true -IncludeQueryString $ true -ApplicationGateway $ appgw

$ redirectconfig = Get-AzureRmApplicationGatewayRedirectConfiguration -Name redirectHttptoHttps -ApplicationGateway $ appgw

Add-AzureRmApplicationGatewayRequestRoutingRule -Name redirectrule -RuleType Basic -HttpListener $ myHTTPListener -RedirectConfiguration $ redirectconfig -ApplicationGateway $ appgw

Set-AzureRmApplicationGateway -ApplicationGateway $ appgw **

© www.soinside.com 2019 - 2024. All rights reserved.