How to configure Nginx to prohibit the download of files with a specified suffix in detail

Author : YDD This article has 406 words, Reading time 2 minutes Posted on: 2024-06-15 Read by 427 people
advertising chart
advertising chart
Want to show up here too?Contact us.

Article Description:

您想知道如何在Nginx中设置禁止下载特定后缀的文件吗?为了实现这一目的,您可以在Nginx的配置文件中添加以下代码段:

location ~* \.(txt|pdf|doc)$ {
deny all;
}

或者,您也可以选择以下配置:

location ~* \.(zip|rar|sql|bak|gz|7z)$ {
return 444;
}

以上配置将匹配以.txt、.pdf和.doc结尾的请求,并使用 deny all 指令来拒绝访问。对于以.zip、.rar、.sql、.bak、.gz和.7z结尾的请求,Nginx会使用 return 444 指令来拒绝连接。这样配置后,Nginx会返回403 Forbidden或444 No Response给客户端。

根据您的需求,您可以根据实际情况修改正则表达式中的文件后缀列表。在完成配置后,请确保将以上代码段正确添加到Nginx配置文件的适当位置,并重新加载或重启Nginx服务器以使配置生效。

advertising chart
advertising chart
Want to show up here too?Contact us.
Frequently Asked Questions FAQ
Can free downloads or VIP member-only resources be commercialized directly?
The resources on this site are collected and organized through the network, for personal research and study purposes only. The copyright belongs to the legal owner of the software and program code, users should verify the copyright and legality of the resources, prohibited for commercial use, illegal activities or any violation of national laws and regulations.
Disclaimer of liability for program or code bugs, compatibility issues or functional defects, etc.
As the resources on this site are collected and organized through the network, not the site's original, it can not fully guarantee its functionality or code compatibility. Users need to verify whether the resources meet the needs of their own, due to the following circumstances lead to losses, this site does not assume any responsibility:
Programs, source code and other computer software resources may contain code vulnerabilities (bugs), compatibility issues or functional defects left by the developer. This site does not provide free repair services for such technical defects, users need to bear the risk of debugging, modification or abandonment of the use.
© 2025 by - SourceHub & Www.ZYYdd.Com. All rights reserved 蜀ICP备2025145155号-1