本地搭建demo的时候,angular项目默认部署在4200端口,然后台服务器部署在8080端口; 当用angular发送请求到服务器的时候会报跨域问题nginx
解决方案是在中间加一层nginxjson
配置以下 nginx:api
location /api { proxy_pass http://localhost:8080/angular/demo; }
angular增长配置文件 proxy.conf.json跨域
{ "/api": { "target": "http://localhost", "secure": false } }
启动angular项目的时候 使用 ng serve --proxy-config proxy.conf.json服务器
这样 angular 请求 http://localhost:4200/api/getList会被解析为 http://localhost/api/getListcode
nginx接收到请求,转发到 http://localhost:8080/angular/demo/getList图片