skip to Main Content

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


  1. Chosen as BEST ANSWER

    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


  2. 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

    server {
        server_name example.com;
        listen 443 http2 ssl;
        listen [::]:443 http2 ssl;
    
        ssl_certificate /etc/nginx/certs/fullchain.cer;
        ssl_certificate_key /etc/nginx/certs/fullchain.key;
    
        location / {
    
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_http_version 1.1;
                proxy_pass http://127.0.0.1:8000; //your next app
        }
    }
    
    Login or Signup to reply.
  3. 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 …)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search