本地搭建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圖片