防盗链
在web02模拟 恶意盗链web01 的内容 配置
主配置文件nginx.conf
[root@web02 conf]# cat /application/nginx/conf/nginx.conf worker_processes 2; worker_cpu_affinity 0101 1010; error_log logs/error.log; #配置Nginx worker进程最大打开文件数 worker_rlimit_nofile 65535; user www www; events { #单个进程允许的客户端最大连接数 worker_connections 20480; #事件处理模型优化 use epoll; } http { include mime.types; default_type application/octet-stream; #sendfile on; #keepalive_timeout 65; #访问日志配置 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #虚拟主机 include /application/nginx/conf/extra/www.conf; include /application/nginx/conf/extra/blog.conf; include /application/nginx/conf/extra/bbs.conf; include /application/nginx/conf/extra/edu.conf; include /application/nginx/conf/extra/phpmyadmin.conf; include /application/nginx/conf/extra/status.conf; #nginx优化---------------------- #隐藏版本号 server_tokens off; #优化服务器域名的散列表大小 server_names_hash_bucket_size 64; server_names_hash_max_size 2048; #开启高效文件传输模式 sendfile on; #减少网络报文段数量 #tcp_nopush on; #提高I/O性能 tcp_nodelay on; #连接超时 时间定义 默认秒 默认65秒 keepalive_timeout 60; #读取客户端请求头数据的超时时间 默认秒 默认60秒 client_header_timeout 15; #读取客户端请求主体的超时时间 默认秒 默认60秒 client_body_timeout 15; #响应客户端的超时时间 默认秒 默认60秒 send_timeout 25; #上传文件的大小限制 默认1m client_max_body_size 8m; #nginx与php之间FastCGI 相关参数调优 #时间超时设定 fastcgi_connect_timeout 240; fastcgi_send_timeout 240; fastcgi_read_timeout 240; #缓冲/缓存设置 fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_temp_path /data/ngx_fcgi_tmp; fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; #使用gzip压缩 gzip on; gzip_min_length 1k; gzip_buffers 4 32k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/css text/xml application/javascript text/plain application/x-javascript application/xml; gzip_vary on; #禁止使用IP访问web服务器 server { listen 80 default; server_name _; return 501; } #控制nginx并发连接数量 limit_conn_zone $binary_remote_addr zone=addr:10m; limit_conn_zone $server_name zone=perserver:10m; #控制客户端请求nginx的速率 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; }
虚拟主机配置文件
[root@web02 conf]# cat /application/nginx/conf/extra/www.conf #server { # listen 80; # server_name www.daolian.com; # rewrite ^(.*)$ https://$host$1 permanent; #} server { listen 80; server_name www.daolian.com; root html/www; index index.php index.html index.htm; ##https证书 # https对防盗链配置测试有巨大影响,需要关闭 #ssl on; #ssl_certificate /application/nginx/conf/key/server.crt; #ssl_certificate_key /application/nginx/conf/key/server.key; #访问日志 access_log logs/access_www.log main buffer=32k flush=5s; location / { #控制nginx并发连接数量 limit_conn addr 1; limit_conn perserver 2; #控制客户端请求nginx的速率 limit_req zone=one burst=5; } ##客户端对静态内容缓存 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30y; root html/www; } location ~ .*\.(js|css)?$ { expires 30d; root html/www; } #php解析 location ~ .*\.(php|php5)?$ { root html/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; #FastCGI 相关参数调优 #fastcgi_cache ngx_fcgi_cache; fastcgi_cache_valid 200 302 1h; fastcgi_cache_valid 301 1d; fastcgi_cache_valid any 1m; fastcgi_cache_min_uses 1; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_cache_key http://$host$request_uri; } }
编辑盗链文件
[root@web02 conf]# cat /application/nginx/html/www/index.htmlcjh wo de bo ke!
wo de bo ke shi cjh bo ke di zhi
web01上传在站点目录上传daolian.jpg图片
浏览器访问测试盗链能否成功
在windows中注意配置hosts 注意:配置的地址是web02 恶意盗链的服务器地址
10.0.0.8 www.daolian.com
web01模拟被盗链的服务器配置
主配置文件nginx.conf
[root@web01 conf]# cat /application/nginx/conf/nginx.conf worker_processes 2; worker_cpu_affinity 0101 1010; error_log logs/error.log; #配置Nginx worker进程最大打开文件数 worker_rlimit_nofile 65535; user www www; events { #单个进程允许的客户端最大连接数 worker_connections 20480; #使用epoll模型 use epoll; } http { include mime.types; default_type application/octet-stream; #sendfile on; #keepalive_timeout 65; #访问日志配置 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #虚拟主机 include /application/nginx/conf/extra/www.conf; include /application/nginx/conf/extra/blog.conf; include /application/nginx/conf/extra/bbs.conf; include /application/nginx/conf/extra/edu.conf; include /application/nginx/conf/extra/phpmyadmin.conf; include /application/nginx/conf/extra/status.conf; #nginx优化---------------------- #隐藏版本号 server_tokens on; #优化服务器域名的散列表大小 server_names_hash_bucket_size 64; server_names_hash_max_size 2048; #开启高效文件传输模式 sendfile on; #减少网络报文段数量 #tcp_nopush on; #提高I/O性能 tcp_nodelay on; #连接超时 时间定义 默认秒 默认65秒 keepalive_timeout 60; #读取客户端请求头数据的超时时间 默认秒 默认60秒 client_header_timeout 15; #读取客户端请求主体的超时时间 默认秒 默认60秒 client_body_timeout 15; #响应客户端的超时时间 默认秒 默认60秒 send_timeout 25; #上传文件的大小限制 默认1m client_max_body_size 8m; #nginx与php之间FastCGI 相关参数调优 #时间超时设定 fastcgi_connect_timeout 240; fastcgi_send_timeout 240; fastcgi_read_timeout 240; #缓冲/缓存设置 fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_temp_path /data/ngx_fcgi_tmp; fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; #使用gzip压缩 gzip on; gzip_min_length 1k; gzip_buffers 4 32k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/css text/xml application/javascript text/plain application/x-javascript application/xml; gzip_vary on; #禁止使用IP访问web服务器 server { listen 80 default; server_name _; return 501; } }
虚拟主机配置文件
[root@web01 conf]# cat /application/nginx/conf/extra/www.conf server { listen 80; server_name www.abc.com; rewrite ^(.*)$ https://$host$1 permanent; } server { listen 443; server_name www.abc.com; root html/www; index index.php index.html index.htm; #https证书 ssl on; ssl_certificate /application/nginx/conf/key/server.crt; ssl_certificate_key /application/nginx/conf/key/server.key; #访问日志 access_log logs/access_www.log main; #隐藏版本号 server_tokens on; location ~* .*\.(jpg|gif|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ { #小括号中不能包含要跳转的图片结尾格式.png,-->否则会进入死循环跳转 valid_referers none blocked *.abc.com abc.com www.abc.com; if ($invalid_referer) { #return 403; rewrite ^/ https://www.abc.com/fangdaolian.png; expires 365d; #说明: 可以添加缓存expire配置, 让禁止盗链的图片在用户本地缓存,就不用让用户再向被盗链端的服务器请求该fangdaolian.png图片了,在一定程度上即防止盗链了,也避免了服务器的流量损失 } } #客户端对静态内容缓存 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30y; root html/www; } location ~ .*\.(js|css)?$ { expires 30d; root html/www; } ##php解析 location ~ .*\.(php|php5)?$ { root html/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; #FastCGI 相关参数调优 #fastcgi_cache ngx_fcgi_cache; fastcgi_cache_valid 200 302 1h; fastcgi_cache_valid 301 1d; fastcgi_cache_valid any 1m; fastcgi_cache_min_uses 1; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_cache_key http://$host$request_uri; } }
浏览器访问盗链端-观察能否防止盗链
在windows中注意配置hosts 注意:配置的地址是web02 恶意盗链的服务器地址
10.0.0.8 www.daolian.com
让开发人员根据cookie进行防盗链