• Nginx 防盗链


    nginx防盗链问题

    盗链:

    就是a网站有一张照片,b网站引用了a网站的照片 。

    防盗链:

    a网站通过设置禁止b网站引用a网站的照片。

    nginx防止网站资源被盗用模块

    ngx_http_referer_module

    如何区分哪些是不正常的用户?

    HTTP Referer是Header的一部分,当浏览器向Web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器借此可以获得一些信息用于处理,例如防止未经允许的网站盗链图片、文件等。因此HTTP Referer头信息是可以通过程序来伪装生成的,所以通过Referer信息防盗链并非100%可靠,但是,它能够限制大部分的盗链情况.

    比如在www.google.com 里有一个www.baidu.com 链接,那么点击这个www.baidu.com ,它的header 信息里就有:Referer=http://www.google.com

    防盗链配置要点

    1. [root@nginx-server ~]# vim /etc/nginx/nginx.conf
    2. # 日志格式添加"$http_referer"
    3. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    4. '$status $body_bytes_sent "$http_referer" '
    5. '"$http_user_agent" "$http_x_forwarded_for"';
    6. # valid_referers 使用方式
    7. Syntax: valid_referers none | blocked | server_names | string ...;
    8. Default: —
    9. Context: server, location

    盗链实验:

    准备俩台服务器,a服务器用做网站正版发布照片,b服务器用作引用a服务器的图片信息的服务器。

    a服务器IP:192.168.231.171

    b服务器IP:192.168.231.173

    在a服务器的配置文件内

    1. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    2. server {
    3. listen 80;
    4. server_name localhost;
    5. location /{
    6. root /usr/share/nginx/html;
    7. index 1.jpg;
    8. }
    9. }
    10. 更改完配置文件 记得重启
    11. [root@localhost ~]# nginx -s reload
    12. #网站默认发布页面的路径
    13. [root@localhost ~]# cd /usr/share/nginx/html/
    14. [root@localhost html]# ls
    15. 1.jpg 50x.html index.html

    正常访问a网站

    b服务器,配置nginx访问页面

    1. 由于b服务器也是yum安装的nginx
    2. 因此b服务器的nginx默认发布页面路径在 /usr/local/nginx/html/
    3. [root@daili ~]# cd /usr/share/nginx/html/
    4. [root@daili html]# ls
    5. 50x.html index.html
    6. vim index.html
    7. <html>
    8. <head>
    9. <meta charset="utf-8"> #用老做实验 红色的底
    10. <title>qf.com</title>
    11. </head>
    12. <body style="background-color:red;">
    13. <img src="http://192.168.231.171/1.jpg"/> #盗用171IP的1.jpg这个图片
    14. </body>
    15. </html>

    此时访问b网站,由于它盗用了a网站的图片,因此

    观察a服务器的日志

    这就做成了盗链实验

    防盗链实验

    实验机器

    a服务器IP:192.168.231.174  真正提供照片的服务器

    b服务器IP:192.168.231.173

    1.防盗链的操作得在真正发布这张图片的服务器修改配置文件

    1. a服务器
    2. #vim /etc/nginx/conf.d/default.conf
    3. server {
    4. listen 80;
    5. server_name localhost;
    6. location /{
    7. root /usr/share/nginx/html;
    8. index 1.jpg;
    9. valid_referers server_names 192.168.231.173;
    10. #server_names 只允许指定ip域名来访问资源
    11. if ($invalid_referer) {
    12. return 502; #其他ip或者域名来访问a服务器,返回502
    13. }
    14. }
    15. }
    16. 重启服务
    17. nginx -s reload

    这样就相当于将173这个ip添加到白名单中,只有192.168.231.173可以访问a服务器的资源。

    1. 通过百度来访问a服务器
    2. [root@daili ~]# curl -e "www.baidu.com" -Ik 192.168.231.173
    3. HTTP/1.1 502 Bad Gateway
    4. Server: nginx/1.24.0
    5. Date: Mon, 23 Oct 2023 11:29:07 GMT
    6. Content-Type: text/html
    7. Content-Length: 157
    8. Connection: keep-alive
    9. #由于在a服务器的配置文件设置只能IP为192.168.231.173的来访问,因此其他ip来访问返回502

    平时我们都是通过本机电脑的ip来访问b服务器,而curl -e "ip/域名" -Ik  要访问的ip

    这条命令是我们通过此ip来访问另一个ip。

    curl -e  "www.baidu.com" -Ik  192.168.231.173

    我们通过百度来访问173IP,相当于我们此时的电脑本机变成了百度。

    相同的是,我们设置为只有百度可以访问a服务器,那么b服务器上的图片就会失真打不开

    1. a服务器
    2. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    3. server {
    4. listen 80;
    5. server_name localhost;
    6. location /{
    7. root /usr/share/nginx/html;
    8. index 1.jpg;
    9. valid_referers server_names www.baidu.com;
    10. if ($invalid_referer) {
    11. return 502;
    12. }
    13. }
    14. }
    15. 重启服务
    16. nginx -s reload

    访问b服务器的网站

    三个要素:

    • none : 允许没有http_refer的请求访问资源;

    • blocked : 允许不是http://开头的,不带协议的请求访问资源;

    • server_names : 只允许指定ip/域名来的请求访问资源(白名单);

    加none

    1. 真正有图片的a服务器
    2. server {
    3. listen 80;
    4. server_name localhost;
    5. location /{
    6. root /usr/share/nginx/html;
    7. valid_referers none 192.168.231.173;
    8. index 1.jpg;
    9. }
    10. }

    浏览查看a服务器的发布页面

    查看日志

    tailf /var/log/nginx/access.log

    通过百度来访问a服务器

    1. [root@localhost ~]# curl -e "www.baidu,com" -Ik 192.168.231.174
    2. HTTP/1.1 200 OK
    3. Server: nginx/1.24.0
    4. Date: Tue, 24 Oct 2023 13:07:27 GMT
    5. Content-Type: image/jpeg
    6. Content-Length: 647023
    7. Last-Modified: Fri, 15 Sep 2023 06:06:10 GMT
    8. Connection: keep-alive
    9. ETag: "6503f452-9df6f"
    10. Accept-Ranges: bytes

    不加none

    1. a服务器的配置文件
    2. /etc/nginx/conf.d/default.conf
    3. server {
    4. listen 80;
    5. server_name localhost;
    6. location /{
    7. root /usr/share/nginx/html;
    8. index 1.jpg;
    9. valid_referers blocked www.jd.com;
    10. if ($invalid_referer) {
    11. return 502;
    12. }
    13. }
    14. }

    访问a服务器的网站

    因此

    none只是决定能不能访问a服务器本身,加none可以访问a服务器本身,不加none不可以访问a服务器本身。

    blocked

    本身含义是允许开头不带http的访问成功,那就意味着 带http的访问不成功

    1. 配置a服务器的配置文件
    2. server {
    3. listen 80;
    4. server_name localhost;
    5. location /{
    6. root /usr/share/nginx/html;
    7. index 1.jpg;
    8. valid_referers blocked 192.168.231.174;
    9. if ($invalid_referer) {
    10. return 502;
    11. }
    12. }
    13. }

    访问www与http://www   后者就不可以访问

    1. [root@daili ~]# curl -e "http://www.jd.com" -Ik 192.168.231.171
    2. HTTP/1.1 502 Bad Gateway
    3. Server: nginx/1.24.0
    4. Date: Mon, 23 Oct 2023 13:02:34 GMT
    5. Content-Type: text/html
    6. Content-Length: 157
    7. Connection: keep-alive
    8. [root@daili ~]# curl -e "www.jd.com" -Ik 192.168.231.171
    9. HTTP/1.1 200 OK
    10. Server: nginx/1.24.0
    11. Date: Mon, 23 Oct 2023 13:02:37 GMT
    12. Content-Type: image/jpeg
    13. Content-Length: 647023
    14. Last-Modified: Fri, 15 Sep 2023 06:06:10 GMT
    15. Connection: keep-alive
    16. ETag: "6503f452-9df6f"
    17. Accept-Ranges: bytes

    情况2 加*.jd.com

    1. server {
    2. listen 80;
    3. server_name localhost;
    4. location /{
    5. root /usr/share/nginx/html;
    6. valid_referers none blocked *.jd.com ;
    7. index 1.jpg;
    8. }
    9. }
    1. [root@daili ~]# curl -e "http://www.jd.com" -Ik 192.168.231.174
    2. HTTP/1.1 200 OK
    3. Server: nginx/1.24.0
    4. Date: Tue, 24 Oct 2023 13:31:52 GMT
    5. Content-Type: image/jpeg
    6. Content-Length: 647023
    7. Last-Modified: Fri, 15 Sep 2023 06:06:10 GMT
    8. Connection: keep-alive
    9. ETag: "6503f452-9df6f"
    10. Accept-Ranges: bytes
    11. [root@daili ~]# curl -e "www.jd.com" -Ik 192.168.231.174
    12. HTTP/1.1 200 OK
    13. Server: nginx/1.24.0
    14. Date: Tue, 24 Oct 2023 13:32:00 GMT
    15. Content-Type: image/jpeg
    16. Content-Length: 647023
    17. Last-Modified: Fri, 15 Sep 2023 06:06:10 GMT
    18. Connection: keep-alive
    19. ETag: "6503f452-9df6f"
    20. Accept-Ranges: bytes

    这里虽然配置文件里面写了blocked,但是白名单里面定义的是*.jd.com,因此他有俩个 一个是www开头,一个是http://  因此访问http://jd.com 可以访问

    1. server {
    2. listen 80;
    3. server_name localhost;
    4. location /{
    5. root /usr/share/nginx/html;
    6. valid_referers none blocked www.jd.com ;
    7. index 1.jpg;
    8. }
    9. }
    1. [root@daili ~]# curl -e "www.jd.com" -Ik 192.168.231.174
    2. HTTP/1.1 200 OK
    3. Server: nginx/1.24.0
    4. Date: Tue, 24 Oct 2023 13:35:17 GMT
    5. Content-Type: image/jpeg
    6. Content-Length: 647023
    7. Last-Modified: Fri, 15 Sep 2023 06:06:10 GMT
    8. Connection: keep-alive
    9. ETag: "6503f452-9df6f"
    10. Accept-Ranges: bytes
    11. [root@daili ~]# curl -e "http://www.jd.com" -Ik 192.168.231.174
    12. HTTP/1.1 200 OK
    13. Server: nginx/1.24.0
    14. Date: Tue, 24 Oct 2023 13:35:19 GMT
    15. Content-Type: image/jpeg
    16. Content-Length: 647023
    17. Last-Modified: Fri, 15 Sep 2023 06:06:10 GMT
    18. Connection: keep-alive
    19. ETag: "6503f452-9df6f"
    20. Accept-Ranges: bytes

    这里尽管定义配置的是www.jd.com 按道理输入http://jd.com 是访问不了的,但是在配置文件里面location是在server下的,而server是在http下面的,因此输入http://jd.com 也是可以访问的。

    相同那么加了blocked 由于 白名单里面没有baidu.com 因此 访问www.baidu.com可以访问,访问http://www.baidu.com 就不可以访问。

    1. [root@localhost conf.d]# curl -e "www.baidu.com" -Ik 192.168.231.174
    2. HTTP/1.1 200 OK
    3. Server: nginx/1.24.0
    4. Date: Tue, 24 Oct 2023 13:57:27 GMT
    5. Content-Type: image/jpeg
    6. Content-Length: 647023
    7. Last-Modified: Fri, 15 Sep 2023 06:06:10 GMT
    8. Connection: keep-alive
    9. ETag: "6503f452-9df6f"
    10. Accept-Ranges: bytes
    11. [root@localhost conf.d]# curl -e "http://www.baidu.com" -Ik 192.168.231.174
    12. HTTP/1.1 502 Bad Gateway
    13. Server: nginx/1.24.0
    14. Date: Tue, 24 Oct 2023 13:57:32 GMT
    15. Content-Type: text/html
    16. Content-Length: 157
    17. Connection: keep-alive

  • 相关阅读:
    HDMI之EDID
    Java性能优化|集合|多线程|Redis|数据库|MySQL|JVM
    初识kotlin(初用kotlin一时爽、一直用一直爽)
    windows11 利用vmware17 安装rocky9操作系统
    内网穿透无法访问本地wordpress网站
    力扣:1848. 到目标元素的最小距离
    dockerfile 快速部署jar项目运行
    Codeforces Round 908 (Div. 2)视频详解
    (笔记整理未完成)【图论】无向图求割点、割边
    阿里云->宝塔配置
  • 原文地址:https://blog.csdn.net/m0_59933574/article/details/133996782