Mary
Mary

Reputation: 1

nginx redirects all files with or without extensions except a few

How to match all files with or without extension, except those with the extension txt, pdf, jpeg.

On my Nginx configuration this downloads all the files without any restriction.

location ~ .+(?<!\.pdf|\.txt|\.jpeg)$ {
        
    auth_request /auth.php;
    error_page 401 = @login;
} 

Thanks

Upvotes: 2

Views: 692

Answers (2)

Mary
Mary

Reputation: 1

Solved I auth.php was not included as an exception which would cause a loop.

location ~ .+(?<!/|auth.php|.txt|.pdf|.jpeg|.jpg|.png)$ { 

    auth_request /auth.php;
    error_page 401 = @login;

}

Upvotes: 1

slauth
slauth

Reputation: 3178

What about defining two locations? E.g.

location ~ \.(pdf|txt|jpeg)$ {
    # handle those with the extension pdf, txt, jpeg
}
location / {
    # handle all files with or without extension
}

Upvotes: 0

Related Questions