Node.js Express仅首页可访问问题排查:Plesk Obsidian环境下二级页面404错误
Problem Description
I'm running a Node.js Express app on Plesk Obsidian 18.0.37, but I'm facing an issue where only the root route (/) loads correctly. When I click the /page2 link on the homepage or manually enter the path in the address bar, it returns a 404 error. I've tried three different routing implementations, but none of them fixed the issue. Here's my code:
Basic Routing (app.js)
const express = require('express'); const app = express(); app.get("/", (request, response) => { response.send("<h1>Home page</h1>" + '<br><a href="/page2">Page 2</a>'); }); app.get("/page2", (request, response) => { response.send("<h1>Page 2</h1>"); }); app.listen(process.env.PORT);
Using sendFile for Static Files
const express = require('express'); const app = express(); app.get("/", (request, response) => { response.send("<h1>Home page</h1>" + '<br><a href="/page2">Page 2 with sendFile</a>'); }); app.get("/page2", (request, response) => { response.sendFile('page2.html'); }); app.listen(process.env.PORT);
Using Express Router
const express = require('express'); const app = express(); var router = express.Router(); router.get("/", (request, response) => { response.send("<h1>Home page with router</h1>" + '<br><a href="/page2">Page 2</a>'); }); router.get("/page2", (request, response) => { response.send("<h1>Page 2 with router</h1>"); }); app.use(router); app.listen(process.env.PORT);
What's causing this issue, and how can I fix it?
Solution
This is almost certainly a Web server (Apache/Nginx) configuration issue in Plesk, not a problem with your Express code. By default, Plesk lets the web server handle requests first, and only forwards root path requests to your Node.js app. All other paths are treated as static resources or non-existent routes, resulting in a 404. Here's how to resolve it:
1. Enable "Pass Through" for Your Node.js App
Go to your domain's settings in Plesk → navigate to the Node.js page:
- Locate the Pass Through option and make sure it's checked. This setting forwards all requests (not just the root path) directly to your Node.js app, letting Express handle routing.
- Save the settings and restart your Node.js app (Plesk usually does this automatically, but a manual restart ensures changes take effect).
2. Verify the Document Root Configuration
Still in the Node.js settings page:
- Confirm the Document Root points to the root directory of your Express app (e.g.,
/var/www/vhosts/yourdomain.com/httpdocs), not a subdirectory likepublic. If it points to a static file directory, the web server will prioritize static files over your Express routes.
3. Fix the sendFile Path in Your Second Implementation
While this isn't the main cause of the 404, using a relative path with sendFile can lead to other issues. Use an absolute path instead:
const path = require('path'); // ... app.get("/page2", (request, response) => { response.sendFile(path.join(__dirname, 'page2.html')); });
__dirname ensures you're referencing the directory where your script is located, avoiding path resolution errors.
4. Configure Custom Rewrite Rules (If You Don't Want Global Pass Through)
If you need the web server to handle some static resources directly, add rewrite rules in Plesk's Apache & Nginx Settings:
- For Nginx: Add this to
Additional nginx directives:
location / { try_files $uri $uri/ @nodejs; } location @nodejs { proxy_pass http://127.0.0.1:YOUR_NODE_PORT; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }
Replace YOUR_NODE_PORT with the port your Node.js app uses (you can find this in the Node.js settings page).
- For Apache: Add this to
Additional Apache directives:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ http://127.0.0.1:YOUR_NODE_PORT/$1 [P,L]
After applying these changes, your Express routes should handle all path requests correctly.
内容的提问来源于stack exchange,提问作者Patrick Miah

