php - 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”文件
解决方案
推荐阅读
- javascript - 循环进入包含对象的可观察数组错误:找不到不同的支持对象'[object Object]'
- vue.js - 如何从预渲染中排除 index.vue,以便在 dist 文件夹(NuxtJS)中生成 index.html?
- asp.net-core - Flutter web 可以发送 Zimbra 电子邮件吗?
- html - 当屏幕变小时,如何防止跨度隐藏文本?
- c# - .NET5 辅助服务模板的 NLog 设置
- visual-studio - 图未显示在屏幕上
- reporting - 如何使用数据自动创建报告
- mongoose - 如何在猫鼬中创建产品模型数据但价格跟随尺寸和颜色?
- python - python中的经验分布函数
- apache-spark - Spark:为什么 foreach() 在 rdd.foreachPartition 中不起作用?