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

标签 amazon-web-services nginx https amazon-elastic-beanstalk amazon-elb

我知道以前有人问过这个问题,但似乎没有什么对我有用。我尝试了多种不同的方法,例如这些问题中描述的答案:

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,所以我不完全确定编辑配置文件的工作原理 - 或者我是否做错了什么。

我的设置如下:

  • 路由 53 指向 Elastic Beanstalk (nginx)
  • ELB port configuration with ACM certificate (使用 tcp/ssl 让我的 websockets 工作)
  • 端口 8080
  • 上的 nodejs 应用程序

    我当前的 .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 在重定向之前呈现,我想在负载均衡器处重定向 - 它应该重定向的地方。

    最佳答案

    直接更新/tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf"非常困难。我发现了这个: https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/security-configuration/https-redirect/nodejs/https-redirect-nodejs.config ,它可以让你设置重定向,但这会改变我的其他配置文件太多。最好的方法是在 .ebextensions 文件夹中创建一个 redirect.config 文件:

    container_commands:
      https_redirect:
        command: |
         sed -i '/location \/ {/i \
                  set $redirect 0;\
                  if ($http_x_forwarded_proto != "https") {\
                    set $redirect 1;\
                  }\
                  if ($http_user_agent ~* "ELB-HealthChecker") {\
                    set $redirect 0;\
                  }\
                  if ($redirect = 1) {\
                    return 301 https://$host$request_uri;\
                  }\
          ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf```
    

    关于amazon-web-services - Elastic Beanstalk Http 重定向到 Https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35817362/

    相关文章:

    python - 从另一个区域连接到 DocumentDB

    reactjs - 将图像上传到 AWS S3 存储桶 ReactJS 时 POST 403 禁止 CORS 响应

    ios - 使用 AWS Appsync 将图像上传到 s3 的过程 ||使用 Appsync 上传 iOS 图片

    docker - Docker --volume不会将文件夹安装到Nginx容器中

    java - https 从一台服务器重定向到另一台服务器

    amazon-web-services - 每当文件进入 s3 存储桶时获取文件详细信息,然后为每个单独的文件触发粘合作业

    nginx 和 uwsgi : large difference between upstream response time and request time

    nginx - 如何禁用缓存 nginx

    ssl - Curl:修复 CURL (51) SSL 错误:没有替代证书主题名称匹配

    .htaccess - Http 到 Https 但不是整个站点