使用Nginx 反向代理解決跨域問題(vue.js使用代理去掉vue.js因?yàn)榭缬蚨|發(fā)的options請(qǐng)求)
創(chuàng)新互聯(lián)建站專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、雨山網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城系統(tǒng)網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為雨山等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
我們的項(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)目啟動(dò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)正在啟動(dòng)中...============================="); if (error) { console.error(error) } else { console.info("==================9999系統(tǒng)啟動(dòng)成功?。?!=============================") } });
五、然后在項(xiàng)目目錄下使用命令node server.production.js 回車發(fā)現(xiàn)包缺少依賴,使用npm install [報(bào)錯(cuò)提示的依賴組件] 即可 分別有express、compression、http-proxy-middleware等。
然后啟動(dòng)項(xiàng)目就可以了 訪問地址http://localhost:8080/xxx即可
六、vue.js使用代理具體可以百度(在config配置文件里的inde.js修改就可了)
proxyTable: { '/api': { target: Host.Host,//設(shè)置你調(diào)用的接口域名和端口號(hà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è)參考,也希望大家多多支持創(chuàng)新互聯(lián)。
分享名稱:vue.js使用代理和使用Nginx來解決跨域的問題
文章位置:http://bm7419.com/article16/jdsjdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)站制作、企業(yè)網(wǎng)站制作、靜態(tài)網(wǎng)站、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)