怎么在vue中利用Nginx代理解决跨域问题

  介绍

本篇文章给大家分享的是有关怎么在vue中利用Nginx代理解决跨域问题,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

解决方法有一下几种

<李>

服务器端配置歌珥

<李>

用Nginx反向代理,和访问本地服务器是一样的

<李>

可以修改成正式线上的地址,再构建

推荐

使用Nginx配置反向代理,这样就可以在前端彻底解决跨域问题。

vue指数。js文件源码

& # 39; use 严格# 39;   版本://,Template  1.2.7。编写//,阅读http://vuejs-templates.github.io/webpack , for 文档。   const  path =,要求(& # 39;path & # 39;)   module.exports =, {   戴夫:{//路径   assetsSubDirectory: & # 39;静态# 39;   assetsPublicPath: & # 39;/& # 39;   changeOrigin:,真的,   proxyTable: {   & # 39;/api # 39;:, {   目标:& # 39;https://api.it120.cc/fengyu& # 39;   changeOrigin:没错,   pathRewrite: {   & # 39;^/api # 39;:, & # 39; & # 39;   }   },   & # 39;/书# 39;:,{   目标:& # 39;http://api.zhuishushenqi.com/& # 39;   changeOrigin:没错,   pathRewrite: {   & # 39;^/书# 39;:,& # 39;& # 39;   }   },   },//,Various  Dev  Server 设置   主持人:,& # 39;localhost # 39;,,//,还要be  overwritten  by  process.env.HOST   端口:8080年,,,//,还要be  overwritten  by  process.env.PORT,, if  port  is 使用,拷贝,a  free  one  will  be 确定   autoOpenBrowser:假的,   errorOverlay:,真的,   notifyOnErrors:,真的,   调查:假的,,//https://webpack.js.org/configuration/dev-server/# devserver-watchoptions -/* *   *,Source 地图   *///https://webpack.js.org/configuration/devtool/开发   devtool: & # 39; eval-source-map& # 39;//,If 你;have  problems  debugging  vue-files  devtools,拷贝//,set 却;能够用false 作用;it  * *,可以帮助//https://vue-loader.vuejs.org/en/options.html cachebusting   cacheBusting:,真的,//,CSS  Sourcemaps  off  by  default  because  relative  paths 断开连接;“buggy"//,with 却;能够选择,,according 用,CSS-Loader  README//,(https://github.com/webpack/css-loader # sourcemaps)//,our 经验,拷贝,活动对象;generally  work  as 预期,//,just  be  aware  of 却;能够issue  when  enabling 却;能够选择。   cssSourceMap:假的,   },   构建:{//,Template  for  index . html   指数:,path.resolve (__dirname, & # 39; . ./dist/index . html,),//路径   assetsRoot: path.resolve (__dirname, & # 39; . ./dist # 39;),   assetsSubDirectory: & # 39;。/静态# 39;   assetsPublicPath: & # 39;。/& # 39;/* *   *,Source 地图   */productionSourceMap:,真的,//https://webpack.js.org/configuration/devtool/生产   devtool: & # 39; # source-map& # 39;//,Gzip  off  by  default  as  many  popular  static  hosts  such //,Surge 或是Netlify  already  gzip  all  static  assets  for 你。//,Before  setting 用“真实”,make  sure ://,npm  install ——save-dev  compression-webpack-plugin   productionGzip:假的,   productionGzipExtensions: [& # 39; js # 39;,, & # 39; css # 39;],//,Run 从而,build  command  with  an  extra  argument //,View 从而,bundle  analyzer  report  after  build 完成://,“npm  run  build ——报告”//,Set 用“真实”,或“假”,用always  turn  it 提醒趁机离开   bundleAnalyzerReport: process.env.npm_config_report   }   }

Nginx配置文件源码

#, power  by  www.php.cn   # user 没有人;   worker_processes  1;   # error_log 日志/error.log;   # error_log 日志/error.log 注意;   # error_log 日志/error.log 信息;   # pid ,,,日志/nginx.pid;   events  {   worker_connections 才能;1024;   }   http  {   include 才能,,,,mime.types;   default_type 才能;应用程序/八进制;   #才能log_format  main  & # 39; remote_addr 美元;作用;remote_user 美元;[time_local美元],“request"美元;,& # 39;   号,,,,,,,,,,,& # 39;status 美元;body_bytes_sent 美元;“http_referer"美元;,& # 39;   号,,,,,,,,,,,& # 39;“http_user_agent"美元;,“http_x_forwarded_for"美元;& # 39;;   # access_log 才能日志/access.log 主要;   sendfile 才能,,,,,   # tcp_nopush 才能,,,   # keepalive_timeout 才能0;   keepalive_timeout 才能;65;   # tcp_nodelay 才能;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在vue中利用Nginx代理解决跨域问题