proxy-module icon indicating copy to clipboard operation
proxy-module copied to clipboard

Proxy with API_URL env var

Open igolka97 opened this issue 5 years ago • 0 comments

I have nuxt frontend and api backend on 1337 port.

I'm trying to proxy http://*nuxtapp*/api/* requests to http://localhost:1337/ Example http://*nuxtapp*/api/products to http://localhost:1337/products

When I'm using API_URL env var, as was mentioned here or here (as 'already practiced solution') then my requests are proxies to http://localhost:1337/api/products instead http://localhost:1337/products

But if I change var name to something another, like API_TARGET then it works as expected.

nuxt.config.js:

axios: {
  proxy: true,
},

proxy: {
  '/api/': {
    target: process.env.API_URL || 'http://localhost:1337',
    pathRewrite: { '^/api/': '' },
    changeOrigin: true,
  },
},

I already looked around this problem:

  • when I set config.axios.baseUrl then config.proxy stop working at all.
  • from axios official docs: Environment variable API_URL can be used to override baseURL
  • from axios official docs: WARNING: baseURL and proxy cannot be used at the same time

Okay, if I understand, using baseURL or API_URL totally disabling proxy feature (or, pathRewrite feature, like in this case) and I have to use any another env var. But why it works in mentioned cases (links on top of this letter)? Or, what I exactly need to do in this case?

igolka97 avatar Dec 30 '20 23:12 igolka97