首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置Beanstalk + Nginx将http重定向到https?

问如何设置Beanstalk + Nginx将http重定向到https?
EN

Stack Overflow用户
提问于 2013-11-24 04:45:34
回答 4查看 3.7K关注 0票数 2

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

我想将所有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,但带有Cipher/SSL证书。好吧,当我检查我的EC2实例上的nginx配置文件时,我只发现了一个"server {listen8080“,而不是”listent80“。

谢谢你们所有人。

EN

回答 4

Stack Overflow用户

发布于 2014-10-31 03:02:24

我已经上线了这个解决方案。

添加.ebextensions/00_nginx_https_rw.config

代码语言:javascript
复制
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
票数 4
EN

Stack Overflow用户

发布于 2015-05-20 09:33:50

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

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2017-03-30 14:45:38

对于那些不使用负载均衡器的人来说,来自user3888643's answer的if块将无法工作。所以我完全删除了它(不确定这个解决方案是否有任何问题),它对我来说是有效的:

代码语言:javascript
复制
  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

至:

代码语言:javascript
复制
  sed -i '/listen 8080;/a \    return 301 https://$host$request_uri;\n' /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20167660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档