在使用vue實現(xiàn)前后端分離的項目中,很容易出現(xiàn)跨域問題,糾結(jié)了很久,經(jīng)過網(wǎng)上查閱資料,終于解決了這個問題:
修改前端的nginx配置文件如下:
location / {
????????????root? /apps/webroot/lebaobei_edu/web;
? ? ? ? ? ? try_files $uri $uri/ /index.html =404;
? ? ? ? ? ? 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;
? ? ? ? ? ? }
? ? ? ? }
# 匹配以/apis/開頭的請求
? ? ? ? location ^~ /api/ {
? ? ? ? ? ? rewrite? ^/api/(.*)$ /$1 break;
? ? ? ? ? ? proxy_pass http://edu_lbb.bestshowgroup.com;//服務(wù)器端接口地址
? ? ? ? }