Skip to content

Proxy with API_URL env var #88

@igolka97

Description

@igolka97

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions