我已将应用程序网关(AG)配置为执行SSL终止/卸载。 AG配置为仅侦听端口443以进行HTTPS连接。是否可以将HTTP重定向到HTTPS而不必:
我希望我忽视AG中的旗帜/功能。
答案 0 :(得分:7)
要扩展@ jonathan-mas答案,
这可以仅使用命令行完成(截至2017年12月)。我不喜欢Powershell方法(有限的可移植性),我更喜欢AZ CLI,因为它更直接地回答了这个问题。
为您的HTTP流量创建一个侦听器(例如T
)。这可以使用Azure门户或CLI完成。
为您的HTTPS流量创建一个侦听器(例如FE-HTTP-80-Site
)。这可以在Azure门户或CLI中完成。
创建重定向配置:
FE-HTTPS-443-Site
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 :(得分: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 :(得分: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>
答案 3 :(得分:1)
你当然可以,但只有PowerShell知道我的知识。在ARM中执行此操作的说明位于documentation。
我通常会在这里发布说明,但这涉及到一些步骤,这将是一个怪物帖子!
答案 4 :(得分:0)
Scott对IIS的回答在Win2k16 \ IIS10和模块2.0上对我不起作用; 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>
答案 5 :(得分:0)
请使用以下命令,它将为您工作
** $ appgw = Get-AzureRmApplicationGateway-名称GatewayName -ResourceGroupName资源组名称
$ myHTTPSListener = Get-AzureRmApplicationGatewayHttpListener-名称appGatewayHttpListener -ApplicationGateway $ appgw
$ myHTTPListener = Get-AzureRmApplicationGatewayHttpListener-名称appGatewayHttpListener -ApplicationGateway $ appgw
Add-AzureRmApplicationGatewayRedirectConfiguration-名称redirectHttptoHttps -RedirectType永久-TargetListener $ myHTTPSListener -IncludePath $ true -IncludeQueryString $ true -ApplicationGateway $ appgw
$ redirectconfig = Get-AzureRmApplicationGatewayRedirectConfiguration-名称redirectHttptoHttps -ApplicationGateway $ appgw
Add-AzureRmApplicationGatewayRequestRoutingRule-名称redirectrule -RuleType基本-HttpListener $ myHTTPListener -RedirectConfiguration $ redirectconfig -ApplicationGateway $ appgw
Set-AzureRmApplicationGateway -ApplicationGateway $ appgw **
答案 6 :(得分:0)
HTTP到HTTPS重定向现在也可以通过门户进行配置。概念是相同的:为http创建一个侦听器,然后添加一个重定向到https侦听器的规则。
https://docs.microsoft.com/en-us/azure/application-gateway/redirect-http-to-https-portal