允许跨域
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
# 设置允许跨域
add_header Access-Control-Allow-Origin *;
}
设置不缓存
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
add_header Access-Control-Allow-Origin *;
if ($request_filename ~* .*\.(?:htm|html|text)$) {
# 设置html资源不缓存
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
}
}
设置不缓存后,发现允许跨域失效
查阅资源发现,if 判断下设置add_header,会重置add_header,造成上面设置失效,需要重新设置下允许跨域。最终设置如下:
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
# 设置允许跨域
add_header Access-Control-Allow-Origin *;
if ($request_filename ~* .*\.(?:htm|html|text)$) {
# 设置html资源不缓存
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
# 重新设置允许跨域
add_header Access-Control-Allow-Origin *;
}
}