13

is there a way to upload files to a server via http using nginx?

I have a program that basically uses curl and the POST method via http to send files to a completely different enterprise software. We want to replicate something similar but are hitting a roadblock.

I have installed nginx and did a basic configuration, I want to be able to upload the files under /data/files/incoming

server {
    listen testserv1;

    location /upload/ {
      limit_except POST PUT { deny all; }
      client_body_temp_path /data/files/incoming/;
      client_body_in_file_only on;
      client_body_buffer_size 128k;
      client_max_body_size 100M;
      proxy_pass_request_headers on;
      proxy_set_body $request_body_file;
      proxy_pass http://127.0.0.1/upload;
      proxy_redirect off;
    }

I basically left the regular config on the nginx.conf and added the above. So my question is, how do I know is actually working? I did from another server try to run the program that supposedly POSTs a file but nothing. is there also a way to test this config from another system? am I missing something on the config?

Any help, anything is much appreciated.

user3311890
  • 321
  • 1
  • 3
  • 10

2 Answers2

6

You can do this without using any external module, by specifying filename into URL. I tested without the proxy, but it should be able to work :

server {
    listen testserv1;

    location ~ "/upload/([0-9a-zA-Z-.]*)$" {
        dav_methods  PUT DELETE MKCOL COPY MOVE;
        client_body_temp_path /tmp/incoming;
        alias     /data/files/incoming/$1;
        create_full_put_path   on;
        dav_access             group:rw  all:r;

        client_body_in_file_only on;
        client_body_buffer_size 128k;
        client_max_body_size 100M;
        proxy_pass_request_headers on;
        proxy_set_body $request_body_file;
        proxy_pass http://127.0.0.1/upload;
        proxy_redirect off;
    }
}

And use : curl -T test.zip http://testserv1/upload/text.zip

Clément Mondon
  • 101
  • 1
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Neeraj Dec 21 '21 at 09:49
  • Thank you for providing an answer without the need an external module! Not sure why half of stackoverflow is pointing to the upload module when a nice built in solution exists. Hint: you forgot to close a `}`. – fiz Apr 24 '22 at 20:05
  • Hey! Thanks for sharing this ) Is it possible to make the target endpoint without the filename? e.g. just location /upload { ... alias /data/files/incoming; } – s0mbre Jul 25 '22 at 04:01
  • this doesn't work due to this: https://stackoverflow.com/questions/53353572/proxy-pass-cannot-have-uri-part-in-location-given-by-regular-expression -- it needs `proxy_pass http://127.0.0.1/upload/$1;` – ekkis Feb 16 '23 at 00:55
  • however, even when I fix it, trying the curl produces this `404 Not Found` – ekkis Feb 16 '23 at 01:01
2

Have a look at the nginx_upload_module: https://www.nginx.com/resources/wiki/modules/upload/

Alex Popov
  • 3,726
  • 1
  • 17
  • 20
  • Just a heads-up that some Linux distros, like Alpine, do not have this module compiled into their Nginx package. You may need to set up the Nginx repos for your package manager according to https://nginx.org/en/linux_packages.html, or build it from source. Some package managers may provide a separate package for this module, but be prepared for the possibility that yours does not. – pyansharp Oct 29 '21 at 18:52