Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy nextjs into a directory which is not a root directory

Recently I learn something about nextjs because I want to make a site which SEO friendly by using React. Everything looks great until I ran into a problem.

It's about how can I deploy nextjs app into a directory which is not a root directory, for example '/next'. I use a simple config for nextjs, use the default node server listen to 3000 port and then use nginx for reverse.

next.config looks like :

enter image description here

nginx conf :

enter image description here

When I access localhost:8080/next I got the 404 page which is provided by nextjs and the css or js is also 404. It seems like the config of nextjs or nginx is wrong.

like image 750
wangcheng Avatar asked Sep 02 '25 04:09

wangcheng


1 Answers

Your current config tells nginx to map urls 1-to-1, that means that /next/what-ever will be mapped to http://localhost:3000/next/what-ever.

To solve this you need to add an additional slash.

server {
  ...
  location /next/ {
    proxy_pass http://localhost:3000/ // <----  this last slash tells to drop the prefix
  }
}

For more info read https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/#passing-a-request-to-a-proxied-server

like image 183
felixmosh Avatar answered Sep 04 '25 23:09

felixmosh