Mastering Nginx

(Ron) #1
Chapter 6

[ 123 ]

location /downloads {

autoindex on;

}

}

In the preceding example any files to be served are found under the root /home/
customer/html. If the client entered just the domain name, NGINX will try to serve


index.html. If that file does not exist, then NGINX will serve index.htm. When a user
enters the /downloads URI in their browser, they will be presented with a directory


listing in HTML format. This makes it easy for users to access sites hosting software


that they would like to download. NGINX will automatically rewrite the URI of a
directory so that the trailing slash is present, and then issue an HTTP redirect. NGINX


appends the URI to the root to find the file to deliver to the client. If this file does not
exist, the client receives a 404 Not Found error message. If you don't want the error


message to be returned to the client, one alternative is to try to deliver a file from


different filesystem locations, falling back to a generic page, if none of those options
are available. The try_files directive can be used as follows:


location / {

try_files $uri $uri/ backups/$uri /generic-not-found.html;

}

As a security precaution, NGINX can check the path to a file it's about to deliver,


and if part of the path to the file contains a symbolic link, it returns an error message
to the client:


server {

root /home/customer/html;

disable_symlinks if_not_owner from=$document_root;

}

In the preceding example, NGINX will return a "Permission Denied" error if a


symlink is found after /home/customer/html, and that symlink and the file it
points to do not both belong to the same user ID.

Free download pdf