A
A
Alex2020-12-30 19:19:34
Nginx
Alex, 2020-12-30 19:19:34

How to allow access only to certain file extensions in nginx?

For example, there is a /upload folder with a bunch of folders and files, and you need to allow access to it only if access is requested to images like png jpg gif
And in the /source folder only to js and css files.
And so that php files and other executive files cannot be launched in these folders, even if .jpg or another allowed extension is substituted at the end (for example /upload/index.php?s=1.jpg )
I would be grateful for a good example of how to do it correctly !

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-12-30
@alexjet73

location ^~ /upload {
    location ~ \.(png|jpe?g|gif)$ {

    }
    return 403;
}

location ^~ /source {
    location ~ \.(js|css)$ {

    }
    return 403;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question