Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set proxy in Nuxt3?

I try to start a nuxt3 program, now I want to set server proxy. A request to http://localhost:3000/api/v1 is supposed to return a response from our backend server on http://39.98.58.238:8594 , but now it gives me a 404 page.

At first, I follow the vite.js docs to set nuxt.config.js file

nuxt.config.js

export default defineNuxtConfig({  
  ...  
  vite: {
    server: {
      proxy: {
        '/api': {
          target: 'http://39.98.58.238:8594',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      },
    }
  },
})

page

<script setup>
  async function test() {
   await usefetch('/api/v1/xxx')
  }
</script>
<template>
  <div>
    <button @click="test">check</button>
  </div>
</template>

It didn't work, my request returned a 404 page. Then I try to follow this issue: text, do not use vite proxy

nuxt.config.js

export default defineNuxtConfig({
  nitro: {
    devProxy: {
        '/api/': {
            target: 'http://39.98.58.238:8594/',
            changeOrigin: true
        }
    }
  }
})

But it's still not work. What can I do to solve the problem? Thanks!!!

like image 529
kant Avatar asked Jan 23 '26 06:01

kant


1 Answers

For those who are still wondering how to do this in production mode. I believe the answer is

export default defineNuxtConfig({
  nitro: {
    routeRules: {
      '/proxy/example': { proxy: 'http://39.98.58.238:8594' },
      "/proxy/**": { proxy: '/api/**' },
    }
  }
})

You can find the docs here ⚠️ EXPERIMENTAL!

like image 75
Kwarkjes Avatar answered Jan 25 '26 23:01

Kwarkjes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!