I have deployed NextJS app on the ubuntu server. When I try to start the app using pm2 start npm --name myapp -- start, I can see that the status of my app is online. When I try to open my website in the browser, It shows me 502 Bad Gateway nginx/1.18.0 (Ubuntu). When I ran pm2 logs command and checked the logs of pm2, I can see that It is displaying error message inside infinite loop & the error states that:
1|myapp | > sample-app-nodejs@1.0.0 start
1|myapp | > next start
1|myapp | ready - started server on 0.0.0.0:3000, url: http://localhost:3000
1|myapp | Error: Could not find a production build in the '/var/www/myapp/.next' directory. Try building your app with 'next build' before starting the production server.
So according to the above message, It seems obvious that there is no .next build inside my /var/www/myapp directory. But I have performed npm run build inside my app & upon listing the files & folders of my app, I can definately see that there is definitely a .next build inside my app as you can see below.
total 1008
4 drwxrwxrwx 14 root root 4096 Sep 22 11:56 .
4 drwxr-xr-x 4 root root 4096 Sep 22 11:02 ..
4 -rwxrwxrwx 1 root root 623 Sep 22 11:01 .env.example
4 drwxrwxrwx 8 root root 4096 Sep 22 11:01 .git
4 -rwxrwxrwx 1 root root 1609 Sep 22 11:01 .gitignore
4 drwxrwxrwx 5 root root 4096 Sep 22 12:02 .next
4 -rwxrwxrwx 1 root root 153 Sep 22 11:01 CHANGELOG.md
4 -rwxrwxrwx 1 root root 3224 Sep 22 11:01 CODE_OF_CONDUCT.md
4 -rwxrwxrwx 1 root root 1743 Sep 22 11:01 CONTRIBUTING.md
4 -rwxrwxrwx 1 root root 1068 Sep 22 11:01 LICENSE
4 -rwxrwxrwx 1 root root 2379 Sep 22 11:01 README.md
4 -rwxrwxrwx 1 root root 1112 Sep 22 11:01 app.json
4 drwxrwxrwx 2 root root 4096 Sep 22 11:01 assets
4 drwxrwxrwx 2 root root 4096 Sep 22 11:01 components
4 drwxrwxrwx 2 root root 4096 Sep 22 11:01 constants
4 drwxrwxrwx 2 root root 4096 Sep 22 11:01 context
4 -rwxrwxrwx 1 root root 558 Sep 22 11:01 jest.config.js
4 -rwxrwxrwx 1 root root 150 Sep 22 11:01 jest.setup.ts
4 drwxrwxrwx 3 root root 4096 Sep 22 11:01 lib
4 -rwxrwxrwx 1 root root 201 Sep 22 11:01 next-env.d.ts
24 drwxrwxr-x 474 ubuntu ubuntu 20480 Sep 22 11:42 node_modules
880 -rwxrwxrwx 1 root root 900511 Sep 22 11:42 package-lock.json
4 -rwxrwxrwx 1 root root 1623 Sep 22 11:51 package.json
4 drwxrwxrwx 3 root root 4096 Sep 22 11:01 pages
4 drwxrwxrwx 2 root root 4096 Sep 22 11:01 scripts
4 drwxrwxrwx 4 root root 4096 Sep 22 11:01 test
4 -rwxrwxrwx 1 root root 909 Sep 22 11:01 tsconfig.json
4 drwxrwxrwx 2 root root 4096 Sep 22 11:01 types
And here is .next directory content
4 drwxrwxrwx 5 root root 4096 Sep 22 12:02 .
4 drwxrwxrwx 14 root root 4096 Sep 22 11:56 ..
4 -rw-rw-r-- 1 ubuntu ubuntu 749 Sep 22 12:02 build-manifest.json
4 drwxrwxr-x 4 ubuntu ubuntu 4096 Sep 22 11:43 cache
4 -rw-rw-r-- 1 ubuntu ubuntu 20 Sep 22 12:02 package.json
4 -rw-rw-r-- 1 ubuntu ubuntu 2 Sep 22 12:02 react-loadable-manifest.json
4 drwxrwxr-x 3 ubuntu ubuntu 4096 Sep 22 12:02 server
4 drwxrwxr-x 5 ubuntu ubuntu 4096 Sep 22 12:02 static
36 -rw-rw-r-- 1 ubuntu ubuntu 35793 Sep 22 12:02 trace
Here is my nginx configuraation
server {
listen 80;
server_name MY_UBUNTU_IP_ADDRESS_IS_HERE
gzip on;
gzip_proxied any;
gzip_types application/javascript application/x-javascript text/css text/javascript;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_min_length 256;
location /_next/static/ {
alias /var/www/myapp/.next/static/;
expires 365d;
access_log off;
}
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}