amazon-web-services - Cloudfront Nginx 重写导致问题

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

我正在使用 cloudfront 设置 CDN。我的 Cloudfront 发行版的起源是我的 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 重定向或不进行此重写?

最佳答案

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

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

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

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

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

关于amazon-web-services - Cloudfront Nginx 重写导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33526113/

相关文章:

amazon-web-services - AWS S3创建错误: "The event is not supported for notifications (Service: Amazon S3; Status Code: 400; Error Code: InvalidArgument"

ruby-on-rails - 从 Rails 应用程序中重新加载 nginx 配置

nginx - 无法选择合适的插件 : The nginx plugin is not working;

url-rewriting - 如何更改 Laravel 的本地主机 url

amazon-web-services - 用户数据在自定义 AMI(基于 Windows 2019 Base AMI 构建)中不起作用?

python - AWS Lambda 上的单元测试

AngularJs 图片上传到 S3

nginx - ElasticBeanstalk 上的 Websockets 给出 404

.htaccess - 为 admin.magentostore.com(不是/admin)的 Magento 创建一个管理 url

Apache 重写以保留查询字符串