首頁  >  文章  >  web前端  >  在vue.js中使用Nginx來解決跨域

在vue.js中使用Nginx來解決跨域

亚连
亚连原創
2018-06-07 18:17:153229瀏覽

下面我就為大家分享一篇vue.js使用代理程式和使用Nginx來解決跨域的問題,具有很好的參考價值,希望對大家有所幫助。

使用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 webpack中如何實作非同步元件載入? ############在vue-resource中有關jsonp跨域問題#############在vue裡面透過父元件如何修改子元件樣式##### #

以上是在vue.js中使用Nginx來解決跨域的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn