Solution:
Root Cause: Node.js Framework Body-Parser Limits
When Nginx proxies request streams to Node.js, Nginx passes requests up to
client_max_body_size. However, frameworks like Express.js default to a
100kb limit via internal middleware (
body-parser). If exceeded, Express responds with its own HTTP 413 error payload (
PayloadTooLargeError: request entity too large).
# Diagnostic Verification:
Examine Node.js application process logs:
bash
pm2 logs --lines 50
# or
journalctl -u my-node-app.service -n 50
Look for
PayloadTooLargeError: request entity too large stack traces.
# Step-by-Step Fix:
1. Update Express middleware body-parsing definitions in your application entry point (
server.js or
app.js):
javascript
const express = require('express');
const app = express();
// Increase JSON payload limits
app.use(express.json({ limit: '50mb' }));
// Increase URL-encoded payload limits
app.use(express.urlencoded({ limit: '50mb', extended: true }));
2. If using file upload middleware (e.g.,
express-fileupload or
multer):
javascript
const fileUpload = require('express-fileupload');
app.use(fileUpload({
limits: { fileSize: 50 * 1024 * 1024 }, // 50MB
}));
3. In your Nginx reverse proxy configuration, align
client_max_body_size:
nginx
location /api/ {
proxy_pass [http://127.0.0.1:3000](http://127.0.0.1:3000);
client_max_body_size 50M;
}
4. Restart the Node application and reload Nginx:
bash
pm2 restart all
sudo systemctl reload nginx
# Prevention & Long-Term Monitoring:
Explicitly define explicit middleware limit exceptions on dynamic upload routes instead of blanket high payload thresholds.