The original post: /r/nginx by /u/HolidayCartoonist323 on 2024-09-05 08:31:17.
I’m facing an issue with file uploads on my Node.js application hosted behind an Nginx server. The setup involves using the Express-Formidable package as middleware for handling file uploads, which are then sent to an AWS S3 bucket.
The problem is that the file upload request never completes—my API request continues processing until it hits the server timeout, and the file never reaches the S3 bucket.
When I checked the Nginx error logs, I found the following entry:
Nginx Error Log:
2024/09/04 18:32:44 [error] 63421#63421: *9345 upstream prematurely closed connection while reading response header from upstream, client: <my_ip>, server: <backend_api>, request: "POST /api/v1/video-project HTTP/2.0", upstream: "http://127.0.0.1:4000/api/v1/video-project", host: "<backend_api>", referrer: "<backend_api>"
Here’s my Nginx config for the server (relevant parts included):
server {
listen 443 ssl http2;
client_max_body_size 600M;
Proxy settings for the main API
location / {
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection ‘upgrade’;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_send_timeout 7200s;
proxy_read_timeout 7200s;
proxy_buffer_size 64k;
proxy_buffers 16 32k;
proxy_busy_buffers_size 64k;
proxy_request_buffering off;
proxy_buffering off;
proxy_connect_timeout 300;
}
}
What I’ve Tried:
- Checked the Nginx error logs but couldn’t find anything beyond the log above.
- Adjusted the client_max_body_size and proxy_timeout settings to handle larger files.
- Verified that the API works fine for smaller requests, but larger file uploads keep stalling.
Questions:
- Has anyone encountered similar issues with Nginx prematurely closing upstream connections during file uploads? What could be the root cause of this?
- Could this be a configuration issue with Nginx or something related to the Node.js Express-Formidable package or AWS S3 SDK?
- Any recommendations on how to debug or resolve this issue? Could this be related to buffer settings or timeout misconfigurations?
Any insights or suggestions would be highly appreciated!