redirect - 如何设置 Beanstalk + Nginx 将 http 重定向到 https?

标签 redirect nginx https amazon-ec2 amazon-elastic-beanstalk

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

我想将所有 http 请求重定向到 https。所以http://www.mysite.com -> https://www.mysite.com

我已经尝试创建一个 AWS 容器来实现诸如“server { listen 80; return 301 https://www.mysite.com/ $request_uri;}”之类的东西,但它不起作用。

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

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

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

谢谢大家

最佳答案

我已将此解决方案在线。

添加.ebextensions/00_nginx_https_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

关于redirect - 如何设置 Beanstalk + Nginx 将 http 重定向到 https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20167660/

相关文章:

java - 从 httpsrest api 调用 httprestapi 时出现错误 502

http - Golang ListenAndServeTLS 在浏览器中不使用 https 时返回数据

php - 如何使用 .htaccess 从非安全版本的站点重定向到安全版本的站点

将流量从测试站点重定向到正常站点

redirect - 我如何重定向到第一个子组件 vue 路由器

regex - 使用重定向配置重写 HAProxy 和 reqrep 路径

ruby - 通过 passenger 自动安装 nginx

java - HTTPS下载文件报错android 4.4

redirect - 无限重定向 - nginx

ssl - 使用 ELB 和 nginx 的 wss 的 ActionCable 404 错误