前言
Nginx配置允许跨域(CORS)
允许跨域
1 2 3 4 5 6 7 8 9 10 11
| location / { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE'; add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; if ($request_method = 'OPTIONS') { return 204; }
root html; index index.html index.htm; }
|
- 如果配置了反向代理,
add_header
字段的配置需要在proxy_pass
字段配置之前
1 2 3 4 5 6 7 8 9 10
| location / { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE'; add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; if ($request_method = 'OPTIONS') { return 204; }
proxy_pass http://localhost }
|
完成
参考文献
CSDN——牛牛Blog
腾讯云开发者社区——用户4919348