Share files between two computers using NGINX server

Share files between two computers using NGINX server

·

3 min read

I found myself in a situation where I wanted to transfer some large video files from one laptop to the other and didn't have a portable hard disk or flash storage around. I postponed the task until I realized after using live server to view a website I was developing on another computer that I could also serve the folder with the video files over the internet and simply access it on the laptop where I wanted the files transferred to and download the files.

So off I went to look for a solution that would let me quickly serve a folder over my local wifi connection, the easiest method I stumbled on is an npm package called http-server. simply install it with the command npm i -g http-server and then type http-serve while in the folder you want to access over the local network.

After installing http-server and then activating it by typing http-server in my downloads folder, it fires up and listens on port:8080. I can then go to the browser on the second laptop and type in the ip address of the main laptop with the running server followed by the port number and I can access everything in the downloads folder of the main laptop from the second laptop.

http-server worked well but I wanted the folder with the video files to be always accessible as opposed to always having to type http-server whenever I wanted to access files. One way I could do this is by creating a linux systemd service that always starts http-server for me, or I could set up Nginx and have it quietly serve the video folder over the local (wifi) network in the background. I went with the Nginx option.

I installed nginx and created a file in /etc/nginx/sites-available called fileserver with this config

server {
        listen 7777;
        server_name localhost;

        location / {
                autoindex on;
                root /home/shasa/Videos;
        }


        location /downloads {
                autoindex on;
                alias /home/shasa/Downloads;
        }
}

After editing the config I created the symbolic link as per nginx custom

sudo ln -s /etc/nginx/sites-available/fileserver /etc/nginx/sites-enabled/fileserver

And then restarted nginx

sudo systemctl reload nginx

With the nginx config, nginx will always run on my main laptop and make the contents of the downloads and videos folder accessible over the local network, any device logged into the same wifi network can navigate to the ip address of the laptop on port 7777 and access downloads and videos.

If the ip address of my main laptop with the nginx server is 192.168.8.3 typing http://192.168.8.3:7777/downloads/ in the browser of the second laptop brings up the following, I can view and download any file in Downloads, no fiddling with flash disks!!