大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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 …

问题原文

原帖链接 · user655355 · CC BY-SA 4.0

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?

已采纳解答

原帖链接 · user655355 · CC BY-SA 4.0

Block the filetypes:

.md
.git
{anything}.txt

add to server block:

location ~\.(git|txt|md)$ {
 deny all;
 return 404;
}

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-07-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。