nginx pass filetype to backend server [closed]
Asked Answered
G

3

6

I am trying to setup nginx to handle file uploads and pass the file information on to a backend server once done. I came across a post at https://coderwall.com/p/swgfvw that shows how to do this and I am able to see a file being uploaded to the /tmp directory. However I would like to also pass on the file name and type (Content-Disposition and Content-Type) to the backend server.

I tried capturing what is received at the http server port and see the below,

POST /upload HTTP/1.1
User-Agent: curl/7.32.0
Host: MyHostName
Accept: */*
Content-Length: 4431
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------6060af4f937c14c9

--------------------------6060af4f937c14c9
Content-Disposition: form-data; name="filedata"; filename="sessions.txt"
Content-Type: text/plain

followed by the data.

My nginx location block for upload is,

        location /upload {
                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            100M;

                proxy_redirect                  off;
                proxy_set_header                X-FILE $request_body_file;
                proxy_set_header                X-Forwared-For  $proxy_add_x_forwarded_for;
                proxy_set_header                Host    $http_host;
                proxy_set_header                X-NginX-Proxy   true;
                proxy_set_header                Connection "";
                proxy_pass_request_headers      on;
                proxy_set_body                  off;
                proxy_http_version              1.1;
                proxy_pass                      http://my_backend;
        }

With this I am able to pass on and receive the following at my backend,

'content-type': 'multipart/form-data; boundary=------------------------6060af4f937c14c9'
'x-file': '/tmp/0000000001'

but would really like to know how I can get the

Content-Disposition: form-data; name="filedata"; filename="sessions.txt"
Content-Type: text/plain

to my backend. Any help with this is much appreciated.

P.S: hope its ok for this question here? (tried superuser but it doesn't seem to have much activity)

Gretta answered 24/4, 2014 at 9:57 Comment(4)
Did you ever have any luck with this? Is the Content-Disposition headers at the top of the file expected behavior? And if so, do you later strip them out in order to display the file (image) or instance?Bremsstrahlung
Actually no, I decided against using this approach for now but do think parsing the file at the upstream server would be one way to get this to work (will probably try something like that when I get the chance).Gretta
I also wonder if anyone had any luck with this...Brownstone
me too, seems like a common requirement. the nginx upload module seems to offer this functionality but requires a special build due to the nginx core team belief that their offering is sufficientSixpence
U
0

if the header is being ignored, try

proxy_pass_header    Content-Disposition;

or directly pass

proxy_set_header     Content-Disposition $http_content_disposition;
Unruh answered 24/4, 2014 at 17:11 Comment(1)
Tried them both but still don't receive a Content-Dispostion at the backend server :(Gretta
P
0

The underscores in custom headers are silently ignored in nginx, a option that might help

underscores_in_headers on;

Praemunire answered 18/1, 2018 at 1:5 Comment(0)
R
0

Replace proxy_set_body off; to proxy_set_body on;

Rotatory answered 16/8, 2024 at 10:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.