如何将HTTP重定向到运行nginx的Elastic Beanstalk的https? [英] How can I redirect http to https for Elastic Beanstalk running nginx?

查看:192
本文介绍了如何将HTTP重定向到运行nginx的Elastic Beanstalk的https?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在这个确切的主题上有很多SO问题。但是,似乎都没有使用最新版本的Elastic Beanstalk / Docker组合。

I know there are a lot of SO questions on this exact topic. However, none seem to work with the latest version of Elastic Beanstalk / Docker combination.

我正在运行一个 Django / Python Web应用程序 Docker ,然后我将其部署到Elastic Beanstalk。我希望http和https处于活动状态,因此我在 AWS EB 配置控制台中启用了 80 443 端口。这很好用。我的网站可通过http和https访问。但是,这不是我想要的。我希望端口 80 http )自动转发到端口 443 https )。

I am running a Django/Python web app inside a Docker, which I then deploy to Elastic Beanstalk. I want http and https to be active, so I enabled both port 80 and 443 in the AWS EB configuration console. This works great. My site is accessible over both http and https. However, this isn't really what I want. I want port 80 (http) automatically forward to port 443 (https).

我已经在SO和其他论坛上跟踪了每一条建议来调试这个,但我认为那里的信息太旧了。 (即,这个不再有用了。

I've followed every piece of advice out there on SO and other forums to debug this, but I think the info out there is too old. (Ie., this no longer works).

我找到了EB设置服务器的地方(在一个名为: / etc / nginx的文件中) /sites-enabled/elasticbeanstalk-nginx-docker-proxy.conf ),它的内容是:

I have found where EB sets up its servers (in a file called: /etc/nginx/sites-enabled/elasticbeanstalk-nginx-docker-proxy.conf), and it's contents are:

map $http_upgrade $connection_upgrade {
  default  "upgrade";
  ""       "";
}

server {
  listen 80;
  location / {
    proxy_pass          http://docker;
    proxy_http_version  1.1;
    proxy_set_header    Connection       $connection_upgrade;
    proxy_set_header    Upgrade          $http_upgrade;
    proxy_set_header    Host             $host;
    proxy_set_header    X-Real-IP        $remote_addr;
    proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
  }
}

当我从听80; 到听443 ssl; 并尝试在 https 上加载我的网站,我得到 ERR_CONNECTION_REFUSED

When I modify this file from listen 80; to listen 443 ssl; and the try to load my site on https, I get ERR_CONNECTION_REFUSED.

有人能指出我正确的方向修改此配置文件以从 http重定向 https

Can someone point me in the correct direction to modify this config file to redirect from http to https?

推荐答案

我假设您使用了 Python 3.4 with uWSGI 2(Docker)1.4.3版。作为我在此处的回答,您需要完善您的Elastic Beanstalk环境:

I assumed you used Python 3.4 with uWSGI 2 (Docker) version 1.4.3. As my answer in here, you need to coustumized your Elastic Beanstalk environment:


  • 弹性负载均衡器


    • 侦听端口80并将其代理到EC2实例端口80。

    • 侦听端口443并将其代理到EC2实例端口443。


    • 侦听端口80并通过重定向到HTTPS进行响应。

    • 收听端口443并提供请求。

    我虽然EB除了Python之外,预定义的Python 3.4(Docker)和单Docker容器几乎相同。我在在AWS Elastic Beanstalk上启用HTTPS和HTTP重定向。你可以在那里阅读指南。它告诉您如何配置Elastic Beanstalk单Docker容器服务HTTPS和HTTP(重定向到HTTPS)。

    I though EB Predefined Python 3.4 (Docker) and Single Docker Container is almost same, except in the Python thing. I've written a post in Enable HTTPS and HTTP-Redirect on AWS Elastic Beanstalk. You could read the guide there. It tells you how to configure Elastic Beanstalk Single Docker Container serve HTTPS and HTTP (redirect to HTTPS).

    如果您收到错误(通常是从Elastic Beanstalk示例启动它)在第一个登录页面上):

    If you got error (usually if you started it from Elastic Beanstalk sample on first landing page):


    更新名为:sg-xxxxxxxx的安全组失败原因:如果没有VpcId,则无法指定SecurityGroupEgress

    Updating security group named: sg-xxxxxxxx failed Reason: SecurityGroupEgress cannot be specified without VpcId

    您需要在 AWSEBLoadBalancerSecurityGroup>内指定 VpcId 。属性。所以,配置应该是:

    You need to specify your VpcId inside AWSEBLoadBalancerSecurityGroup > Properties. So, the config should be:

    ...
    "AWSEBLoadBalancerSecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Allow HTTP and HTTPS",
        "VpcId": "vpc-xxxxxxxx",
        "SecurityGroupIngress": [
          {
            "IpProtocol": "tcp",
            "FromPort": 80,
            "ToPort": 80,
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": 443,
            "ToPort": 443,
            "CidrIp": "0.0.0.0/0"
          }
        ],
        "SecurityGroupEgress": [
          {
            "IpProtocol": "tcp",
            "FromPort": 80,
            "ToPort": 80,
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": 443,
            "ToPort": 443,
            "CidrIp": "0.0.0.0/0"
          }
        ]
      }
    },
    ...
    

    这篇关于如何将HTTP重定向到运行nginx的Elastic Beanstalk的https?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆