1

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:

Upload post request

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".

2
  • have u tried saving it in some other folder rather than temp and then try renaming it? Commented Jan 1, 2016 at 15:55
  • Thank you for the reply, yes I did and I get the same problem. Commented Jan 1, 2016 at 17:24

1 Answer 1

3

Could you check the Postman version? In my environment, Postman(v3.2.8) has "binary" radio button on request method.

According the blog post, "clientbodyinfileonly" method is incompatible with multi-part data and supports binary data upload only.

So please retry request with binary mode(Postman or another method, e.g. XHR2).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, like you have said, previously I was using multi-part, which is not supported by clientbodyinfileonly module. I have upgraded postman and I've used the binary type, now after I rename the file I can open it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.