首页 > 系统相关 >第六节:Nginx常用案例(反盗链、限速、黑名单、跨域等等)

第六节:Nginx常用案例(反盗链、限速、黑名单、跨域等等)

时间:2023-07-21 09:56:10浏览次数:39  
标签:deny 跨域 192.168 server Nginx html location 盗链 include

 

三. 常用场景

1. 防盗链

 直接输入地址,没有referer字段,所以匹配了后面的none或blocked,不跳转。通过搜索引擎打开的含有referer字段,走后面的匹配规则。  

 none 代表没有referer

 blocked 代表有referer,但是被防火墙或代理给去除了。

配置如下:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  test2.cneasy.cn;
        location /{
            valid_referers none blocked *.cneasy.cn;
            if ($invalid_referer) {
                return 403;
            }
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

2. 下载限速

配置如下:

location / {
    limit_rate 1m; //限制每秒下载速度
    limit_rate_after 30m; // 超过30秒之后再下载
}

3. IP黑名单

(1). 基本语法

# 屏蔽单个ip访问
deny 192.168.0.1;
# 允许单个ip访问
allow 192.168.0.1;
# 屏蔽所有ip访问
deny all;
# 允许所有ip访问
allow all;
#屏蔽整个段即从123.0.0.1到123.255.255.254访问的命令
deny 123.0.0.0/8
#屏蔽IP段即从123.45.0.1到123.45.255.254访问的命令
deny 124.45.0.0/16
#屏蔽IP段即从123.45.6.1到123.45.6.254访问的命令
deny 123.45.6.0/24

(2). 作用范围问题

 A. http{}语句块,所有网站

 B. server{}语句块,单个网站

 C. server下location语句块,单个网站下的某个映射规则

配置如下:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;    
    #1. 作用于所有网站
    deny 192.168.3.9;
    server {
        listen       8080;
        server_name   localhost;
        #2.作用于单个网站
        #deny 192.168.3.9;
        location / {
            #3. 作用于单个网站下的某个规则
            #deny 192.168.3.9;
            proxy_pass http://localhost:9001;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

效果如下:

(3). 可以配置成文件,通过include引入。 

 在nginx.conf所在的目录下新建denyIp.conf,在里面编写禁止IP的代码。

denyIp.conf配置

deny 192.168.3.9;
deny 192.168.137.1;

nginx.conf配置

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;    
    #1. 作用于所有网站
    include denyIp.conf;
    server {
        listen       8080;
        server_name   localhost;
        #2.作用于单个网站
        #include denyIp.conf;
        location / {
            #3. 作用于单个网站下的某个规则
            #include denyIp.conf;
            proxy_pass http://localhost:9001;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

 

标签:deny,跨域,192.168,server,Nginx,html,location,盗链,include
From: https://www.cnblogs.com/yaopengfei/p/17570461.html

相关文章

  • HTTP | 服务器防盗链
    什么是防盗链防盗链其实就是采用服务器端编程,通过url过滤技术实现的防止盗链的软件。比如file.abc.com/test.rar这个下载地址,如果没有装防盗链,别人就能轻而易举的在他的网站上引用这个地址。如果对file.abc.com这个站的服务器端编程,比如通过file.abc.com/test.rar?authcode=xx......
  • 制作nginx docker镜像
    制作NGINXDocker镜像流程1.准备工作在开始制作NGINXDocker镜像之前,我们需要确保已经满足以下条件:安装Docker:在主机上安装好Docker,确保可以正常运行。准备NGINX配置文件:将NGINX的配置文件放置在一个目录中,后续会将其复制到Docker镜像中。2.创建DockerfileDockerfile是用......
  • Python监控Nginx 4、7层健康检查
    [root@acs-hk-ctos7-prod-01scripts]#catupstrem.py#!/usr/bin/envpython#-*-coding:utf-8-*-#@Time:2023/6/2517:18#@File:nginx_upstram.py#@Software:PyCharmimportrequestsfromurllib.requestimporturlopenimportos,socket,datetim......
  • Nginx常用配置
    一、基础配置userroot;worker_processes1;events{worker_connections10240;}http{log_format'$remote_addr-$remote_user[$time_local]''"$request"$......
  • nginx变量
    一、配置多个location匹配规则location^~/admin{alias/var/www/admin/;indexregist.html;}location/{root/data/html/;indexindex.htmlindex.html;}二、root和alias的区别root与alias主要区别在于nginx如何解释location后面的uri......
  • win nginx 用include管理多个配置文件,如部署多个web前端程序 配置host域名访问
    当要部署多个web程序时,先将web程序分文件夹到html, 1在conf目录下新建conf-test/test_web.conf内容为:server{ listen80; server_namewebone.com; location/{ roothtml/web1; indexindex.htmlindex.htm; }}server{ listen80; serve......
  • HTTP请求返回304状态码以及研究nginx中的304
    文章目录1.引出问题2.分析问题3.解决问题4.研究nginx中的3044.1启动服务4.2ETag说明4.3响应头Cache-Control 1.引出问题之前在调试接口时,代码总出现304问题,如下所示:2.分析问题HTTP304:NotModified是什么意思?标准解释是:NotModified客户端有缓......
  • nginx反代配置tips
    nginx轮训导致验证码不正确在upstream里添加ip_hash;,例子:http{upstreamtest{#这样同一台电脑会一直访问到同一台机器ip_hash;server172.0.0.1:8080;}}静态资源访问出错在location里重写header:server{location/{......
  • Nginx 虚拟主机与域名解析
       监听不同域名配置nginx.cfgworker_processes1;#允许进程数量,建议设置为cpu核心数或者auto自动检测,注意Windows服务器上虽然可以启动多个processes,但是实际只会用其中一个events{#单个进程最大连接数(最大连接数=连接数*进程数)#根据硬件调整,和前面工作进......
  • docker nginx部署前端项目
    使用Docker部署前端项目介绍Docker是一个开源的容器化平台,可以帮助开发人员轻松地构建、打包和部署应用程序。它提供了一种简单的方式来创建和管理容器,使开发人员能够快速部署应用程序,并确保在不同的环境中具有相同的运行方式。在本文中,我们将探讨如何使用Docker来部署前端项目......