ubuntu

Ubuntu Apache2如何配置防盗版机制

小樊
48
2025-08-20 05:35:33
栏目: 智能运维

以下是在Ubuntu上使用Apache2配置防盗版(防盗链)的常见方法:

一、使用mod_rewrite模块(推荐)

  1. 启用模块

    sudo a2enmod rewrite
    sudo systemctl restart apache2
    
  2. 配置规则

    • 方式1:通过.htaccess文件(需确保AllowOverride All已启用)
      在网站根目录(如/var/www/html)创建或编辑.htaccess文件,添加:

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

      yourdomain.com替换为实际域名,可添加多个域名用|分隔。

    • 方式2:通过虚拟主机配置
      编辑/etc/apache2/sites-available/yourdomain.conf,在<VirtualHost>块中添加:

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

    sudo systemctl restart apache2
    

二、使用mod_security模块(高级防护)

  1. 安装模块

    sudo apt install libapache2-mod-security2
    sudo a2enmod security2
    
  2. 配置规则
    编辑/etc/modsecurity/modsecurity.conf,添加规则(示例为禁止非授权域名访问图片):

    SecRule REQUEST_FILENAME "\.(jpg|jpeg|png|gif)$" \
    "id:1001,phase:2,deny,status:403,msg:'Unauthorized image access'"
    
  3. 重启服务

    sudo systemctl restart apache2
    

三、辅助措施

注意事项

参考来源:

0
看了该问题的人还看了