I recently deployed a react static app on my digitalocean server. All I did was show the root directory of the build file in the Nginx config file. And it’s running well.
Now I am trying to host a NextJS app on my server. I showed the root directory root /var/www/html/NextJSTestApp/.next
I even tried with root /var/www/html/NextJSTestApp/node_modules
When I am running the npm run build
the build is done then I tried to reach my server it’s showing "502 Bad Gateway"
But when I am running npm start
site image
The app is showing
What I can do to run it all the time? I am using digitalocean droplet.
3
Answers
I found this solution. To run this project background I used pm2.
sudo npm install -g pm2
Then gone to my project folder
cd website/
pm2 start --name=website npm -- start
then
pm2 startup systemd
I followed this tutorial
When running NextJS as a node application (e.g. next start), you need to set nginx to use
proxy_pass
instead of pointing it to a root directory (omit root directive).Example nginx location block
First, Manually running
next start
does not persist the process once you close the terminal or leave the bash. In order to let it run in the background, you may use && at the end of start command (next start &&) or create a systemd file (recommended).Second, Once you are done with a nextJs running on background, you can go ahead and setup a proxy pass to your web server(nginx, apache …)