Redirect 如何建立Beanstalk+;Nginx是否将http重定向到https?

Redirect 如何建立Beanstalk+;Nginx是否将http重定向到https?,redirect,nginx,https,amazon-ec2,amazon-elastic-beanstalk,Redirect,Nginx,Https,Amazon Ec2,Amazon Elastic Beanstalk,我的域指向Beanstalk应用程序(DNS别名)。 我已经在Beanstalk实例上正确设置了SSL证书。 所以现在: ->Beanstalk应用程序与http ->带有https的Beanstalk应用程序 我想将所有http请求重定向到https。所以-> 我已经尝试创建一个AWS容器来实现类似“server{listen 80;return 301$request_uri;}”的东西,但它不起作用 我已经在谷歌上花了几个小时,试图找到一些关于如何做到这一点的指导。我发现了一些线索,比如3

我的域指向Beanstalk应用程序(DNS别名)。 我已经在Beanstalk实例上正确设置了SSL证书。 所以现在: ->Beanstalk应用程序与http ->带有https的Beanstalk应用程序

我想将所有http请求重定向到https。所以->

我已经尝试创建一个AWS容器来实现类似“server{listen 80;return 301$request_uri;}”的东西,但它不起作用

我已经在谷歌上花了几个小时,试图找到一些关于如何做到这一点的指导。我发现了一些线索,比如301重定向,重写。。。但我无法将任何解决方案应用于我的Beanstalk EC2实例

也许我需要一个更详细的解释如何做到这一点。 有人能帮帮我吗

PS:有一件事我很难理解,负载平衡器说负载平衡器端口80指向实例端口80,负载平衡器端口443(HTTPS)也指向实例端口80,但使用密码/SSL证书。 好的,当我检查EC2实例上的nginx配置文件时,我只找到一个“server{listen 8080”,而不是“listen 80”


谢谢大家。

我已将此解决方案联机

Add.ebextensions/00\u nginx\u https\u rw.config

files:
  "/tmp/45_nginx_https_rw.sh":
    owner: root
    group: root
    mode: "000644"
    content: |
      #! /bin/bash

      CONFIGURED=`grep -c "return 301 https" /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf`

      if [ $CONFIGURED = 0 ]
        then
          sed -i '/listen 8080;/a \    if ($http_x_forwarded_proto = "http") { return 301 https://$host$request_uri; } \n' /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
          logger -t nginx_rw "https rewrite rules added"
          exit 0
        else
          logger -t nginx_rw "https rewrite rules already set"
          exit 0
      fi

container_commands:
  00_appdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/appdeploy/enact
  01_configdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact
  02_rewrite_hook_perms:
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh
  03_rewrite_hook_ownership:
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh

基于上面的代码,这是我用来将http请求重定向到https以获得独立(即不在负载平衡器后面)Docker映像的代码:

files:
  "/tmp/000_nginx_https_redirect.sh":
    owner: root
    group: root
    mode: "000644"
    content: |
      #!/bin/bash
      sed -i 's/80;/80;\n    return 301 https:\/\/$http_host$request_uri;\n/' /etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf

container_commands:
  00_appdeploy_rewrite_hook:
    command: cp -v /tmp/000_nginx_https_redirect.sh /opt/elasticbeanstalk/hooks/appdeploy/enact
  01_configdeploy_rewrite_hook:
    command: cp -v /tmp/000_nginx_https_redirect.sh /opt/elasticbeanstalk/hooks/configdeploy/enact
  02_rewrite_hook_perms:
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/000_nginx_https_redirect.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/000_nginx_https_redirect.sh
  03_rewrite_hook_ownership:
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/000_nginx_https_redirect.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/000_nginx_https_redirect.sh

我不确定用户3888643的答案是否仍然正确,因为aws今年早些时候更新了他们自己的一些安装脚本在elastic beanstalk上运行的方式,但我刚刚与aws支持人员进行了检查,这仍然是建议的解决方案。向.ebextensions添加一个文件,例如带有以下内容的.ebextensions/00_nginx_https_rw.config

files:
  "/tmp/45_nginx_https_rw.sh":
    owner: root
    group: root
    mode: "000644"
    content: |
      #!/usr/bin/env bash
  CONFIGURED=`grep -c "return 301 https" /opt/elasticbeanstalk/support/conf/webapp.conf`

  if [ $CONFIGURED = 0 ]
    then
      sed -i '/  location \/ {/a \    if ($http_x_forwarded_proto = "http") { \n        return 301 https://$host$request_uri;\n    }' /opt/elasticbeanstalk/support/conf/webapp.conf
      logger -t nginx_rw "https rewrite rules added"
      exit 0
    else
      logger -t nginx_rw "https rewrite rules already set"
      exit 0
  fi

container_commands:
  00_appdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/appdeploy/enact
  01_configdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact
  02_rewrite_hook_perms:
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh
  03_rewrite_hook_ownership:
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh
  04_reload_nginx:
    command: /etc/init.d/nginx reload
需要注意的一点是:我发现我无法部署此文件,因为.ebextensions中文件的早期(不正确)版本之间存在交互,因此会出现错误,部署将失败,即使该文件不再位于正在部署的repo中

[Instance: i-0c767ece] Command failed on instance.
Return code: 6     
Output: nginx: [warn] duplicate MIME type "text/html" in /etc/nginx/nginx.conf:38 nginx:
[emerg] unknown directive "...." in /etc/nginx/conf.d/000_config.conf:4
 nginx: configuration file /etc/nginx/nginx.conf test failed.
 container_command 04_reload_nginx in .ebextensions/ssl_redirect.config failed.
For more detail, check /var/log/eb-activity.log using console or EB CLI.

看起来每个实例都有一个以前在/etc/nginx/conf.d/中部署的文件的副本,所以我必须进入每个实例并删除我在/etc/nginx/conf.d中以前的配置文件,一旦我这样做了,部署就会顺利进行。

对于那些不使用负载平衡器的人来说,
if
块将不起作用e完全删除了它(不确定此解决方案是否有任何问题),它对我有效:

  sed -i '/listen 8080;/a \    if ($http_x_forwarded_proto = "http") { return 301 https://$host$request_uri; }\n' /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
致:


在哪里可以找到正确的方法?在我的情况下不起作用:
[error]9579#0:*7上游在从上游读取响应头时过早关闭连接,客户端:80.215.204.109,服务器:,请求:“GET/favicon.ico HTTP/1.1”,上游:http://127.0.0.1:8081/favicon.ico,主机:“domainname.fr”,推荐人:"http://domainname.fr
有什么建议吗?我正在使用一个实例为我工作!谢谢:)
  sed -i '/listen 8080;/a \    return 301 https://$host$request_uri;\n' /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf