如何防止 nginx 中的图像热链接?

如何防止 nginx 中的图像热链接?

我正在尝试在 nginx 中实现图像热链接保护问题,我需要帮助。我有一个很大的问题,我的网站的图片被提交到 StumbleUpon 等社交网络,带有直接链接,如

http://example.com/da.jpg

现在我想阻止对它们的访问,但我无法在 nginx.conf 文件中实现热链接预防。下面是我的 nginx.conf 文件,我应该把代码放在哪里?

要实现的热链接代码:

  location ~ \.(jpe?g|png|gif)$ {
    valid_referers none blocked example.com *.example.com;
    if ($invalid_referer) {
        return 403;
    }
}  

我当前的代码如下

{

#user  nobody;
worker_processes  10;
worker_rlimit_nofile 81918;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  81918;
    multi_accept on;
}


http {


    client_body_buffer_size 10K;
    client_header_buffer_size 1k;
    client_max_body_size 8m;
    large_client_header_buffers 2 1k;

    client_body_timeout   32;
    client_header_timeout 32;
    sendfile_max_chunk 512k;
    keepalive_timeout 5; # default 65
    send_timeout 20;     # default 60

    reset_timedout_connection on;

    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  5;
    #keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }


        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    server {
        listen       *:80;
            server_name  dl.rahim-soft.org;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location / {
            root   E:/WWW;
            index  index.html index.htm;

        }
    }
    server {
        listen       *:80;
        server_name  dl1.rahim-soft.org;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   E:/dl1;
            index  index.html index.htm;
        }
    }
    server {
        listen       *:80;
        server_name  dl2.rahim-soft.org;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   F:/dl2.rahim-soft.org;
            index  index.html index.htm;
        }

    }

}

答案1

也许您的图像也需要根位置。

server {
  listen       *:80;
  server_name  dl2.rahim-soft.org;

  location / {
    location ~* \.(jpe?g|png|gif)$ {
      valid_referers none blocked rahim-soft.org *.rahim-soft.org;
      if ($invalid_referer) {
        return 403;
      }
    }  

    root   F:/dl2.rahim-soft.org;
    index  index.html index.htm;
  }
} 

答案2

您需要剪切并粘贴该配置片段到每个服务器 { } 节中,例如 dl2.rahim-soft.org:

server {
    listen       *:80;
    server_name  dl2.rahim-soft.org;

    location ~ \.(jpe?g|png|gif)$ {
        root   F:/dl2.rahim-soft.org;
        valid_referers none blocked rahim-soft.org *.rahim-soft.org;
        if ($invalid_referer) {
            return 403;
        }
    }  
    location / {
        root   F:/dl2.rahim-soft.org;
        index  index.html index.htm;
    }
} 

Nginx 将优先处理正则表达式匹配,而不是前缀匹配。但是,它首先评估前缀位置,允许管理员通过使用 = 和 ^~ 修饰符指定位置来覆盖此操作。

虽然前缀位置通常根据最长、最具体的匹配进行选择,但是当找到第一个匹配位置时,正则表达式评估就会停止。

为了更好地理解 nginx 如何优先处理位置匹配,你可能需要阅读 digitalocean 的这篇精彩文章:

https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms#matching-location-blocks

答案3

我认为你忘记了语法的一部分(服务器名称):

    valid_referers none blocked server_names example.com *.example.com;

相关内容