Generate Nginx routing configuration for static Next.js projects.
npm install next-nginx-routesGenerate Nginx routes configuration file for Next.js static HMTL export, also known as output: export.
This dependency-free NPM package provides a NPM script to convert all your Next.js routes like:
``json`
{
"page": "/[foo]",
"regex": "^/([^/]+?)(?:/)?$",
"routeKeys": { "foo": "foo" },
"namedRegex": "^/(?
}
... into Nginx routes like:
`nginx`
location ~ ^/([^/]+?)(?:/)?$ {
try_files /[foo].html /index.html;
}
Note: currently only Next.js pages router is fully supported.
`shell`
yarn add --dev next-nginx-routes
Make sure output: "export" is configured in your next.config.js file.
Add next-nginx-routes to your build script:
`json`
{
"build": "next build && next-nginx-routes"
}
And run Next.js export:
`shell`
yarn run build
Finally include the generated configuration file next-routes.conf in your Nginx site configuration file and make the Next.js out directory available to Nginx.
- Next.js 12 or higher
- Node.js 18 or higher
- See example` folder for a minimal Docker example.
- Read more about this approach in our blog post.