12

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

Frugal Rasin
  • 143
  • 1
  • 1
  • 5

2 Answers2

18

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/

Tubeless
  • 1,640
  • 14
  • 15
  • 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 Nov 21 '15 at 22:06
  • You are getting `403 forbidden` error most probably because of the wrong permissions. Make sure that `/media/user/data` is readable by nginx user. You can verify that by checking current permissions with `ls -la /media/user/data`. – Tubeless Nov 21 '15 at 22:15
  • The file permissions on /media/user/data were set by the following command as root: `chmod 777 /media/user/data` but this still produced the `403 Forbidden` error. – Frugal Rasin Nov 21 '15 at 22:18
  • What does the Nginx error log say? The error log is most probably located in `/var/log/nginx/` directory. – Tubeless Nov 21 '15 at 22:20
  • 1
    `2015/11/21 22:22:40 [error] 10311#0: *2 open() "/media/user/data" failed (13: Permission denied), client: 127.0.0.1, server: _, request: "GET /data HTTP/1.1", host: "127.0.0.1"` – Frugal Rasin Nov 21 '15 at 22:24
  • Either `/media/` or `/media/user/` directories have very strict permissions, which forbid Nginx to reach `/media/user/data`. Which operating system do you run? It's possible that Apparmor or SElinux are forbidding access to that directory. – Tubeless Nov 21 '15 at 22:31
  • The OS is debian. Not certain nginx (`www-data`) would need recursive access to `/media` and `/media/user`. – Frugal Rasin Nov 21 '15 at 22:40
  • It should. You don't have to and you shouldn't chmod everything to 777 recursively. It's safe to give ony the execute (x) permissions to both `/media/` and `/media/user/` directories with `sudo chmod o+x /media/ /media/user/` so that `www-data` user can reach `/media/user/data` directory. – Tubeless Nov 21 '15 at 22:43
  • Okay. That worked. You can edit your answer to reflect the troubleshooting you did, after which I will mark it as the correct answer. Also, the directory `data` should not be written twice in both the `root` option, and the `location` option. I will also update my own question to reflect that new information. – Frugal Rasin Nov 21 '15 at 23:00
  • Great. I'm glad you solved the problem. Regarding the path to `data` directory, you can have it in both `server` and `location` block simultaneously, but in your case it's redundant. – Tubeless Nov 21 '15 at 23:15
0

To just serve a single directory and subdirectories in NGINX, the easiest way is to make the following changes in your nginx.conf file:

remove the line that adds the "sites-enabled" directory: the one with the following contents:

    include /etc/nginx/sites-enabled/*;

and in the same place put the following:

    server {
            listen 80 default_server;
            listen [::]:80 default_server;
            server_name _;
            root /path/to/the/dir/you/want/to/serve;
            autoindex on;
    }
Daniel K
  • 649
  • 1
  • 4
  • 16
Dan Ortega
  • 103
  • 4