我希望将所有的HTTP请求重定向到ELB上的HTTPS请求。我有两个EC2实例,使用nginx作为服务器。我已经尝试在没有成功的情况下重新编写nginx配置文件。我希望得到一些关于此问题的建议。
我希望将所有的HTTP请求重定向到ELB上的HTTPS请求。我有两个EC2实例,使用nginx作为服务器。我已经尝试在没有成功的情况下重新编写nginx配置文件。我希望得到一些关于此问题的建议。
AWS应用程序负载均衡器现在支持本地HTTP到HTTPS重定向。
要在控制台中启用此功能,请执行以下操作:
也可以使用CLI实现相同功能,具体说明请参见此处。
在Cloudformation中也可以完成此操作,需要设置类似于以下内容的Listener对象:
HttpListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref LoadBalancer
Port: 80
Protocol: HTTP
DefaultActions:
- Type: redirect
RedirectConfig:
Protocol: HTTPS
StatusCode: HTTP_301
Port: 443
如果您仍在使用经典负载均衡器,请选择其他人描述的NGINX配置之一。
instances
选项卡) - Florian LudewigELB设置X-Forwarded-Proto
头,您可以使用它来检测原始请求是否为HTTP并重定向到HTTPS。
您可以在server
配置中尝试此操作:
if ($http_x_forwarded_proto = 'http') {
return 301 https://yourdomain.com$request_uri;
}
请查看ELB文档。
nginx
配置的,但原则适用于任何 Web 服务器。 - Dmitry Mukhin我有同样的问题,在我的情况下,HTTPS是完全由ELB处理的,而且我事先不知道我的源域,所以最终我做了这样的操作:
server {
listen 81;
return 301 https://$host$request_uri;
}
server {
listen 80;
# regular server rules ...
}
当然,接下来需要将 ELB 的 'https' 指向实例端口 80,将 'http' 路由指向实例端口 81。
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}
如果您在同一EC2 Web服务器上管理多个域名时使用vhost.conf,则可以将以下内容添加到vhost.conf中(将其添加到要使用https的域中):
<VirtualHost *:80>
...
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}
...
</VirtualHost>
IIS
安装 IIS Url-Rewrite 模块,使用配置界面添加以下设置:
<rewrite xdt:Transform="Insert">
<rules>
<rule name="HTTPS rewrite behind ELB 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>
在这里阅读更多。
RewriteCond %{HTTP:X-Forwarded-Proto} !(https|^$)
- natronite以上的.htaccess解决方案导致了ELB健康检查失败。我曾经遇到了一些麻烦,直到我在网上发现了一篇文章,其中有人遇到了和我一样的问题。他的解决方案是在.htaccess文件的开头添加以下内容:
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
基于 @Ulli 的回答,如果您想使用Terraform进行配置,这里提供一个示例>
resource "aws_alb_listener" "web" {
load_balancer_arn = "${aws_alb.web.arn}"
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
也许这不是你想要的解决方案,但另一种选择是在使用ELB的同时使用AWS CloudFront。CloudFront允许将所有传入的HTTP流量重定向到HTTPS。
# Required for http/https switching
map $http_x_forwarded_port $switch {
default off;
"80" off;
"443" on;
}
$switch
变量中拥有它。
并且此时您可以在需要的所有位置使用它:location ~ /softwareapi/index.php {
fastcgi_param HTTPS $switch;
.. other settings here ..
}
通过设置HTTPS,PHP应用程序将自动检测正确的协议,并谨慎构建相对路径,以防止混合内容问题。
此致敬礼。
我刚刚通过以下命令在EC2实例终端中进行了重定向测试:
curl -Iv [your url]
例如:curl -Iv http://example.com
要求
在AWS Route 53中设置DNS和名称服务器的托管区域
使用AWS证书管理器获取证书
在负载均衡器上添加侦听器,用于端口443选择2.中的证书和SSL
我所做的是:
步骤
1. 进入 EC2 负载均衡器
2. 进入监听器 ID HTTP:80(在规则列下面点击查看/编辑规则)
编辑默认操作:(在顶部点击铅笔图标,使用“切换到完整 URL”,直到输入 #{port}
才会变灰)
https://#{host}:443/#{path}?#{query}
添加一个操作(点击顶部的加号符号)
https://www.#{host}:443/#{path}?#{query}
3. 返回并进入侦听器ID HTTPS: 443(单击“查看/编辑规则”)
添加一个动作(在顶部单击加号符号)
https://www.#{host}:443/#{path}?#{query}
不要编辑HTTPS上的默认操作
这里是精确答案: 使用以下名称创建此文件,并将以下内容放入其中。
redirect-to-https.json
[
{
"Type": "redirect",
"RedirectConfig": {
"Protocol": "HTTPS",
"Port": "443",
"Host": "#{host}",
"Path": "/#{path}",
"Query": "#{query}",
"StatusCode": "HTTP_301"
}
}
]
aws elbv2 create-listener --load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:12345678910:loadbalancer/app/demo1-alb/b4d5fbc78965417f --port 80 --protocol HTTP --default-actions file://redirect-to-https.json