Nginx安裝和部署就不提了。
sever配置proxy_pass只能解決轉發的問題,如果轉發不帶header配置的話前端的預檢請求容易出現
Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response的問題,不過使用postman或直接訪問的話是可以正常請求到數據的。
所以這裏需要在Nginx配置裏補充一下
以下是詳細配置
location /api/ {
proxy_pass http://代理地址/; # 將地址代理到api上
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
}
由於開發時經常配到此類跨域問題,故做此記錄,如果還是出現跨域問題,則注意下請求頭。