ubuntu

Ubuntu Apache配置中如何启用防盗版

小樊
51
2025-08-16 04:09:20
栏目: 智能运维

在Ubuntu Apache中启用防盗版(防盗链)可通过以下步骤实现,核心是使用mod_rewrite模块限制资源访问来源:

1. 启用必要模块

确保mod_rewrite模块已启用:

sudo a2enmod rewrite  
sudo systemctl restart apache2  

2. 配置防盗链规则

方法一:通过.htaccess文件

在网站根目录(如/var/www/html)创建或编辑.htaccess文件,添加以下内容:

RewriteEngine On  
# 允许空Referer(如直接访问)或指定域名访问  
RewriteCond %{HTTP_REFERER} !^$  
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain\.com [NC]  
# 阻止非授权域名访问图片/视频等资源  
RewriteRule \.(jpg|jpeg|png|gif|mp4|avi)$ - [F,L]  

yourdomain.com替换为你的实际域名,可根据需要调整文件类型。

方法二:通过Apache配置文件

编辑虚拟主机配置文件(如/etc/apache2/sites-available/your-site.conf):

<Directory "/var/www/html">  
    RewriteEngine On  
    RewriteCond %{HTTP_REFERER} !^$  
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain\.com [NC]  
    RewriteRule \.(jpg|jpeg|png|gif)$ - [F,L]  
</Directory>  

保存后重启Apache:

sudo systemctl restart apache2  

3. 验证配置

尝试从非授权域名访问网站资源,若配置正确,将返回403 Forbidden错误。

注意事项

以上配置基于mod_rewrite模块,适用于大多数场景,可根据实际需求调整规则。

0
看了该问题的人还看了