vue打包使用Nginx代理解决跨域问题


Posted in Javascript onAugust 27, 2018

vue 在开发环境,涉及跨域,就在本地配置了代理,但是部署到服务器上,就不行了。

解决方法有一下几种

  • 服务器端配置CORS
  • 用nginx反向代理,和访问本地服务器是一样的
  • 可以修改成正式线上的地址,再build

推荐

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

vue index.js文件源码

'use strict'
// Template version: 1.2.7
// see http://vuejs-templates.github.io/webpack for documentation.
const path = require('path')
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
changeOrigin: true,
proxyTable: {
'/api': {
target: 'https://api.it120.cc/fengyu',
changeOrigin:true,
pathRewrite: {
'^/api': ''
}
},
'/book': {
target: 'http://api.zhuishushenqi.com/',
changeOrigin:true,
pathRewrite: {
'^/book': ''
}
},
},
// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
/**
* Source Maps
*/
// https://webpack.js.org/configuration/devtool/#development
devtool: 'eval-source-map',
// If you have problems debugging vue-files in devtools,
// set this to false - it *may* help
// https://vue-loader.vuejs.org/en/options.html#cachebusting
cacheBusting: true,
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false,
},
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: './static',
assetsPublicPath: './',
/**
* Source Maps
*/
productionSourceMap: true,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
}
}

Nginx 配置文件源码

# power by www.php.cn
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid    logs/nginx.pid;
events {
  worker_connections 1024;
}
http {
  include    mime.types;
  default_type application/octet-stream;
  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  #         '$status $body_bytes_sent "$http_referer" '
  #         '"$http_user_agent" "$http_x_forwarded_for"';
  #access_log logs/access.log main;
  sendfile    on;
  #tcp_nopush   on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  #tcp_nodelay on;
 fastcgi_connect_timeout 300;
 fastcgi_send_timeout 300;
 fastcgi_read_timeout 300;
 fastcgi_buffer_size 128k;
 fastcgi_buffers 4 128k;
 fastcgi_busy_buffers_size 256k;
 fastcgi_temp_file_write_size 256k;
 #gzip on;
 gzip on;
 gzip_min_length 1k;
 gzip_buffers   4 32k;
 gzip_http_version 1.1;
 gzip_comp_level 2;
 gzip_types    text/plain application/x-javascript text/css application/xml;
 gzip_vary on;
 gzip_disable "MSIE [1-6].";
 server_names_hash_bucket_size 128;
 client_max_body_size   100m; 
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
  server {
    listen    80;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    root  "E:/phpStudy/PHPTutorial/WWW";
    location / {
      index index.html index.htm index.php l.php;
      autoindex off;
    }
    #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(.*)$ {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_path_info;
      fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
      include    fastcgi_params;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #  deny all;
    #}
location /api {
proxy_pass https://api.it120.cc/fengyu;
} 
  }
  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #  listen    8000;
  #  listen    somename:8080;
  #  server_name somename alias another.alias;
  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}
  # HTTPS server
  #
  #server {
  #  listen    443;
  #  server_name localhost;
  #  ssl         on;
  #  ssl_certificate   cert.pem;
  #  ssl_certificate_key cert.key;
  #  ssl_session_timeout 5m;
  #  ssl_protocols SSLv2 SSLv3 TLSv1;
  #  ssl_ciphers HIGH:!aNULL:!MD5;
  #  ssl_prefer_server_ciphers  on;
  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}
include vhosts.conf;
}

成功截图

vue打包使用Nginx代理解决跨域问题

vue打包使用Nginx代理解决跨域问题

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
一个加载js文件的小脚本
Jun 28 Javascript
js获得网页背景色和字体色的方法
Mar 21 Javascript
介绍一个简单的JavaScript类框架
Jun 24 Javascript
JS实现的简洁二级导航菜单雏形效果
Oct 13 Javascript
js clearInterval()方法的定义和用法
Nov 11 Javascript
适用于javascript开发者的Processing.js入门教程
Feb 24 Javascript
js css自定义分页效果
Feb 24 Javascript
js eval函数使用,js对象和字符串互转实例
Mar 06 Javascript
详解vue嵌套路由-query传递参数
May 23 Javascript
利用C/C++编写node.js原生模块的方法教程
Jul 07 Javascript
解决jquery appaend元素中id绑定事件失效的问题
Sep 12 jQuery
JavaScript之实现一个简单的Vue示例
Jan 17 Javascript
Vue cli构建及项目打包以及出现的问题解决
Aug 27 #Javascript
Vue.js实现双向数据绑定方法(表单自动赋值、表单自动取值)
Aug 27 #Javascript
vue实例中data使用return包裹的方法
Aug 27 #Javascript
vue项目打包部署到服务器的方法示例
Aug 27 #Javascript
解决vue中修改了数据但视图无法更新的情况
Aug 27 #Javascript
解决vue接口数据赋值给data没有反应的问题
Aug 27 #Javascript
vue 修改 data 数据问题并实时显示的方法
Aug 27 #Javascript
You might like
PHP的Laravel框架结合MySQL与Redis数据库的使用部署
2016/03/21 PHP
PHP基于正则批量替换Img中src内容实现获取缩略图的功能示例
2017/06/07 PHP
Laravel 微信小程序后端搭建步骤详解
2019/11/26 PHP
JavaScript OOP面向对象介绍
2010/12/02 Javascript
深入理解JavaScript系列(6):S.O.L.I.D五大原则之单一职责SRP
2012/01/15 Javascript
iphone safari不支持position fixed的解决方法
2012/05/04 Javascript
基于JavaScript 类的使用详解
2013/05/07 Javascript
js导出table到excel同时兼容FF和IE示例
2013/09/03 Javascript
JS实现模拟风力的雪花飘落效果
2015/05/13 Javascript
IE10中flexigrid无法显示数据的解决方法
2015/07/26 Javascript
Webpack 实现 AngularJS 的延迟加载
2016/03/02 Javascript
VC调用javascript的几种方法(推荐)
2016/08/09 Javascript
原生js和css实现图片轮播效果
2017/02/07 Javascript
微信小程序 开发经验整理
2017/02/15 Javascript
微信小程序开发入门基础教程
2017/04/19 Javascript
微信小程序如何获取openid及用户信息
2018/01/26 Javascript
vue 设置proxyTable参数进行代理跨域
2018/04/09 Javascript
微信小程序实现预览图片功能
2020/10/22 Javascript
微信小程序引入Vant组件库过程解析
2019/08/06 Javascript
JavaScript将数组转换为链表的方法
2020/02/16 Javascript
jQuery实现B2B网站后台管理系统侧导航
2020/07/08 jQuery
vue实现桌面向网页拖动文件的示例代码(可显示图片/音频/视频)
2021/03/01 Vue.js
详解使用 pyenv 管理多个版本 python 环境
2017/10/19 Python
Python实现可获取网易页面所有文本信息的网易网络爬虫功能示例
2018/01/15 Python
python3连接MySQL数据库实例详解
2018/05/24 Python
keras实现调用自己训练的模型,并去掉全连接层
2020/06/09 Python
详解Python利用configparser对配置文件进行读写操作
2020/11/03 Python
详解CSS3开启硬件加速的使用和坑
2017/08/21 HTML / CSS
木工主管岗位职责
2013/12/08 职场文书
安全检查与奖惩制度
2014/01/23 职场文书
女娲补天教学反思
2014/02/05 职场文书
授权委托书范本
2014/04/03 职场文书
职业规划实施方案
2014/06/10 职场文书
2014教师党员自我评议总结
2014/09/19 职场文书
护士自我推荐信范文
2015/03/24 职场文书
导游词之台湾安平古堡
2019/12/25 职场文书