Nuxt3中如何使用proxy代理伺服器?

設定在nuxt.config.ts


核心

  • 設定寫在nuxt.config.ts中的vite屬性
  • 記得要「rewrite」路徑
  • 請用http而非https

實作

  • nuxt.config.ts

    import { defineNuxtConfig } from 'nuxt'; // https://v3.nuxtjs.org/docs/directory-structure/nuxt.config export default defineNuxtConfig({ vite: { server: { proxy: { '/api-main': { target: 'http://your-api-here', ws: true, changeOrigin: true, rewrite: (path) => path.replace(/^\/api-main/, ''), // rewrite這個proxy的路徑 }, }, }, }, });

REF

https://stackblitz.com/edit/nuxt-starter-g2m6nk?file=README.md

https://github.com/nuxt/framework/discussions/1223

繼續閱讀 Related posts

目錄