国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue.js使用代理和使用Nginx來解決跨域的問題

2019-11-19 14:25:19
字體:
來源:轉載
供稿:網友

使用Nginx 反向代理解決跨域問題(vue.js使用代理去掉vue.js因為跨域而觸發的options請求)

我們的項目還是需要node.js作為容器的

一、Windows 下安裝Nginx (官網下載穩定版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服務 location ^~/api { proxy_pass http://119.23.227.141:10001/;//你的后端IP和端口 } #根據路由設置,避免出現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文件如下

四、然后再加一個項目啟動配置文件 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("==================系統正在啟動中...============================="); if (error) {  console.error(error) } else {  console.info("==================9999系統啟動成功!!!=============================") }});

五、然后在項目目錄下使用命令node server.production.js 回車發現包缺少依賴,使用npm install [報錯提示的依賴組件] 即可 分別有express、compression、http-proxy-middleware等。

然后啟動項目就可以了 訪問地址http://localhost:8080/xxx即可

六、vue.js使用代理具體可以百度(在config配置文件里的inde.js修改就可了)

 proxyTable: {     '/api': {    target: Host.Host,//設置你調用的接口域名和端口號 別忘了加http    changeOrigin: true,    pathRewrite: {     '^/api': '/'//這里理解成用‘/api'代替target里面的地址,后面組件中我們掉接口時直接用api代替 比如我要調用'http://40.00.100.100:3002/user/add',直接寫‘/api/user/add'即可    }   }  },

以上這篇vue.js使用代理和使用Nginx來解決跨域的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 康马县| 理塘县| 台安县| 北宁市| 鹰潭市| 乃东县| 大埔区| 汾西县| 隆德县| 康乐县| 肃北| 潮州市| 哈尔滨市| 盐源县| 抚州市| 财经| 类乌齐县| 绥江县| 河曲县| 合阳县| 溧阳市| 抚州市| 延川县| 开封县| 云梦县| 中西区| 武义县| 黄平县| 东乡县| 随州市| 顺义区| 嘉祥县| 五华县| 东源县| 奈曼旗| 鹤岗市| 顺平县| 莆田市| 天等县| 丹江口市| 张家川|