Table of Contents
Detailed explanation of the routing failure problem after Next.js static site is deployed to Nginx
Problem description
Problem analysis
Solution
Home Web Front-end JS Tutorial Why does Next.js static export fail when refreshing the page after being deployed on Nginx?

Why does Next.js static export fail when refreshing the page after being deployed on Nginx?

Apr 04, 2025 pm 03:48 PM
vue nginx Solution Why

Why does Next.js static export fail when refreshing the page after being deployed on Nginx?

Detailed explanation of the routing failure problem after Next.js static site is deployed to Nginx

After using Next.js static export to deploy to Nginx server, the route fails when refreshing the page and jumps back to the homepage, which is a common problem encountered by many developers. This article will explore the causes and solutions to this problem in depth.

Problem description

Suppose you have created a Next.js project, including a /test route, and configured with static export:

1

2

3

{

  "output": "export"

}

Copy after login

Local development environment (run npm run dev ) access localhost:3000/test displays normally. But after deploying to Nginx, access /test jumps to the homepage. Even if Nginx is configured with 404, jump to the homepage:

1

2

3

location / {

    try_files $uri $uri/ /index.html;

}

Copy after login

This is different from Vue.js' history mode, which usually works properly in similar configurations.

Problem analysis

When the Nginx's try_files directive cannot find the corresponding routed file in the static directory, it will fall back to index.html . This seems to be as expected, as the static export may not generate the static file for the corresponding route. However, the root cause is the difference between Next.js' static export mechanism and client routing processing methods.

Solution

To solve this problem, you need to start from the configuration of Next.js and Nginx:

  1. Next.js basePath configuration: If your Next.js application is deployed in a subpath (for example /my-app ), you need to configure basePath in next.config.js :

1

2

3

module.exports = {

  basePath: '/my-app',

}

Copy after login
  1. Nginx configuration optimization: Adjust Nginx's try_files instruction to make it handle routing more accurately:

1

2

location / {

   try_files $uri $uri/ /my-app/index.html; # If deployed under /my-app}

Copy after login

This will ensure that Nginx correctly points to the index.html file in the deployment directory when the specified routing file cannot be found.

  1. Use the server that comes with Next.js: Avoid using Nginx service static files directly. It is recommended to use the server that comes with Next.js and use Nginx as a reverse proxy. In this way, routing processing is done by Next.js server, avoiding the problem of static file matching.

  2. Check the generated static files: Make sure that the static export of Next.js has correctly generated all necessary routing files. You can re-export using the next export command and check the generated directory structure.

Through the above method, you can effectively solve the problem of Next.js static export route failure on Nginx, ensuring that your static website can correctly handle all routing requests. Which method to choose depends on your specific deployment environment and requirements. If possible, it is recommended to prioritize the use of Next.js' own server and reverse proxy, which can provide more reliable routing processing.

The above is the detailed content of Why does Next.js static export fail when refreshing the page after being deployed on Nginx?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to display child categories on archive page of parent categories How to display child categories on archive page of parent categories Apr 19, 2025 pm 11:54 PM

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

Why does the Spring project cause randomness problems due to circular dependencies when starting? Why does the Spring project cause randomness problems due to circular dependencies when starting? Apr 19, 2025 pm 11:21 PM

Understand the randomness of circular dependencies in Spring project startup. When developing Spring project, you may encounter randomness caused by circular dependencies at project startup...

Why is the rise or fall of virtual currency prices? Why is the rise or fall of virtual currency prices? Why is the rise or fall of virtual currency prices? Why is the rise or fall of virtual currency prices? Apr 21, 2025 am 08:57 AM

Factors of rising virtual currency prices include: 1. Increased market demand, 2. Decreased supply, 3. Stimulated positive news, 4. Optimistic market sentiment, 5. Macroeconomic environment; Decline factors include: 1. Decreased market demand, 2. Increased supply, 3. Strike of negative news, 4. Pessimistic market sentiment, 5. Macroeconomic environment.

How to solve the problem of printing spaces in IDEA console logs? How to solve the problem of printing spaces in IDEA console logs? Apr 19, 2025 pm 09:57 PM

How to solve the problem of printing spaces in IDEA console logs? When using IDEA for development, many developers may encounter a problem: the console printed...

Why is the return value empty when using RedisTemplate for batch query? Why is the return value empty when using RedisTemplate for batch query? Apr 19, 2025 pm 10:15 PM

Why is the return value empty when using RedisTemplate for batch query? When using RedisTemplate for batch query operations, you may encounter the returned results...

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? Apr 19, 2025 pm 08:03 PM

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

See all articles