I am trying to configure nginx to manage files upload for node.js app. I have followed this tutorial: https://coderwall.com/p/swgfvw/nginx-direct-file-upload-without-passing-them-through-backend
I have made it with the following configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /upload {
auth_request /upload/authenticate;
limit_except POST { deny all; }
client_body_temp_path /tmp/;
client_body_in_file_only on;
client_body_buffer_size 128K;
client_max_body_size 1000M;
proxy_pass_request_headers on;
proxy_set_header X-FILE $request_body_file;
proxy_set_body off;
proxy_redirect off;
proxy_pass http://localhost:3000/uploads;
}
location /upload/authenticate {
internal;
proxy_set_body off;
proxy_pass http://localhost:3000/auth/isAuthenticated;
}
}
And I did the test with Postman as follows:
The post request :
POST /upload HTTP/1.1 Host: localhost Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C Content-Disposition: form-data; name="image"; filename="pic.jpg" Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
It works fine and nginx uploads the image in the /tmp directory. The problem is that the file is renamed as "0000000001" and when I rename it manually as "pic.jpg" and try to open it the viewer prompts "Error interpreting JPEG image file (Not a JPEG file: starts with 0x2d 0x2d)".
And when I run the file command(file pic.jpg) it returns: "pic.jpg: data".