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.


Install nginx (which is beyond the scope of this tutorial)
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;
}
}
}



Save it as /tmp/nginx.conf , and save the following content as /tmp/webdav/webdav.html :


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


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:


The standard nginx webdav module provides partial implementation and cannot be used with standard webdav clients.
To restrict upload and deletion to authorized users, try limit_except GET { auth_basic ... in the location directive.
Consider protecting the webdav.html itself from being overwritten.
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.

Top News