Score:0

Nginx Block access to certain file types (like .txt)

gb flag

my goal: Block any request that requests these file types:

{random name{.txt
.md
.git

If the user tries to access any of the "blocked" file types, i want to return a HTTP 404. This should happen on all folders, not just the root folder

my Server code so far

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /forum;
        index index.php;
        server_name forum.example.net;
        location / {
                try_files $uri $uri/ =404;
        }
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
        location ~ /\.ht {
                deny all;
        }
        location ~ \.md {
        deny all;
        return 404;
        }
        location ~ /\.txt {
        deny all;
        return 404;
        }
        location ~ /\.git {
        deny all;
        return 404;
        }
}

When i try going to the url: /folder/test.md

It downloads, and not sending a Deny all, which i want to block it

how can i do this?

bocian85 avatar
cn flag
try to put those block before location / { try_files $uri $uri/ =404; } also remove / before \.filetype, because it will only match url like something/subdir/.txt
Michael Hampton avatar
cz flag
Why is there a `deny all` in there?
Score:0
gb flag

Block the filetypes:

.md
.git
{anything}.txt

add to server block:

location ~\.(git|txt|md)$ {
 denyall;
 404;
}

if user accesses /folder/1.txt, 1.txt will return has HTTP 404

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.