【问题标题】:Elastic Beanstalk Redirect Missing Colon? [closed]Elastic Beanstalk 重定向缺少冒号? [关闭]
【发布时间】:2019-12-06 23:41:39
【问题描述】:

我在 Elastic Beanstalk 中设置了一个网站,但是当我在浏览器中输入 website.com URL 时,它会自动将我定向到 https//website.com 并且缺少冒号...如果我添加 www.website,它就可以工作。 com 虽然...或者如果我输入https://www.website.com

.ebextensions/prod01.config 文件的内容

files:
  /etc/nginx/conf.d/proxy.conf:
    owner: root
    group: root
    mode: "000644"
    content: |
      # Elastic Beanstalk Managed

      # Elastic Beanstalk managed configuration file
      # Some configuration of nginx can be by placing files in /etc/nginx/conf.d
      # using Configuration Files.
      # http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/customize-containers.html 


      upstream nodejs {
          server 127.0.0.1:8081;
          keepalive 256;
      }

      server {
          listen 8080;
          server_name website.com;

          if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
              set $year $1;
              set $month $2;
              set $day $3;
              set $hour $4;
          }
          access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
          access_log  /var/log/nginx/access.log  main;

          location / {
              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://www.heyants.com$request_uri;
              }

              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;
          }
      }

      server {
          listen 8080;
            server_name www.website.com;

          if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
              set $year $1;
              set $month $2;
              set $day $3;
              set $hour $4;
          }
          access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
          access_log  /var/log/nginx/access.log  main;


          location / {
              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;
              }

              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;
      gzip_comp_level 4;
      gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

      }
  /opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
    owner: root
    group: root
    mode: "000755"
    content: |
      #!/bin/bash -xe
      rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
      if [[ -e /etc/init/nginx.conf ]] ; then
        echo Using initctl to stop and start nginx
        initctl stop nginx || true
        initctl start nginx
      else
        echo Using service to stop and start nginx
        service nginx stop 
        service nginx start
      fi

container_commands:
  removeconfig:
    command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"

更新

我已更新为使用下面提供的 AWS 参考之一;但是重定向的一种情况仍然不起作用;我希望通过这次明确的更新,可以找到真正适用于所有人的规范解决方案。

【问题讨论】:

  • 你能进入服务器看看 nginx 文件长什么样吗?您还可以在更新后添加nginx -T 命令,它将转储文件的所有内容以确保它是否正在更新
  • 你可以试试这个方法吗? github.com/awsdocs/elastic-beanstalk-samples/blob/master/…。不要使用=,而是使用!= 来检查http 标头
  • 使用nginx而不是应用负载均衡器是否有原因,是单实例环境吗?

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


【解决方案1】:

根据您的要求使用以下内容更新驻留在此处的 AWS Elastic Beanstalk 配置文件 "/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"

location / {
    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;
    }

    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;
}

【讨论】:

    【解决方案2】:

    我用你的配置测试过:

    请求:GET http://website.com/ --> 响应:301,位置:http://https://www.website.com/

    第二个分号被浏览器删除。这是出乎意料的,也许是错误。 您可以尝试改用重写吗:

    listen 8080;
    server_name website.com;
    ...
    if ($redirect = 1) {
        rewrite ^(.*) https://www.website.com$1 permanent;
    }
    

    请求:GET https://website.com/ --> 失败

    在 website.com 规则上,它仅在其 http 时才会重定向到 https://www.website.com/。如果你想https://website.com/ 重定向到https://www.website.com/ 然后添加

    listen 8080;
    server_name website.com;
    ...
    if ($host = "website.com") {
        rewrite ^(.*) https://www.website.com$1 permanent;
    }
    

    【讨论】:

    • 您是否尝试过使用return 308 https://...?也可以试试 302307。抱歉只能建议反复试验,因为这个 nginx 行为(错误?)不符合 doc
    猜你喜欢
    • 2013-03-30
    • 2016-08-25
    • 2016-06-19
    • 2019-10-04
    • 2018-01-01
    • 2019-11-28
    • 2014-07-05
    • 2018-05-02
    • 2021-02-23
    相关资源
    最近更新 更多