Amazon web services Cloudfront Nginx重写导致问题

Amazon web services Cloudfront Nginx重写导致问题,amazon-web-services,nginx,url-rewriting,amazon-cloudfront,Amazon Web Services,Nginx,Url Rewriting,Amazon Cloudfront,我正在使用cloudfront设置cdn。我的cloudfront发行版源于aws负载平衡器(ELB)。当我向cloudfront发出请求而不是获取cloudfront url(cdn.mysite.com/images/image.jpg)时,它会被重定向到。由于我的nginx.conf,我了解了它为什么会这样做: server { root /var/www/html/alio/public; index index.php; server_tokens off;

我正在使用cloudfront设置cdn。我的cloudfront发行版源于aws负载平衡器(ELB)。当我向cloudfront发出请求而不是获取cloudfront url(cdn.mysite.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\u uri?永久的;更改url(当我删除重写时,我会得到cdn url)。我进行了此重写,以确保对我的站点的所有请求都是https。如果有办法代替重写301重定向,或者在检测到cloudfront正在调用ELB时不重写?

是否已将cloudfront配置为白名单主机头

对于每个行为>转发标题>选择“白名单”>从列表中选择“主机”,然后点击添加

此设置确保主机头(cdn.mysite.com)包含在返回到源站的请求中(因此请确保已将cdn.mysite.com添加到服务器名称指令中)

如果您只希望通过TLS访问站点,那么还应该考虑使用HTTP Strict Transport Security标头。将以下内容添加到配置中应该可以做到这一点:

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

我经常看到这些类型的构造。他们是种族隔离,你应该学会用不同的方式去做

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

这还允许保持整洁,并且不会无意中在http级别上配置绕过始终https逻辑的内容

所以https总是很简单:

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

server {
    listen 443 ... ;
    ....
}
不应使其更复杂:-)