Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion

Minimum static file hosting with nginx
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Minimum static file hosting with nginx

In this tutorial I am going to show how to setup a minimum static file hosting service that allows random users to upload, download, list and delete files with a browser. The only requirement is nginx on the server, and a web browser on the client side. It has very few functions, but it may come in handy for some quick, random or temporary file hosting.

It uses the standard webdav and autoindex module shipped with nginx, no plugins or CGI required. The client is a very simple HTML, which uses plain JavaScript (XHR) to upload and download. It's 50 lines of code, no bloat JavaScript library or framework. I wrote it because I was unable to find an absolute minimum webdav client that works like this.

The following commands are fully functional examples. It puts everything in /tmp and assumes the directory for uploaded files is /tmp/webdav. It can and should be run with an unprivileged user.

  1. Install nginx (which is beyond the scope of this tutorial)

  2. This is a minimum example of full nginx configuration:

pid /tmp/nginx.pid;
events {worker_connections  1024;}

http {
    default_type application/octet-stream;
    server {
        listen 8080;
        location / {
            root /tmp/webdav;
            charset utf8;
            autoindex on;
            dav_methods PUT DELETE;
            client_max_body_size 1G;
        }
    }
}
  1. Save it as /tmp/nginx.conf, and save the following content as /tmp/webdav/webdav.html:

https://gist.github.com/trimsj/1d6202e29e7550c7473b60ddbf67296e

  1. Run nginx nginx -c /tmp/nginx.conf and go to http://127.0.0.1:8080

For upload and deletion, navigate to webdav.html. For uploads, there are no fancy progress bars. A popup will be shown for each file uploaded.

Some notes:

  1. The standard nginx webdav module provides partial implementation and cannot be used with standard webdav clients.
  2. To restrict upload and deletion to authorized users, try limit_except GET { auth_basic ... in the location directive.
  3. Consider protecting the webdav.html itself from being overwritten.
  4. You can disable or restrict autoindex so that guests don't know existing files. Users will be notified of the URL for files they uploaded.

Comments

  • JustPfffJustPfff Member
    edited September 2019

    thanks for the tutorial but I was need to make bit of changes

    pid /tmp/nginx.pid;
    events {  worker_connections  1024;  }
     error_log  /tmp/upluadtest/error.log;
    
    http {
        default_type application/octet-stream;
        server {
            listen 8080;
            location / {
                root /tmp/upluadtest/;
                charset utf8;
                autoindex on;
                dav_methods PUT DELETE;
                client_max_body_size 1G;
            }
     access_log  /tmp/upluadtest/access.log   combined if=$http_x_fb_http_engine;
    
        }
    }
    

    Anyway the web-page show me http 500 error maybe because Nginx user permission

Sign In or Register to comment.