如何防止 nginx 中的盜鏈

如何防止 nginx 中的盜鏈

我需要防止我的 php 網站中出現熱連結。我怎樣才能做到這一點?我正在使用 nginx 作為網絡伺服器..如果我嘗試查看site.com/uploads/image.jpg無需登錄,我可以在瀏覽器中查看圖像,我需要防止在未登入的情況下直接存取資料夾。

我的專案結構

site
    -index.php
    -uploads/image.jpg....
    -css/
    -js/

網站設定檔

server {

    listen site.com;

    server_name site.com;



    root /home/vijo/Music/PHP/site;

    index index.php index.html index.htm;



    keepalive_timeout 70;

    access_log /home/vijo/Music/PHP/site/log/access.log;

    error_log /home/vijo/Music/PHP/site/log/error.log;





    # Make site accessible from http://localhost/



   location / {

            try_files $uri $uri/ @rewrite;

            expires max;

    }



    location @rewrite {

            rewrite ^ /index.php;
    }



    location ~ \.php$ {

            fastcgi_split_path_info ^(.+\.php)(/.+)$;

            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

            fastcgi_index index.php;

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            include fastcgi_params;

    }



    location ~ /\.ht {

            deny all;

    }

}

答案1

範例配置如下所示
https://thebarton.org/prevent-hotlinking-nginx/
http://techgirlkb.guru/2017/08/preventing-site-mirroring-via-hotlinking/

location ~* \.(gif|png|jpe?g)$ {
expires 7d;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
# prevent hotlink
valid_referers none blocked ~.google. ~.bing. ~.yahoo. server_names ~($host);
if ($invalid_referer) {
rewrite (.*) /static/images/hotlink-denied.jpg redirect;
# drop the 'redirect' flag for redirect without URL change (internal rewrite)
}
}
# stop hotlink loop
location = /static/images/hotlink-denied.jpg { }

相關內容