首页 > 解决方案 > Nginx cors 启用 html 文件不起作用

问题描述

我正在使用 AWS 和 CloudFront 设置 magento 2 环境,并尝试使用 nginx 配置为静态文件启用 CORS,并使其适用于除“.html”文件之外的所有扩展!

试图在全局范围内添加标题“Access-Control-Allow-Origin”,但仍不能明确地为“.html”工作

这是我的 nginx.conf 文件中的位置指令

location /static/ {
    # Uncomment the following line in production mode
    expires max;

    # Remove signature of the static files that is used to overcome the browser cache
    location ~ ^/static/version {
        rewrite ^/static/(version[^/]+/)?(.*)$ /static/$2 last;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|json|html)$ {
        #add_header Cache-Control "public";
        #add_header X-Frame-Options "SAMEORIGIN";
        add_header Cache-Control "max-age=604800, public";
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
        add_header 'Access-Control-Max-Age' '604800';
        add_header 'Access-Control-Allow-Headers' 'Host, Content-Type, Origin, Accept';
        expires +1y;

        if (!-f $request_filename) {
            rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }

    location ~* \.(html)$ {
            add_header Cache-Control "max-age=604800, public";
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
    }

    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;

        if (!-f $request_filename) {
           rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }
    if (!-f $request_filename) {
        rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

它适用于所有提到的扩展名,如 jpg、png ... 等。但仍然不适用于仅“.html”文件

标签: phpamazon-web-servicesnginxcorsamazon-cloudfront

解决方案


推荐阅读