ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

amazon-web-services – 如何让Elastic Beanstalk nginx支持的代理服务器从HTTP自动重定向到HTTPS?

2019-09-19 11:19:24  阅读:158  来源: 互联网

标签:nginx amazon-web-services amazon-ec2 elastic-beanstalk


我有一个Node.js支持的网站,我在Amazon Elastic Beanstalk上运行.

我的Node.js应用程序侦听端口8080,我在我的EB应用程序中使用nginx弹性负载均衡器配置,在端口80和443上侦听HTTP和HTTPS.

但是,我只想接受通过HTTPS发送的应用中的流量.

我可以在应用程序中安装一些东西来解决这个问题,但是我有兴趣让负载均衡器通过HTTPS将所有HTTP请求重定向到我的站点.

解决方法:

经过亚马逊付费支持的几次虚假启动后,他们最终确实通过了.您可以通过以下方式配置环境以响应端口80和443.然后在主Node.js应用程序文件夹中创建一个名为.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

亚马逊的支持团队解释说:这个配置创建了一个部署钩子,它将重写规则添加到/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf.

(之前他们曾经提供过.config,它将单独的文件复制到/etc/nginx/conf.d中,但是由于某种原因,这些文件要么没有效果,要么更糟糕,似乎覆盖或优先于默认的nginx配置.)

如果您想要撤消此操作,即删除挂钩,则需要删除此ebextension并发出命令以删除它创建的文件.您可以手动执行此操作,也可以通过临时安装的ebextensions命令执行此操作:

/opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh
/opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh

我没有试过这个,但可能这样的东西可以删除它们并撤消这个改变:

container_commands:
  00_undochange:
    command: rm /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh
  01_undochange:
    command: rm /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh

希望这可以在将来帮助别人.

标签:nginx,amazon-web-services,amazon-ec2,elastic-beanstalk
来源: https://codeday.me/bug/20190919/1812889.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有