NuxtN
Nuxt4mo ago
11 replies
Meldiron

Static files with Nuxt (Nitro) handler preset

When self-hosting Nuxt build, by default it uses node-server preset. This gives an entrypoint that runs on a port and serves website. All works well in this setup.

I wanted to add more logic around this, such as authorization headers, error handling, metrics tracking, and more. So I switch to node-listener preset, but static files stops working, like robots.txt in public directory.
I went the simple route and simply added static serving on my own, from public directory.

This worked well, until I faced problem with nitro configuration in
nuxt.config.ts
, for example this one:
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  compatibilityDate: '2025-07-15',
  devtools: { enabled: true },
  nitro: {
    routeRules: {
      "/img/**": { headers: { "cache-control": "public,max-age=6666,s-maxage=6666" } },
      "/_nuxt/**": { headers: { "cache-control": "public,max-age=6666,s-maxage=6666" } },
    }
  },
})


Notice I have images, and I want then cached for 6666 seconds.
When I build this as node-server, my img/sample-3.png has correct cache control headers.

When I build for node-listener, they dont get served (shows index HTML instead). When I add my own static serving, it doesnt respect nitro routeRules, so headers dont get added.

---

What is recommended solution for this problem?

A) Is there a way to tell node-listener to also serve static files?
B) When serving static files on my own, are there any helpers to use to respect those nitro configurations?
Was this page helpful?