add_header
Nginx
响应数据时,要告诉浏览器一些头信息,就要使用add_header
。例如跨域访问(详细参见Nginx跨域访问配置)
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'X-Requested-With';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS'
# 由于跨域请求,浏览器会先发送一个OPTIONS的预检请求,我们可以缓存第一次的预检请求的失效时间
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 2592000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}