amazon-web-services – Elastic Beanstalk Http重定向到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" :

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...