【问题标题】:Cloudfront Nginx Rewrite Causing ProblemsCloudfront Nginx 重写导致问题
【发布时间】:2016-02-05 04:46:24
【问题描述】:

我正在使用 cloudfront 设置一个 cdn。我的云端分发的来源是我的 aws 负载均衡器 (ELB)。当我向 cloudfront 发出请求而不是获取 cloudfront url (cdn.mysite.com/images/image.jpg) 时,它被重定向到https://www.alio.com/images/image.jpg。由于我的 nginx.conf,我弄清楚了为什么它会这样做:

server {
    root /var/www/html/alio/public;
    index index.php;

    server_tokens off;

    server_name www.alio.com;

    location / {
        if ($http_x_forwarded_proto != 'https') {
            rewrite ^ https://$host$request_uri? permanent;
        }
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

重写 ^ https://$host$request_uri?永恒的;更改 url(当我删除重写时,我得到 cdn url)。我进行了重写以确保对我网站的所有请求都是 https。如果我检测到它是云端调用 ELB,是否有办法代替重写 301 重定向或不进行重写?

【问题讨论】:

    标签: amazon-web-services nginx url-rewriting amazon-cloudfront


    【解决方案1】:

    您是否已将 CloudFront 配置为将主机标头列入白名单?

    对于每个行为 > 转发标头 > 选择“白名单”> 从列表中选择“主机”并点击添加。

    此设置可确保主机标头 (cdn.mysite.com) 包含在返回源的请求中(因此请确保您已将 cdn.mysite.com 添加到 server_name 指令中)。

    如果您只希望通过 TLS 访问您的网站,则可能还值得考虑使用 HTTP 严格传输安全标头。将以下内容添加到您的配置中应该可以做到:

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    

    【讨论】:

      【解决方案2】:

      我经常看到这些类型的构造。他们是阿帕奇主义者,你应该学会以不同的方式做事。

      Http 和 https 是协议,因此应在处理协议的级别而不是处理文档位置的级别处理,除非它们是特定于位置的,在您的情况下它们不是。

      这还允许保持干净,不会无意中在 http 级别上配置一些东西,从而绕过始终 https 逻辑。

      所以 https 总是很简单:

      server {
          listen 80;
          return 301 https://$host$request_uri;
      }
      
      server {
          listen 443 ... ;
          ....
      }
      

      不应该让它更复杂:-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多