Blocking Dangerous File Types with NGINX

You might have uploaded dangerous file types to your server just to store them, but maybe you don’t want to serve them to the public. Dangerous file types include executable(.exe) and dynamic link libraries (.dll). To block all files with that file type, just use the following code snippet.

location ~ \.(exe|dll)$ {
     return 404;

That will match all files on the server_name with that end with .exe or .dll. You can add more blocked filed types by adding “|” and then your file type after “dll” between the parenthesis.

Return text with NGINX

Maybe you’re want to display the simplest message to a viewer. You may opt to display plain text. NGINX provides a simple solution using the example code below. Simply choose the path and return your text.

location / {
    #So browsers won't download a file with the text
    add_header Content-Type text/plain;
    #Displays the actual text
    return 200 'Your text here';

Return accepts 2 arguments. First one is HTTP Code and the last one is the text you want to show.