Nginx – How To Serve Static File Directory Over HTTP Using NGINX

httpnginx

Problem

A server with NGINX has a directory with files it needs to serve over HTTP.

The directory is located at (example): /media/user/data

Question

What configuration is required in NGINX to serve that directory (and all its files and sub-folders) over HTTP?

The served directory over HTTP must be fully accessible and any users should be able to browse through all of the directory's contents.

All steps for configuration required.

Research

Research on the topic found changes will be required in the default file within the sites-available directory inside the /etc/nginx folder as such:

 location /data {
    root /media/user/;
    autoindex on;
  }
}

Other

From recent comments to proposed answers:

The default file in sites-available was edited to include the
directory, then the server was restarted and verified to be capable of
serving by visiting 127.0.0.1 which displayed the standard nginx
congratulations page. Then when attempting to access
http://127.0.0.1/data the server produced a 403 Forbidden error. –
Frugal Rasin

Best Answer

You can achieve that by editing existing Nginx virtualhost (the default one, that you mentioned). Just make sure that /media/user/data directory and all the content inside that directory are readable by the user under Nginx service is started (most probably "nginx" user).

If you want to host those files under different (sub)domain, you can create new Nginx virtualhost, with content like:

server {
  listen *:80;
  server_name example.com www.example.com;

  root /media/user/data;
  autoindex on;
}

Update: from the comments below, it was also necessary to modify directory permissions so that Nginx can serve the content and not to return 403 forbiden error.

Although the permissions of /media/user/data/ directory were good, /media/ and /media/user/ directories were missing executable permissions. The problem was solved with the following command:

sudo chmod o+x /media/ /media/user/