美文网首页
nginx常用模块

nginx常用模块

作者: 魔曦帝天 | 来源:发表于2020-01-09 11:29 被阅读0次

PS: Nginx中的http、server、location之间的关系是?

http       标签主要用来解决用户的请求与响应。
server     标签主要用来响应具体的某一个网站。
location   标签主要用于匹配网站具体url路径。

http{} 层下允许有多个Server{},可以有多个网站.
一个Server{} 下又允许有多个location{} 每个网站的uri路径不同,所以要分别进行匹配.

nginx实现访问控制,基于来源IP控制、基于用户名密码控制

示例一.允许特定的IP访问(10.0.0.1 ),其他全部拒绝

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;                #开启目录索引,提供下载
    autoindex_exact_size off;    #以人性化方式显示大小
    autoindex_localtime on;      #与本地时间保持一致 </pre>

      location / {
              index index.html;
     }

      location /centos {
              allow 10.0.0.1/32;
              deny all;
     }

}

示例二.拒绝特定的IP访问(10.0.0.100),其他全部允许

注意:deny和allow的顺序,默认情况下,从第一条规则进行匹配 如果匹配成功,则不继续匹配下面的内容。 如果匹配不成功,则继续往下寻找能匹配成功的内容。

location / {
    index index.html;
}

location /centos {
    deny 10.0.0.100/32;
    allow all;
}

nginx实现限速 ( 下载限速 限制单位时间内的Http请求 连接限制 )

1.请求频率限制 Http

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
limit_req_zone $binary_remote_addr zone=req_od:10m rate=1r/s;
#第一个参数:$binary_remote_addr表示通过这个标识来做限制,限制同一客户端ip地址。 #第二个参数:zone=req_one:10m表示生成一个大小为10M,名为req_one的内存区域,用来存储访问的频次信息。 #第三个参数:rate=1r/s表示允许相同标识的客户端的访问频次,这里限制的是每秒1次,还可以30r/m。
server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;                #开启目录索引,提供下载
    autoindex_exact_size off;    #以人性化方式显示大小
    autoindex_localtime on;      #与本地时间保持一致
    limit_req zone=req_od burst=3 nodelay;
#第一个参数:zone=req_one 设置使用哪个配置区域来做限制,与上面limit_req_zone 里的name对应。 #第二个参数:burst=3,设置一个大小为3的缓冲区,当有大量请求过来时,超过了访问频次限制的请求可以先放到这个缓冲区内。 #第三个参数:nodelay,超过访问频次并且缓冲区也满了的时候,则会返回503,如果没有设置,则所有请求会等待排队。
        location / {
              index index.html;
       }
        location /centos {
              auth_basic "hello test";
              auth_basic_user_file "/etc/nginx/auth_conf";
       }
}

2.连接限制

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;            #开启目录索引,提供下载
    autoindex_exact_size off;    #以人性化方式显示大小
    autoindex_localtime on;    #与本地时间保持一致
  limit_conn conn_od 2;
        location / {
              index index.html;
       }
        location /centos {
              auth_basic "hello test";
              auth_basic_user_file "/etc/nginx/auth_conf";
       }
}

3.速率限制

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
limit_conn_zone $binary_remote_addr zone=conn_od:10m;

server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;            #开启目录索引,提供下载
    autoindex_exact_size off;    #以人性化方式显示大小
    autoindex_localtime on;    #与本地时间保持一致
    limit_conn conn_od 2;
    limit_rate_after 100m;
    limit_rate 100k;

        location / {
            index index.html;
        }
        location /centos {
            auth_basic "hello test";
            auth_basic_user_file "/etc/nginx/auth_conf";
        }
}

相关文章

  • nginx 2

    1 nginx 常用模块整理 nginx 常用模块整理1 http核心模块 ngx_http_core_modu...

  • nginx与lvs详解

    详细描述常见nginx常用模块和模块的使用示例 nginx常见的模块分类: 核心模块:core moduleNgi...

  • Nginx与lvs详解

    详细描述常见nginx常用模块和模块的使用示例 nginx常见的模块分类: 核心模块:core moduleNgi...

  • Nginx配置referer校验,实现简单的防盗链详解

    1、Nginx Referer模块 nginx模块ngx_http_referer_module通常用于阻挡来源非...

  • nginx常用模块使用及原理了解

    nginx常用模块了解及使用 nginx常用模块介绍及使用 对客户端进行限制相关配置-预防攻击 一般是下面的需求:...

  • nginx 的 rewrite

    其实nginx的rewrite模块是在日常nginx配置中十分常用的模块,应该是除了 real_ip 模块、匹配时...

  • nginx常用模块

    nginx常用模块 1、日志模块 ngx_http_log_module 2、压缩传输模块 ngx_http_gz...

  • Nginx常用模块

    本文介绍了几个Nginx常用模块的使用方法。 ngx_http_ssi_module 官方说明文档: 要点: 在 ...

  • Nginx 常用模块

    Linux System Environment 标注:本章节教程以Nginx以源码编译安装环境讲解,默认Ngin...

  • Nginx常用模块

    第一章 目录索引 1.1 应用场景 可以使用nginx作为简易的文件下载服务器 1.2 参数说明 1.3 配置文件...

网友评论

      本文标题:nginx常用模块

      本文链接:https://www.haomeiwen.com/subject/zgjsoctx.html