Enseña cómo evitar que otros para robar sus imágenes, nginx anti-hotlinking

Dos sitios A y B, B cita las imágenes en el sitio web de A, este comportamiento se le llama hotlinking. cadena antirrobo, es evitar que la imagen B de referencia de A.

1. ¿Cómo distinguir lo que no es usuario normal?

Referer cabecera es parte de, cuando el navegador envía una solicitud a un servidor Web, suelen traer Referer, le digo a la que el servidor de enlaces de la página otra vez, mediante el cual se puede obtener algo de información del servidor para su procesamiento, por ejemplo, no impidieron permisos de
sitios web hotlinking imágenes, archivos y así sucesivamente. Por lo tanto, la información de cabecera HTTP Referer es generada por un camuflaje programa, por lo que la cadena de seguridad por El árbitro no fiable al 100%, sin embargo, es posible limitar Daolian mayoría de los casos.

2. La configuración de la cadena de seguridad

Tenga en cuenta lo siguiente:

1.如果你是编译安装的nginx,你需要去配置文件中将日志格式的注释去掉
[root@localhost ~]# vim /etc/nginx/nginx.conf     #配置文件
# 日志格式添加"$http_referer"
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for"';
#将上面的注释去掉,编译安装默认是注释掉的
2.如果你是yum安装的话,默认这些日志格式是没有被注释的,可以不用做任何操作
3.准备两台装有nginx的虚拟机
图片网站服务器:上传图片  192.168.13.133
盗链机器:用于盗链测试192.168.13.129
3. Foto Configuración del servidor Web (192.168.13.133)

Aquí Insertar imagen Descripción
Estamos en esta imagen, por ejemplo,

[root@localhost ~]# mv test.jpg /usr/share/nginx/html/      
#上传图片并改名为test.jpg,并将其移动到/usr/share/nginx/html/目录下,yum安装的默认有这个目录(这个目录编译安装的nginx可能没有,没有的话自己创建)
[root@localhost ~]# chmod 644 /usr/share/nginx/html/test.jpg           
#有的虚拟机上传图片后可能权限不够,需要给其添加权限。如果权限不够的话,盗链机器访问时会返回403状态码
开始配置(这里是yum安装的nginx)
[root@localhost ~]# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf,bak   #备份配置文件以防万一
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
清空并添加以下代码
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
[root@localhost ~]# nginx -t   #看看配置又没有出错
[root@localhost ~]# nginx -s reload   #重新加载配置文件

A continuación, abra el sitio, escriba: http: //192.168.13.133/test.jpg
Aquí Insertar imagen Descripción
aparentemente exitosas subir fotos

4. configuración de la máquina Daolian (192.168.13.129)
[root@localhost ~]# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf,bak   #备份配置文件以防万一
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
同样清空并添加以下代码
server{
        listen 80;
        server_name locahost;
        location / {
        		root /usr/share/nginx/html;
        		index index.html;
        }

}
[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost ~]# cp index.html index.html.bak     #备份
[root@localhost ~]# vim index.html
清空并添加以下代码
<html>
<head>
    <meta charset="utf-8">
    <title>test.com</title>
</head>
<body style="background-color:red;">
    <img src="http://192.168.13.133/test.jpg"/>
</body>
</html>
#这里面只需要将ip地址改为你自己的图片服务器的地址,其它的都是一些网页的格式
[root@localhost ~]# nginx -t   #看看配置又没有出错
[root@localhost ~]# nginx -s reload   #重新加载配置文件

Abra dirección de la página Web máquinas hotlinking de entrada IP: 192.168.13.129
Aquí Insertar imagen Descripción
Encontrará hotlinking máquina puede robar sus imágenes
cómo prevenirlo?

La cadena antirrobo (192.168.13.133)
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
清空并添加以下代码
server {
      listen       80;
      server_name  localhost;
      location / {
          root   /usr/share/nginx/html;
        index  index.html index.htm;
  
        valid_referers none blocked www.jd.com;  #允许这些访问
                  if ($invalid_referer) {
                   return 403;
                  }
        }
  }
  • Ninguno: no se les permite solicitar el acceso a los recursos de http_refer
  • Bloqueado: permiten en lugar de http: // al principio del acuerdo sin que solicita el acceso a los recursos - se filtran fuera del cortafuegos;
[root@localhost ~]# nginx -t   #看看配置又没有出错
[root@localhost ~]# nginx -s reload   #重新加载配置文件

A continuación, abra la entrada de dirección de la página web hotlinking máquinas IP: 192.168.13.129
Aquí Insertar imagen Descripción
encontrará máquinas hotlinking no ven la imagen.
Y se ve el registro de acceso del servidor de máquina de foto

[root@localhost ~]# tail -f /var/log/nginx/access.log 

Aquí Insertar imagen Descripción
Usted encontrará que el acceso a su máquina IP aparezca.

Publicado 13 artículos originales · ganado elogios 27 · vistas 3638

Supongo que te gusta

Origin blog.csdn.net/baidu_38803985/article/details/104802695
Recomendado
Clasificación