通过Nginx解决跨域问题,请看如下:
1.修改系统host文件,添加被调用方的域名:
127.0.0.1 b.com
2.在nginx的conf目录下新建一个vhost目录,用来存放虚拟主机的配置文件
3.在conf/nginx.conf文件下面添加内容,告诉nginx载入vhost目录下的所有以conf结尾的文件
include vhost/*.conf;
4.在vhost目录里创建b.com.conf文件
server{
listen 80;
server_name b.com;
location /{
proxy_pass http://localhost:8080/;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Max-Age 3600;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Headers $http_access_control_request_headers;
if ($request_method = OPTIONS){
return 200;
}
}
}
5.配置完毕,附加几个nginx命令
启动命令:start nginx.exe
测试配置文件是否正确 nginx.exe -t
重启命令 nginx.exe -s reload
停止命令 nginx.exe -s stop