CentOS服務器下nginx防盜鏈介紹與配置

一、防盜鏈介紹
1.什麼是防盜鏈
簡單的說,就是某些不法的網站,通過在其自身網站程序裏爲經許可非法調用其他網站資源然後在自己的網站上顯示這些調用的資源,達到了填充自身網站顯示的效果,但是浪費了調用資源網站的網站流量,造成其他網站的帶寬及服務壓力吃緊,甚至宕機。
二、配置防盜鏈
1.配置三個個站點

[root@lnmp ~]# cd /application/nginx/
[root@lnmp nginx]# cat conf/nginx.conf      
worker_processes  1;
events {
    worker_connections  1024;
    use epoll;
}
http {
    server_tokens off;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.liang.com;
            root   html/www;
            index  index.php index.html index.htm;
        location ~ \.php$ {
            root           html/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
                    }        
        }

        server {
        listen       80;
        server_name  bbs.liang.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
                }
        }
        server {
        listen       80;
        server_name  blog.liang.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
                }
        }
}

2.測試是否能訪問

[root@lnmp nginx]# curl www.liang.com/index.html       
https://blog.csdn.net/liang_operations/
[root@lnmp nginx]# curl bbs.liang.com                 
bbs
[root@lnmp nginx]# curl blog.liang.com
blog

3.模仿環境配置
3.1www.liang.com爲被盜

[root@lnmp nginx]# mkdir html/www/ima   
[root@lnmp nginx]# rz
rz waiting to receive.
 zmodem trl+C ȡ

  100%     277 KB  277 KB/s 00:00:01       0 Errors
  
[root@lnmp nginx]# mv  timg.jpg html/www/ima/

3.2bbs.liang.com盜取www的鏈接

[root@lnmp nginx]# cat html/bbs/index.html   
<img src="http://www.liang.com/ima/timg.jpg">

3.3訪問測試
在這裏插入圖片描述

4.www配置防盜
[root@lnmp nginx]#vi conf/nginx.conf
server {
listen 80;
server_name www.liang.com;
root html/www;
index index.php index.html index.htm;
location ~ .php$ {
root html/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scriptsKaTeX parse error: Expected 'EOF', got '}' at position 83: … }̲ locati… {
valid_referers none blocked www.liang.com;
if ($invalid_referer) {
rewrite ^/ http://blog.liang.com/ima/b.jpg;
}
}
}

代碼詳解:
第一行: location ~* .(gif|jpg|png|swf|flv|bmp)$
其中“gif|jpg|png|swf|flv|bmp”設置防盜鏈文件類型,自行修改,每個後綴用“|”符號分開!
第二行: valid_referers none blocked www.liang.com;
就是白名單,允許文件鏈出的域名白名單,自行修改成您的域名! 可以使用子域名,域名與域名之間使用空格隔開!
第五行:rewrite ^/ http://blog.liang.com/ima/b.jpg;
這個圖片是盜鏈返回的圖片,也就是替換盜鏈網站所有盜鏈的圖片。這個圖片要放在沒有設置防盜鏈的網站上,因爲防盜鏈的作用,這個圖片如果也放在防盜鏈網站上就會被當作防盜鏈顯示不出來了,盜鏈者的網站所盜鏈圖片會顯示X符號。
這樣設置差不多就可以起到防盜鏈作用了。
5.配置blog
[root@lnmp nginx]# ll html/blog/ima/
total 12
-rw-r–r--. 1 root root 11988 Aug 9 2018 b.jpg
6.網頁測試
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章