Nginx : directory listing and serve static files

July 27th, 2015 § 0 comments

Recently I need to enable the directory listing in one nginx route and enable file download for team members.

Enable Directory listing

Enabling directory listing in nginx is pretty easy.


location ~ ^/files(|\/)$  {
  alias   /var/www/team_files/;
  autoindex on;
}

Here autoindex on; is the one enabling directory listing. alias points to the source file directory.

Serve Static files

This was relatively difficult part for me. I played a lot for this to work.


location ~ ^/files/(.*)\.(doc|txt|jpg)$ {
    alias /var/www/team_files/;
    try_files $1.$2 =404;
}

Now when my team enter localdomain.dev/files the files directory, the team_files directory will be listed and all can download the files shared in that directory.

Revath S Kumar [RSK]

- Rubyist / JavaScripter - Yeoman Team member - Open source enthusiast - Blogger @ PHPRepo and http://blog.revathskumar.com - Coder @ Whatznear - Google fanboy - jQuery lover

More Posts - Website - Twitter - Facebook - LinkedIn - Pinterest - Google Plus - YouTube

Tagged