nginx 解决跨域问题
- 请求一个非地址栏上的url域名时,浏览器可能会报跨域的问题,导致无法访问,这里简单记录下Nginx解决跨域的配置,加入这些代码即可。
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
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 200;
}
..........
}
- 除了Nginx以外,在自己应用层代码解决跨域也行。