s

vue/vite 开发模式和生产模式 跨域配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
【vue.config.js 或者 vite.config.js 中配置】  server: {
    // proxy: { //server 开启, 生产模式
    //   '/api': {
    //     changeOrigin: true,
    //     target: 'http://yourdomain:9504',
    //     // rewrite: (path) => path.replace(/^\/tp5\/index\/rsdemo/, '')
    //   }
    // },
    proxy: {
      '/tp5/index/rsdemo': { // 开发模式  ,后面请求 就需要带上 这一字符串
        changeOrigin: true,
        target: 'http://localhost:8999' // http:// 不可少, 如果只写 localhost:3001 代理会失败
      }
    }
  }

  axios:

import axios from 'axios';

const instance = axios.create({ // 创建实例
  timeout: 10000,
  baseURL: '/tp5/index/rsdemo' //localhost
  // baseURL: '/api' // 175 server 开启
})

useage:

1
2
3
4
5
6
7
const server = '/point/getPoints'
const local = '/getPoints'
const {
  data: data
} = await proxy.$axios.post(local, { // 这里把axios配置为全局api了
  'belongTo': path,
});
复制代码
但是需要注意的是,这个跨域只针对开发模式有效,一旦打包之后,前端配置的跨域就不起作用了,打包后就必须部署在web服务器上,脱离了 vue的代理配置。
如果是部署在nginx上,反向代理配置如下:
    server {
        listen       3000;
        server_name  localhost;

        location / {
            root   D:/phpstudy_pro/WWW/biwDOT/code/biwDot/dist; // 这里是打包生成的dist目录
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        } 
        location /index/rsdemo { // 这里是跨域接口前缀, 当请求地址为 http://localhost:3000/index/rsdemo/xxx 时 就会代理到 http://localhost:8999/index/rsdemo/xxx
           proxy_pass http://localhost:8999;
        }
    }
复制代码

  

本文作者:努力不搬砖的iori

本文链接:https://www.cnblogs.com/Hijacku/p/15923670.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   努力不搬砖的iori  阅读(3200)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.