question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SSR: Axios request fails with 404 when using a proxy

See original GitHub issue

I’m using Laravel Lumen for the backend, which is on a different domain, so I have to use a proxy to be able to share the cross-origin resource.

Everything works fine on the client-side, but when enabling universal mode and trying to get a prerendered site with the first request products are not rendered because Axios request fails with status code 404.

nuxt-ssr

After enabling Axios debugging I have noticed that ‘api’ string is still in the requested URL, but it shouldn’t be, because of the pathRewrite rule:

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

When modifying code like this (removed ‘api’ in the request path), Axios request works, but then of course doesn’t on the client.

nuxt-ssr nuxt-ssr

What can I do to make the request work on the client and server side?

Nuxt.js v2.12.1

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

5reactions
pjnicolascommented, Feb 5, 2021

Maybe it’s because the API_URL env variable is overriding the baseURL parameter. IDK if that’s what you want here. Try using a different name.

4reactions
nasirDoecommented, Jan 22, 2021

try to following this code bellow:

nuxt.config.js

modules: ['@nuxtjs/axios', '@nuxtjs/proxy'],
axios: {
proxy: true,
host: 'localhost',
prefix: '/api/',
headers: { //optional
    Accept: 'application/json',
    'Content-Type': 'application/json',
  }
},
proxy: {
    '/api/': {
      target: process.env.API_URL,
      pathRewrite: { '^/api/': '' },
    },
  },
Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue How to fix 404 error when using nuxt proxy and nuxt axios
In my vue -app I'm using the @nuxtjs/proxy together with @nuxtjs/axios but for some reason I don't know, I always get an Request...
Read more >
[axioserror: request failed with status code 404] - You.com
IMHO 404 means that the endpoint that you are looking for does not exist. In some cases, it also means that the method...
Read more >
Axios 404 Not Found - Azienda Roberto e Nicola Ercolani
SSR: Axios request fails with 404 when using a proxy #464. 404 Not Found error messages are frequently customized by individual websites. axios...
Read more >
Problem with Axios/Proxy : r/Nuxt - Reddit
Looking at the docs, you may need to set the 'prefix' option while using the proxy. https://axios.nuxtjs.org/options/#baseurl.
Read more >
Have a Axios error "Request failed with status code 404" in ...
Hi anyone, i am using nuxtjs for my project. But i have a problem, on mode ssr(server side render) when run project is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found