本文共 4319 字,大约阅读时间需要 14 分钟。
防盗链是指一个网站的资源(图片或附件)未经允许在其它网站提供浏览和下载,尤其热门资源的盗链,对网站带宽的消耗非常大,设置防盗链以节省资源。
[root@sdwaqw vhost]# vim linuxtest.confserver{ listen 80; server_name linuxtest.com; index index.html index.htm index.php; root /data/wwwroot/linuxtest; location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ { expires 7d; valid_referers none blocked server_names *.linuxtest.com ;# 定义referer白名单 if ($invalid_referer) { return 403;# if函数的意思是:如果不是白名单内的域名,返回值:403 }# location /# { # auth_basic "Auth";# auth_basic_user_file /usr/local/nginx/conf/htpasswd;# } access_log /tmp/linuxtest.log combined_realip;}#使用access_log指定日志存储路径和使用的日志格式名字
[root@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -s reload[root@sdwaqw vhost]# echo "这是防盗链jpg测试!" > /data/wwwroot/linuxtest/test.jpg[root@sdwaqw vhost]# curl -x127.0.0.1:80 linuxtest.com/test.jpg -IHTTP/1.1 200 OKServer: nginx/1.12.2Date: Thu, 15 Mar 2018 14:33:07 GMTContent-Type: image/jpegContent-Length: 28Last-Modified: Thu, 15 Mar 2018 14:32:45 GMTConnection: keep-aliveETag: "5aaa840d-1c"Expires: Thu, 22 Mar 2018 14:33:07 GMTCache-Control: max-age=604800Accept-Ranges: bytes[root@sdwaqw vhost]# curl -x127.0.0.1:80 -e "http://www.com" linuxtest.com/test.jpg -I //-e选项自定义refererHTTP/1.1 403 ForbiddenServer: nginx/1.12.2Date: Thu, 15 Mar 2018 14:33:28 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive
访问控制即限制指定的IP才能访问指定的目录
[root@sdwaqw vhost]# vim linuxtest.conf //添加如下内容
location /admin/
{ allow 192.168.242.128;allow 127.0.0.1;deny all;# 设置IP白名单 }[root@sdwaqw vhost]# mkdir /data/wwwroot/linuxtest/admin
[root@sdwaqw vhost]# echo “test,test”>/data/wwwroot/linuxtest/admin/1.html[root@sdwaqw vhost]# curl -x127.0.0.1:80 linuxtest.com/admin/1.html“test,test”[root@sdwaqw vhost]# curl -x192.168.242.128:80 linuxtest.com/admin/1.html“test,test”location ~ .(abc|image)/..php$
{ deny all;}if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{ return 403;}修改虚拟主机配置文件
[root@sdwaqw vhost]# vim linuxtest.conflocation ~ .php$
{ include fastcgi_params;//fastcgi_pass 127.0.0.1:9000fastcgi_pass unix:/tmp/php-fcgi.sock;# fastcgi_pass两种监听格式,但是要保证Nginx和php-fpm中格式一致fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;}Nginx代理是一种反向代理。反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
graph LR用户–>代理服务器代理服务器–>用户代理服务器–>web服务器web服务器–>代理服务器[root@sdwaqw vhost]# vim proxy.conf
server
{ listen 80;server_name ask.apelearn.com;# 定义域名(一般和被代理ip的域名保持一致)location /{ proxy_pass ;# 指定被代理(被访问)的IP(web服务器IP)proxy_set_header Host $host;# $host指的是代理服务器的servername(也是被代理IP的域名)proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}[root@sdwaqw vhost]# vim proxy.conf
[root@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -s reload[root@sdwaqw vhost]# curl -x127.0.0.1:80 ask.apelearn.com -I //同通过代理HTTP/1.1 200 OKServer: nginx/1.12.2Date: Thu, 15 Mar 2018 15:44:25 GMTContent-Type: text/htmlConnection: keep-aliveVary: Accept-EncodingX-Powered-By: PHP/5.3.29P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"Set-Cookie: ape__Session=k44g3eklsert1fgbjhl061l4f4; path=/; domain=.apelearn.comExpires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0Pragma: no-cache[root@sdwaqw vhost]# curl ask.apelearn.com -I //直接连
HTTP/1.1 200 OKServer: nginx/1.8.0Date: Thu, 15 Mar 2018 15:46:06 GMTContent-Type: text/htmlConnection: keep-aliveVary: Accept-EncodingX-Powered-By: PHP/5.3.29P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"Set-Cookie: ape__Session=ium8s3hsrjh4ulf6qbrjpdcme2; path=/; domain=.apelearn.comExpires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0Pragma: no-cache转载于:https://blog.51cto.com/sdwaqw/2088617