Vue Nginx反向代理配置 解決生產環境跨域


Vue本地代理舉例:

module.exports = {
  publicPath: './',
  devServer: {
    proxy: {
      '/api': {
        target: 'https://movie.douban.com',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      '/bpi': {
        target: 'https://cdnopenapialifc.agaege.com/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/bpi': ''
        }
      }
    }
  },
  pwa: {
    iconPaths: {
      favicon32: 'favicon.ico',
      favicon16: 'favicon.ico',
      appleTouchIcon: 'favicon.ico',
      maskIcon: 'favicon.ico',
      msTileImage: 'favicon.ico'
    }
  }
}

  

Vue 本地代理編輯好后,能實現跨域獲取接口數據,但是打包后在生產環境接口報錯404,要怎樣才能解決生產環境跨域問題呢?
在開發環境配置好本地代理后,使用Nginx反向代理解決生產環境跨域問題!

1、修改Nginx的配置文件 xxx.conf

location /api {
   rewrite  ^.+api/?(.*)$ /$1 break; //可選參數,正則驗證地址
   include  uwsgi_params; //可選參數,uwsgi是服務器和服務端應用程序的通信協議,規定了怎么把請求轉發給應用程序和返回
   proxy_pass   https://www.xxxxx.cn:444; #此處修改為自己的請求地址,必填
}
###api為本地開發時,在config/index.js中的proxyTable: {}配置的請求代理
###根據具體情況修改

  2、記得重啟Nginx服務,使修改生效

 

舉例:

location /api {
rewrite ^.+api/?(.*)$ /$1 break;
include uwsgi_params;
proxy_pass https://movie.douban.com;
}

location /bpi {
rewrite ^.+bpi/?(.*)$ /$1 break;
include uwsgi_params;
proxy_pass https://cdnopenapialifc.agaege.com/;
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM