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 相关文章推荐
Display SQL Server Version Information
Jun 21 Javascript
不使用中间变量,交换int型的 a, b两个变量的值。
Oct 29 Javascript
Javascript操作URL函数修改版
Nov 07 Javascript
用html5 js实现点击一个按钮达到浏览器全屏效果
May 28 Javascript
javascript基于HTML5 canvas制作画箭头组件
Jun 25 Javascript
JQuery实现展开关闭层的方法
Feb 17 Javascript
Juery解决tablesorter中文排序和字符范围的方法
May 06 Javascript
JS基于Mootools实现的个性菜单效果代码
Oct 21 Javascript
js 获取经纬度的实现方法
Jun 20 Javascript
详解js模板引擎art template数组渲染的方法
Oct 09 Javascript
微信小程序实现的五星评价功能示例
Apr 25 Javascript
JS实现网站楼层导航效果代码实例
Jun 16 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
用header 发送cookie的php代码
2007/03/16 PHP
必须收藏的23个php实用代码片段
2016/02/02 PHP
linux下php上传文件注意事项
2016/06/11 PHP
PHP设计模式之注册树模式分析
2018/01/26 PHP
Laravel框架使用Redis的方法详解
2018/05/30 PHP
Laravel5.1 框架响应基本用法实例分析
2020/01/04 PHP
判断js对象是否拥有某一个属性的js代码
2013/08/16 Javascript
js实现添加可信站点、修改activex安全设置,禁用弹出窗口阻止程序
2016/08/17 Javascript
AngularJS 面试题集锦
2016/09/06 Javascript
layui文件上传实现代码
2017/05/20 Javascript
JavaScript实现多态和继承的封装操作示例
2018/08/20 Javascript
JS实现倒计时图文效果
2018/11/17 Javascript
layui导出所有数据的例子
2019/09/10 Javascript
JS实现transform实现扇子效果
2020/01/17 Javascript
vscode中Vue别名路径提示的实现
2020/07/31 Javascript
vue+iview实现分页及查询功能
2020/11/17 Vue.js
Python中矩阵库Numpy基本操作详解
2017/11/21 Python
使用python实现http及ftp服务进行数据传输的方法
2018/10/26 Python
python滑块验证码的破解实现
2019/11/10 Python
如何将 awk 脚本移植到 Python
2019/12/09 Python
Python selenium的基本使用方法分析
2019/12/21 Python
python GUI库图形界面开发之PyQt5信号与槽基本操作
2020/02/25 Python
python实现低通滤波器代码
2020/02/26 Python
python实现简单的购物程序代码实例
2020/03/03 Python
完美解决Django2.0中models下的ForeignKey()问题
2020/05/19 Python
Cotton On美国网站:澳洲时装连锁品牌
2016/10/25 全球购物
Skyscanner波兰:廉价航班
2017/11/07 全球购物
新秀丽官方旗舰店:Samsonite拉杆箱、双肩包、皮具
2018/03/05 全球购物
SQL Server笔试题
2012/01/10 面试题
如何为DataGridView添加一个定制的Column Type
2014/01/21 面试题
寄语学生的话
2014/04/10 职场文书
竞选大队长演讲稿
2014/04/29 职场文书
2014年语文教学工作总结
2014/12/17 职场文书
手术室护士个人总结
2015/02/13 职场文书
严以修身专题学习研讨会发言材料
2015/11/09 职场文书
vue里使用create, mounted调用方法
2022/04/26 Vue.js