I would like to expose files in /var/files to the user on http://myweb.com/img/someimage.jpg but also to disable accessing non-image files.

This is what I am trying to do, but it does not work, could you help me?

    location /img/*\.(jpg|jpeg|gif|png|bmp|ico) {
        alias /var/files;

According to the docs, you should be able to match the file name via regex and use it in the alias statement, i.e.

location ~ /img/(.+\.(?:jpg|jpeg|gif|png|bmp|ico))$ {
    alias /var/files/$1;

(That is pretty much one of the examples there ;-)

  • It always puts trailing slash at the end 500 Internal Server Error :/ Jun 19 '14 at 13:35
  • First, the problem I was facing even while trying all the methods was that chrome stored trailing slash at wrong config and continued adding slash at the end of URL even nginx was fine configured, had to test this in Anonymous window in Chrome. Next there was missing ^ char. ~^/img/( is correct. Thanks for your help! ;) Jun 19 '14 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.