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 and making fetch async), 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
  • Reactions:2
  • Comments:10

github_iconTop GitHub Comments

3reactions
juretopolakcommented, Apr 29, 2021

@bagaskarala .env variable named API_URL was causing me problems since it overrides baseURL config of ‘@nuxtjs/axios’. I prepared a demo project when I was looking for help and updated it with a “solution”: https://github.com/juretopolak/nuxt-axios-proxy-ssr-demo

@xlanex6 if you also named your .env variable API_URL maybe you are facing the same problem.

3reactions
bagaskaralacommented, Apr 29, 2021

i’m also facing this issue, then i’ve resolved it. To call backend api, set this env API_URL: http://backend.com. then i use this setting:

 axios: {
    proxy: true,
    prefix: '/api/'
  },

  proxy: {
    '/api/': {
      target: API_URL,
      pathRewrite: { '^/api/': '' }
    }
  },

i proxied request HOST/api to actual backend url, it will work on server and client slide request. note: HOST can be your localhost (dev) or your frontend url (production)

Read more comments on GitHub >

github_iconTop Results From Across the Web

SSR: Axios request fails with 404 when using a proxy · 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...
Read more >
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 >
Problem with Axios/Proxy : r/Nuxt - Reddit
I am using the nuxtjs/axios and nuxtjs/proxy modules with the ... (which fails with 404) instead of api.example.com/product/getProduct.
Read more >
[Solved]-Proxy not hitting in Nuxt Axios causing CORS-nuxt.js
while proxying you have to call it below way async fetch() { const data = await this.$axios.$get( '/listingapi/api/export', { credentials: true, ...
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