加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

amazon-web-services – Elastic Beanstalk Http重定向到Https

发布时间:2020-12-13 21:00:09 所属栏目:Nginx 来源:网络整理
导读:我知道之前已经问过这个问题,但似乎没有什么对我有用.我尝试过多种不同的东西,例如这些问题中描述的答案: How to get Elastic Beanstalk nginx-backed proxy server to auto-redirect from HTTP to HTTPS? Redirecting EC2 elb from http to https 他们似乎

我知道之前已经问过这个问题,但似乎没有什么对我有用.我尝试过多种不同的东西,例如这些问题中描述的答案:

How to get Elastic Beanstalk nginx-backed proxy server to auto-redirect from HTTP to HTTPS?
Redirecting EC2 elb from http to https

他们似乎都没有工作.我是一个aws noob,所以我不完全确定如何编辑配置文件 – 或者我是否做错了什么.

我的设置如下:

> Route 53指向Elastic Beanstalk(nginx)
> ELB port configuration with ACM certificate(使用tcp / ssl,因为它使我的websockets工作)
>端口8080上的nodejs app

我当前的.ebextensions文件夹中的nginx.config文件(从this article开始):

files:
  "/tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
        upstream nodejs {
            server 127.0.0.1:8081;
            keepalive 256;
        }
        server {
            listen 8080;
            set $fixedWWW '';
            set $needRedir 0;
            # nginx does not allow nested if statements
            # check and decide on adding www prefix
            if ($host !~* ^www(.*)) {
                set $fixedWWW 'www.';
                set $needRedir 1;
            }
            # what about that https? the traffic is all http right now
            # but elastic load balancer tells us about the original scheme
            # using $http_x_forwarded_proto variable
            if ($http_x_forwarded_proto != 'https') {
                set $needRedir 1;
            }
            # ok,so whats the verdict,do we need to redirect?
            if ($needRedir = 1) {
                rewrite ^(.*) https://$fixedWWW$host$1 redirect;
            }
            location / {
                proxy_pass  http://nodejs;
                proxy_set_header   Connection "";
                proxy_http_version 1.1;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            }
            gzip on;
        }

但这似乎没有做任何事情.我已经没想完了.我不确定我是否错过了一步或什么,但我不知道该怎么做.作为一种解决方法,我的angularjs前端重定向非https请求,但是这太过于hacky并且在重定向之前渲染了一些DOM,我想在负载均衡器处重定向 – 它应该重定向.

看起来你正在尝试为非WWW和非HTTPS连接进行重定向.您是否尝试过仅仅http:// – >的简单案例? https://?

if ($http_x_forwarded_proto = "http") {
    return 301 https://$host$request_uri;
}

有时通过两个重定向更容易处理它,一个从HTTP到HTTPS,一个从非WWW到WWW.事实上,如果您要通过HSTS(https-无处不在)注册您的网站,他们需要这种方法.

编辑:另外,只是注意到配置的第一行,您可能想尝试直接注入nginx文件:

files:
  "/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf" :

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读