1

We have hosted next JS app in Cloudways Linode server via pm2. The page is working properly in localhost but showing 404 error in server. The screenshot of the page is as below: enter image description here

The .htacess file is as below:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)?$ http://127.0.0.1:3002/$1 [P,L]

The package json is as below:

{
  "name": "sb",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev -p 3002",
    "build": "next build",
    "start": "next start -p 3002",
    "lint": "next lint"
  },
  "dependencies": {
    "next": "13.0.0",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  },
  "devDependencies": {
    "eslint": "8.26.0",
    "eslint-config-next": "13.0.0"
  }
}

Kindly let us know what changes has to be done to make next JS app show index.js page

We tried npm export, changing .htaccess file and changing port but it is still showing 404 error. I also tried creating test.js page inside root folder and opening in browser it is running properly. But next JS index page not showing.

N.SH
  • 616
  • 7
  • 20

1 Answers1

1

I found solution that worked for me (2023-02-15) I'm also using nextjs on cloudways

Edit your .htaccess to

DirectoryIndex disabled
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:3002/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:3002/$1 [P,L]
ket-c
  • 211
  • 1
  • 10
  • can you explain a little bit. didn't you make a production build? i mean used "npm run build" command? or used "npm run dev" if use dev then where did you put the running script. Or at least can you explain step that you've taken from localhost to live server? – Alauddin Ahmed Feb 28 '23 at 15:11
  • 1
    You just have to upload the codes to the server as you have it on your local. Then Edit/create **.htaccess** file and put the above codes inside (Remember to replace the port with your own port [default port is usually **3001**]) The run ```npm run build``` and ```npm run dev``` but when you close the terminal, the app will go off. So what you have to do is to install **pm2** on the server (contact cloudways to install it for you) Then you will use the pm2 to serve the app so that it can still run on the background – ket-c Mar 01 '23 at 00:04