Home > Blockchain >  Unable to run NextJS with npm run build
Unable to run NextJS with npm run build

Time:05-24

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.

CodePudding user response:

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
    }
}

CodePudding user response:

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

  • Related