前后端分离项目,访问后端接口跨域问题解决办法
背景:在本地部署了一个前后端分离项目,后端部署方式:php+nginx;前端项目部署方式:node本地启动的服务(127.0.0.1:8089),现在前端页面访问时报出现跨域问题
解决办法:配置nginx允许全部跨域
配置文件:
# 该项目的后端配置文件
server {
listen 8088;
server_name http://wkkadmin.com;
root "/Users/wangkaikai/dnmp/www/wkkAdmin/Admin/public";
# 所有请求允许跨域
location / {
# 设置允许跨域
add_header ‘Access-Control-Allow-Origin‘ ‘*‘;
add_header ‘Access-Control-Allow-Methods‘ ‘GET,POST,DELETE‘;
add_header ‘Access-Control-Allow-Header‘ ‘Content-Type,*‘;
}
# 这是php部分
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}