设置允许所有的请求
1 2 3 4 5
| server { location / { add_header 'Access-Control-Allow-Origin' '*'; } }
|
只允许GET请求
1 2 3 4 5 6
| server { location / { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Request-Method' 'GET'; } }
|
请求白名单
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server { location / { if ($http_origin ~* (baidu\.com|github.xuexb.com)$) { add_header 'Access-Control-Allow-Origin' '$http_origin'; add_header 'Access-Control-Allow-Credentials' true; add_header 'Access-Control-Request-Method' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } } }
|
iconfont 字体跨域配置
1 2 3 4 5 6 7
| server { root xxx; location ~* \.(eot|otf|ttf|woff|svg)$ { add_header Access-Control-Allow-Origin *; } }
|
但如果你的 location
已经配置了, 可以使用 if
判断添加, 如:
1 2 3 4 5 6 7 8
| server { location / { if ($document_uri ~ \.(eot|otf|ttf|woff|svg)$) { add_header Access-Control-Allow-Origin *; } } }
|