基本示例
vite.config.ts中添加proxy配置
1 2 3 4 5 6 7 8 9 10 11 12
| export default defineConfig({ server: { proxy: { '/mapi': { target: 'https://api.psvmc.cn', changeOrigin: true, rewrite: (path) => path.replace(/^\/mapi/, '') } } } })
|
项目中的接口请求就不要再添加请求的域名了。
这里都以/mapi开始,配置代理后所有以/mapi的请求都会使用Node的代理进行处理。
如上:
假如我们项目的访问地址是http://localhost:8080,接口的地址是https://api.psvmc.cn
那么http://localhost:8080/mapi/user/login的请求就会经过代理,代理实际访问的地址是https://api.psvmc.cn/user/login。
如果不配置rewrite
1 2 3 4 5 6 7 8 9 10 11
| export default defineConfig({ server: { proxy: { '/mapi': { target: 'https://api.psvmc.cn', changeOrigin: true } } } })
|
那么http://localhost:8080/mapi/user/login的请求就会经过代理,代理实际访问的地址是https://api.psvmc.cn/mapi/user/login。
验证代理生效
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| export default defineConfig({ server: { proxy: { '/mapi': { target: 'https://api.psvmc.cn', changeOrigin: true, rewrite: (path) => path.replace(/^\/mapi/, ''), configure: (proxy, options) => { proxy.on('proxyReq', (proxyReq, req, res) => { if (options.target && req.url) { console.log('Proxying request to:', options.target + req.url) } }) proxy.on('proxyRes', (proxyRes, req, res) => { console.log('Received response from target:', proxyRes.statusCode) }) } } } } })
|
添加configure配置我们可以在控制台查看代理后实际访问的地址。
在浏览器的F12查看代理前的访问的地址。
常见示例
字符串
1 2
| '/foo': 'http://localhost:4567',
|
注意
字符串简写写法,changeOrigin 默认是 true。
选项写法
1 2 3 4 5 6
| '/api': { target: 'http://jsonplaceholder.typicode.com', changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, '') },
|
正则写法
1 2 3 4 5 6
| '^/fallback/.*': { target: 'http://jsonplaceholder.typicode.com', changeOrigin: true, rewrite: (path) => path.replace(/^\/fallback/, '') },
|
WebSocket 代理
1 2 3 4 5
| '/socket.io': { target: 'ws://localhost:3000', ws: true }
|
以/socket.io开头的请求会被代理到ws://localhost:3000。
整体示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import { defineConfig } from 'vite'
export default defineConfig({ server: { proxy: { '/foo': 'http://localhost:4567', '/api': { target: 'http://jsonplaceholder.typicode.com', changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, '') }, '^/fallback/.*': { target: 'http://jsonplaceholder.typicode.com', changeOrigin: true, rewrite: (path) => path.replace(/^\/fallback/, '') }, '/socket.io': { target: 'ws://localhost:3000', ws: true } } } })
|
Nginx配置
假设Vue 3 项目打包后的静态文件存放在 /var/www/vue3-project 目录下,
后端接口服务器运行在 http://backend-server:8080。
打包后接口是无法访问的,因为没有Node进行代理了,我们可以使用Nginx进行代理。
Nginx配置如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| server { listen 80; server_name yourdomain.com; listen 443 ssl; ssl_certificate /etc/nginx/cert/xhkjedu.pem; ssl_certificate_key /etc/nginx/cert/xhkjedu.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on;
root /var/www/vue3-project; index index.html; if ($scheme = http) { return 301 https://$host$request_uri; }
location / { try_files $uri $uri/ /index.html; }
location /mapi { proxy_pass http://backend-server:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }
keepalive_timeout 60;
client_max_body_size 200m;
location ~ .*\.(html)$ { add_header Cache-Control no-store; } }
|