您好,跨域请求通常是通过代码层面来进行处理使用的,下述是一个nginx处理跨域请求的例子,您可以根据实际情况来进行修改使用。
- location / {
- # 允许跨域的请求,可以自定义变量$http_origin,*表示所有
- add_header 'Access-Control-Allow-Origin' *;
- # 允许携带cookie请求
- add_header 'Access-Control-Allow-Credentials' 'true';
- # 允许跨域请求的方法:GET,POST,OPTIONS,PUT
- add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT';
- # 允许请求时携带的头部信息,*表示所有
- add_header 'Access-Control-Allow-Headers' *;
- # 允许发送按段获取资源的请求
- add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
- # 一定要有!!!否则Post请求无法进行跨域!
- # 在发送Post跨域请求前,会以Options方式发送预检请求,服务器接受时才会正式请求
- 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;
- # 对于Options方式的请求返回204,表示接受跨域请求
- return 204;
- }
- }
复制代码 |