reactjsnode.jsnginxvitevps

Nginx doesn't seem to recognize index.html


I just bought VPS on Hostinger and i'm trying to deploy my app, my NodeJS server is working fine at ***, I have my ReactJS build folder in root /bushnaq/client/dist which contains index.html, but when i visit my domain at bushnaq.group, it says 404 not found, which is the fallback, as I understand, nginx doesn't recognize the path root /bushnaq/client/dist

bushnaq.group.conf

server {
    listen 80;
    server_name bushnaq.group;

    location / {
        root /bushnaq/client/dist;
        try_files $uri $uri/ /index.html =404;
    }

}

index.js

import express from "express";
import dbConnection from "./dbConnection.js";
import routes from "./routes.js";
import cors from "cors";
const app = express();
app.use(cors({ origin: "https://ashoshari.github.io/bushnaq-group/" }));

const port = process.env.PORT || 8000;
const host = process.env.HOST || "0.0.0.0";

// Middleware to parse JSON requests
app.use(express.json());

app.get("/", (req, res) => {
  res.send("Hello World!");
});
app.use("/api", routes);
app.use((err, req, res, next) => {
  err.statusCode = err.statusCode || 500;
  err.message = err.message || "Internal Server Error";
  res.status(err.statusCode).json({
    message: err.message,
  });
});

// If database is connected successfully, then run the server

dbConnection
  .getConnection()
  .then(() => {
    app.listen(port, host, () => {
      console.log(`Server is running on http://${host}:${port}`);
    });
  })
  .catch((err) => {
    console.log(`Failed to connect to the database: ${err.message}`);
  });


Solution

  • The problem was related to permissions, the nginx user (usually www-data) didn't has permissions to access the dist directory, so i had to change permissions for the project directory