nginx跨域问题解决

location /han {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5;
}
一、Access-Control-Allow-Origin,这里使用变量 $http_origin取得当前来源域,你们说用“*”表明容许全部,我实际使用并不成功,缘由未知;html

二、Access-Control-Allow-Credentials,为 true 的时候指请求时可带上Cookie,本身按状况配置吧;前端

三、Access-Control-Allow-Methods,OPTIONS必定要有的,另一般也就GET和POST,若是你有其它的也可加进去;web

四、Access-Control-Allow-Headers,这个要注意,里面必定要包含自定义的http头字段(就是说前端请求接口时,若是在http头里加了自定义的字段,这里配置必定要写上相应的字段),从上面可看到我写的比较长,我在网上搜索一些经常使用的写进去了,里面有“web-token”和“app-token”,这个是我项目里前端请求时设置的,因此我在这里要写上;后端

五、Access-Control-Expose-Headers,可不设置,看网上大体意思是默认只能获返回头的6个基本字段,要获取其它额外的,先在这设置才能获取它;跨域

六、语句“ if ($request_method = 'OPTIONS') { ”,由于浏览器判断是否容许跨域时会先日后端发一个 options 请求,而后根据返回的结果判断是否容许跨域请求,因此这里单独判断这个请求,而后直接返回;浏览器

原文:http://www.javashuo.com/article/p-vihyznkh-kt.html app

相关文章
相关标签/搜索