vue.js使用代理和使用Nginx來解決跨域的問題
使用Nginx 反向代理解決跨域問題(vue.js使用代理去掉vue.js因?yàn)榭缬蚨|發(fā)的options請求)
我們的項(xiàng)目還是需要node.js作為容器的
一、Windows 下安裝Nginx (官網(wǎng)下載穩(wěn)定版http://nginx.org/en/download.html)
二、修改config里的nginx.conf文件的server
server {
listen 8899;// 你的端口
server_name localhost;
root C:/ZOBSF_F/dist;//你打包部署的文件路徑
#charset koi8-r;
#access_log logs/host.access.log main;
# 匹配 api 路由的反向代理到API服務(wù)
location ^~/api {
proxy_pass http://119.23.227.141:10001/;//你的后端IP和端口
}
#根據(jù)路由設(shè)置,避免出現(xiàn)404
location / {
try_files $uri $uri/ @router;
index index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
三、 你的uve.js打包的dist文件如下

四、然后再加一個(gè)項(xiàng)目啟動配置文件 server.production.js
var express = require('express');
var app = express();
var compression = require('compression');
var proxyMiddleware = require('http-proxy-middleware')
var history = require('connect-history-api-fallback');
app.use(compression());
app.use(express.static(__dirname));
app.middleware = [//使用代理api
proxyMiddleware(['/api'], {target: 'http://192.168.11.103:10001',
changeOrigin: true,
pathRewrite: {
'^/api' : '/',
},}),
];
app.get('*', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
app.use(history());
app.use(app.middleware);
app.listen('8080', function(error) {
console.info("==================系統(tǒng)正在啟動中...=============================");
if (error) {
console.error(error)
} else {
console.info("==================9999系統(tǒng)啟動成功?。?!=============================")
}
});
五、然后在項(xiàng)目目錄下使用命令node server.production.js 回車發(fā)現(xiàn)包缺少依賴,使用npm install [報(bào)錯(cuò)提示的依賴組件] 即可 分別有express、compression、http-proxy-middleware等。
然后啟動項(xiàng)目就可以了 訪問地址http://localhost:8080/xxx即可
六、vue.js使用代理具體可以百度(在config配置文件里的inde.js修改就可了)
proxyTable: {
'/api': {
target: Host.Host,//設(shè)置你調(diào)用的接口域名和端口號 別忘了加http
changeOrigin: true,
pathRewrite: {
'^/api': '/'//這里理解成用‘/api'代替target里面的地址,后面組件中我們掉接口時(shí)直接用api代替 比如我要調(diào)用'http://40.00.100.100:3002/user/add',直接寫‘/api/user/add'即可
}
}
},
以上這篇vue.js使用代理和使用Nginx來解決跨域的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中$event使用之獲取當(dāng)前元素及相關(guān)元素
這篇文章主要介紹了vue中$event使用之獲取當(dāng)前元素及相關(guān)元素,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue3實(shí)現(xiàn)一個(gè)定高的虛擬列表
虛擬列表對于大部分一線開發(fā)同學(xué)來說是一點(diǎn)都不陌生的東西了,這篇文章主要來教大家如何在2分鐘內(nèi)實(shí)現(xiàn)一個(gè)定高的虛擬列表,感興趣的可以了解下2024-12-12
vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能(流程詳解)
無論是移動端還是pc端登錄或者注冊界面都會見到手機(jī)驗(yàn)證碼登錄這個(gè)功能,輸入手機(jī)號,得到驗(yàn)證碼,這篇文章主要介紹了基于vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能,需要的朋友可以參考下2019-12-12
Vue3中watch監(jiān)聽器及源碼學(xué)習(xí)
本文主要介紹了Vue3中watch監(jiān)聽器及源碼學(xué)習(xí),Watch偵聽器在Vue3中特性進(jìn)行了一些改變和優(yōu)化,下面來詳解的介紹一下基本使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
vue3實(shí)現(xiàn)圖片瀑布流展示效果實(shí)例代碼
這篇文章主要介紹了vue3實(shí)現(xiàn)圖片瀑布流展示效果的相關(guān)資料,該組件可以調(diào)整列數(shù)、支持懶加載、自定義每頁滾動數(shù)量、高度和點(diǎn)擊效果,作者展示了組件的效果,并詳細(xì)說明了實(shí)現(xiàn)方法,包括組件的創(chuàng)建和依賴的工具庫,需要的朋友可以參考下2024-11-11

