Server proxy
How to use your Nuxt app as a proxy for Sanctum API requests.
When using SSR, it's often convenient to serve all operations under a single domain. You can achieve this by enabling the server proxy catch-all feature, which forwards client requests to your Laravel API while preserving cookies and headers.
To enable it, update your nuxt.config.ts
:
export default defineNuxtConfig({
modules: ['nuxt-auth-sanctum'],
ssr: true,
sanctum: {
baseUrl: '/api/sanctum',
serverProxy: {
enabled: true,
route: '/api/sanctum',
baseUrl: 'http://api.frontend.dev',
},
},
})
Once serverProxy.enabled
is set to true
, Nuxt adds a server route at: http://frontend.dev/api/sanctum
which is defined as serverProxy.route
parameter.
Note that sanctum.baseUrl
is now /api/sanctum
(a local path), while serverProxy.baseUrl
points to your Laravel backend. This setup tells Nuxt where to forward requests internally.
You can test the proxy using any helper like useSanctumFetch
:
// Request URL = http://frontend.dev/api/sanctum/user/profile
// Actual URL = http://api.frontend.dev/user/profile
const { data } = await useSanctumFetch('/user/profile')
The catch-all route (/api/sanctum
) is stripped from the final proxied URL. If needed, you can customise this behaviour by modifying serverProxy.baseUrl
.
Last updated