Home > Net >  nginx: Index file outside of the document root
nginx: Index file outside of the document root

Time:12-05

I'm trying to serve a directory of user-provided files with nginx, but with an index file that is outside of the document root. The reason I don't want to locate the index file inside the directory is because the directory is meant for users to drop their stuff in there.

Nginx's index directive seems to only work for files inside the document root; the documentation says that the index can be an "absolute path", but my experiments tell that this is only relative to the document root.

So, I tried to serve another index location with location =/index.html { alias /path/to/index.html }, and this manages to work when /index.html is directly requested, but it doesn't work if / is requested (403 with log "directory index of "/srv/docroot/" is forbidden"). It starts to work, though, if I create an empty file to /srv/docroot/index.html; then /path/to/index.html is served at /. So it seems that Nginx

  1. Checks if /srv/docroot/index.html file exists in the filesystem at the /'s location.
  2. If it exists, it does an internal redirect, and serves the /path/to/index.html at /index.html location.

What is the correct way to serve an index file outside of the document root?

CodePudding user response:

index and try_files directives, provided with absolute paths, seem only to be able to point to the files inside the current document root. (With relative ../ paths you can point outside of the document root, but that's not ideal if you want to point to an absolute path in the filesystem.)

It seems that only the alias directive can point outside the document root. I was able to get my setup to work with:

...
        location / {
            try_files $uri /index.html;
        }
        location =/index.html {
            alias /path/to/index.html;
        }
...

This doesn't strictly answer to the question in the sense, that in this case, index.html isn't shown only when / is requested, but always when a matching file isn't found. I'm happy with this solution, but it might make sense in some cases to separate the 404 error.

CodePudding user response:

To serve an index file outside of the document root with Nginx, you can use the try_files directive in your server block. The try_files directive allows you to specify a list of files that Nginx should try to serve, in order. If none of the specified files exist, Nginx will serve a default 404 error page.

Here is an example configuration that shows how to use the try_files directive to serve an index file outside of the document root:

    server {
  listen 80;
  server_name example.com;

  root /srv/docroot;

  index index.html;

  location / {
    try_files /path/to/index.html $uri $uri/ =404;
  }
}

In this configuration, Nginx will first try to serve the file at /path/to/index.html when a request for / is received. If that file does not exist, Nginx will try to serve the file at the requested URI ($uri). If that file does not exist, Nginx will try to serve the directory index for the requested URI ($uri/). If none of these files exist, Nginx will serve a default 404 error page.

This configuration allows you to serve an index file outside of the document root without having to create a placeholder file inside the document root. It also allows you to serve the correct index file for any request, rather than just requests for /index.html.

Let me know if it helps!

  • Related