Refer to the documentation:vite official website、node-http-proxy
Full Example:
export default defineConfig({
server: {
proxy: {
// abbreviated way of writing a character string
'/foo': 'http://localhost:4567',
// Option writing
'/api': {
target: '',
changeOrigin: true,
rewrite: (path) => (/^\/api/, '')
},
// regular expression writing
'^/fallback/.*': {
target: '',
changeOrigin: true,
rewrite: (path) => (/^\/fallback/, '')
},
// utilization proxy an actual example
'/api': {
target: '',
changeOrigin: true,
configure: (proxy, options) => {
// proxy be 'http-proxy' 的an actual example
}
}
}
}
})
Property Description:
-
target: is the destination address, example
'/foo': 'http://abc:4567'
In the case of a complete requesthttp://loaclhost:8080/foo/login Proxy to address after matching keyword /foohttp://abc:4567/foo/login On, it should be noted that the replacement of the content in front of the keywords, keywords including the content behind will be added to the target address as is. - changeOrigin: true/false, default: false, changes the source of the request header to the target's URL
- secure: true/false, whether to validate the https ssl certificate or not
- rewrite: Rewrite Address is a function that takes an address, modifies it, and returns it.