1、配置虚拟主机文件

[root@bogon tmp]# cat /usr/local/nginx/conf/vhosts/test.conf

server
{
    listen 80;
    server_name www.test.com www.aaa.com;
    if ($host != 'www.test.com')
    {
     rewrite ^/(.*)$ http://www.test.com/$1 permanent;
    }
    index index.html index.htm index.php;
    root /data/www;
    access_log /tmp/access.log combined_realip;

    location ~ .*admin\.php$ {
       auth_basic "caimz auth";
     auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi1.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|gz|bz2)$
    {
     access_log off;
     expires 15d;
     valid_referers none blocked *.test.com *.aaa.com *.aminglinux.com;
     if ($invalid_referer)
     {
          return 403;
     }
    }
    location ~ \.(js|css)
    {
     access_log off;
     expires 2h;    
    }
    location ~ (static|cache)
    {
     access_log off;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi1.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }
}

wKioL1Y0s4OifUDpAAVJ8OvPFQY068.jpg


2、检查配置文件

[root@bogon tmp]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

3、重新加载nginx的配置文件

[root@bogon tmp]# /usr/local/nginx/sbin/nginx -s reload

4、测试

wKiom1Y0s4OxFnZ9AAboEcYbvMw877.jpg

curl -e可是测试referer

上图图片地址为:

  1. Request URL:

    http://www.test.com/static/p_w_picpath/common/logo_88_31.gif

[root@bogon tmp]# curl -e "http://www.baidu.com/111" -I -x127.0.0.1:80 'http://www.test.com/static/p_w_picpath/common/logo_88_31.gif'
HTTP/1.1 403 Forbidden          #测试也就是我这图片能否放在baidu网上。 显示403则说明防盗链设置OK,起作用了。禁止盗取。
Server: nginx/1.6.2
Date: Sun, 25 Oct 2015 09:45:30 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

[root@bogon tmp]# curl  -I -x127.0.0.1:80 'http://www.test.com/static/p_w_picpath/common/logo_88_31.gif'
HTTP/1.1 200 OK                      #测试也就是我这图片能否放在www.test.com。 显示200则说明防盗链设置OK,起作用了。可以的,因为www.test.com就是在我白名单中
Server: nginx/1.6.2
Date: Sun, 25 Oct 2015 09:45:45 GMT
Content-Type: p_w_picpath/gif
Content-Length: 2528
Last-Modified: Tue, 09 Jun 2015 17:21:10 GMT
Connection: keep-alive
ETag: "55772086-9e0"
Expires: Mon, 09 Nov 2015 09:45:45 GMT
Cache-Control: max-age=1296000
Accept-Ranges: bytes

[root@bogon tmp]# curl -e "http://www.test.com/111" -I -x127.0.0.1:80 'http://www.test.com/static/p_w_picpath/common/logo_88_31.gif'
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Sun, 25 Oct 2015 09:46:14 GMT
Content-Type: p_w_picpath/gif
Content-Length: 2528
Last-Modified: Tue, 09 Jun 2015 17:21:10 GMT
Connection: keep-alive
ETag: "55772086-9e0"
Expires: Mon, 09 Nov 2015 09:46:14 GMT
Cache-Control: max-age=1296000
Accept-Ranges: bytes

[root@bogon tmp]# curl -e "http://www.aaa.com/111" -I -x127.0.0.1:80 'http://www.test.com/static/p_w_picpath/common/logo_88_31.gif'
HTTP/1.1 200 OK                             #测试也就是我这图片能否放在www.aaa.com。 显示200则说明防盗链设置OK,起作用了。可以的,因为www.aaa.com就是在我白名单中 
Server: nginx/1.6.2
Date: Sun, 25 Oct 2015 09:46:21 GMT
Content-Type: p_w_picpath/gif
Content-Length: 2528
Last-Modified: Tue, 09 Jun 2015 17:21:10 GMT
Connection: keep-alive
ETag: "55772086-9e0"
Expires: Mon, 09 Nov 2015 09:46:21 GMT
Cache-Control: max-age=1296000
Accept-Ranges: bytes