我想将所有HTTP请求重定向到ELB上的https请求。我有两个EC2实例。我正在为服务器使用Nginx。我尝试重写nginx conf文件没有任何成功。我希望就此提出一些建议。
我想将所有HTTP请求重定向到ELB上的https请求。我有两个EC2实例。我正在为服务器使用Nginx。我尝试重写nginx conf文件没有任何成功。我希望就此提出一些建议。
Answers:
AWS Application Load Balancer现在支持本机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标签)
                    ELB设置X-Forwarded-Proto标头,您可以使用它来检测原始请求是否是对HTTP的请求,然后再重定向到HTTPS。
您可以在serverconf中尝试:
if ($http_x_forwarded_proto = 'http') {
    return 301 https://yourdomain.com$request_uri;
}
看一下ELB文档。
nginx配置,但是该原理适用于任何Web服务器。
                    我遇到了同样的问题,在我的情况下,HTTPS完全由ELB处理,并且我不知道自己的源域,所以我最终做了如下操作:
server {
  listen 81;
  return 301 https://$host$request_uri;
}
server {
  listen 80;
  # regular server rules ...
}
然后当然将ELB“ https”指向实例端口80,然后将“ http”路由指向实例端口81。
Amazon Elastic Load Balancer(ELB)支持称为X-FORWARDED-PROTO的HTTP标头。通过ELB的所有HTTPS请求的X-FORWARDED-PROTO值将等于“ HTTPS”。对于HTTP请求,您可以通过添加以下简单的重写规则来强制执行HTTPS。对我来说,一切正常!
阿帕奇
您可以在.htaccess文件中添加以下行:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}
或者,如果您使用vhost.conf在同一EC2 Web服务器中管理多个域,则可以将以下内容添加到vhost.conf中(将其添加到您要对其使用https的域中):
<VirtualHost *:80>
...
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}
...
</VirtualHost>
IIS
安装IIS Url-Rewrite模块,使用配置GUI添加以下设置:
<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|^$)
                    上面的htaccess解决方案导致ELB健康检查失败。在找到在线文章中有人遇到与我相同的问题之前,我很难找到解决方案。他的解决方案是将其添加到htaccess文件的开头:
RewriteEngine on 
RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
要通过HTTP允许此请求和其他本地请求,同时将外部请求通过ELB重定向到HTTPS,请调整重写条件以使其在http上匹配,而在https上不匹配。
我对nginx和ELB配置有奇怪的问题。我的设置在ELB后面的一个Nginx中包含3种不同的服务。而且我遇到了混合内容问题:当您对ELB的请求是https,但仅在ELB HTTP中,并且服务器使用http创建到静态的相对路径时,浏览器将出现“混合内容”问题。而且我必须为没有重定向的http / https工作创建解决方案。
这是位于nginx/conf.d/文件夹中的配置:  
# Required for http/https switching
map $http_x_forwarded_port $switch {
  default   off;
  "80"    off;
  "443"   on;
}
这意味着我们将了解什么是真正的客户端协议。如您所见,我们将在$switchvar中使用它。现在,您可以在需要的所有位置使用它:
location ~ /softwareapi/index.php {
  fastcgi_param HTTPS $switch;
  .. other settings here ..
}
通过HTTPS设置,php应用程序将自动检测正确的协议,并仔细构建相对路径以防止混合内容问题。
最好的祝福。
基于@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"
    }
  }
}
              创建一个.ebextensions/00_forward_http_to_https.config具有以下内容的文件:
files: 
  /tmp/deployment/http_redirect.sh:
    mode: "000755"
    content: |
      APP_URL=`/opt/elasticbeanstalk/bin/get-config environment --output yaml | grep -oP 'APP_URL: \K([^\s)\"](?!ttp:))+'`
      sed -ie 's@$proxy_add_x_forwarded_for;@$proxy_add_x_forwarded_for;\n        if ($http_x_forwarded_proto = 'http') { return 301 https://'"$APP_URL"'$request_uri; }@' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf
container_commands:
  http_redirect:
    command: "/tmp/deployment/http_redirect.sh"
确保事先从AWS管理控制台设置APP_URL环境变量。