目录
- 概述
- Nginx 跨域配置
- php7进阶到架构师相关阅读
概述
这是关于php进阶到架构之Nginx进阶学习的第四篇文章:Nginx跨域配置
- 第一篇:5分钟搞懂nginx的location匹配规则
- 第二篇:nginx rewrite规则详解
- 第三篇:5分钟上手nginx分流实战
- 第四篇:Nginx跨域配置
Nginx 跨域配置
使用场景:
网站A通过jqeury请求网站B的api接口时,会提示跨域问题,这时候有2种跨域处理。
一是:服务器端设置跨域问题,比如后端是php开发
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, X-Token");
header("Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH");
header("Access-Control-Allow-Credentials: true");
二是:在nginx设置跨域问题配置。即本文重点介绍的方法
在nginx.conf文件http里配置
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type;
2、在(有多个项目配置的)具体的项目配置里location /里配置
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type;
return 204;
}
本文暂时没有评论,来添加一个吧(●'◡'●)