nginx常用模块 - Go语言中文社区

nginx常用模块


nginx常用模块

1.目录索引模块

ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求,并生成目录列表。
当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。
  • nginx 默认是不允许列出整个目录浏览下载
Syntax:    autoindex on | off;
Default:    autoindex off;
Context:    http, server, location
 
# autoindex常用参数
autoindex_exact_size off;
默认为on, 显示出文件的确切大小,单位是bytes。
修改为off,显示出文件的大概大小,单位是kB或者MB或者GB。
 
autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
修改为on, 显示的文件时间为文件的服务器时间。
 
charset utf-8,gbk;
默认中文目录乱码,添加上解决乱码。
  • 配置示例
server {
        listen 10.0.0.8:80;
        server_name www.qiqi.com;
        access_log /var/log/nginx/www.qiqi.com.access.log main;
        charset utf-8,gbk;

        location / {
                root /naonao/h5_games;
                index index.html;

        }
        location /download {
                alias /dir;
                autoindex on;
                autoindex_exact_size on;
                autoindex_localtime on;

        }

        location /favicon.ico {
                access_log off;
                return 200;
    }

        location /js/common.js {
                access_log /var/log/nginx/js.log main;
    }
}

2.nginx状态模块

ngx_http_stub_status_module模块提供对基本状态信息的访问。 默认情况下不构建此模块,应使用--with-http_stub_status_module`配置参数启用它

Syntax: stub_status;
Default: —
Context: server, location
server {
    listen 80;
    server_name www.qiqi.com;
    access_log off;
 
    location /nginx_status {
        stub_status;
    }
}

访问浏览器

http://www.qiqi.com/nginx_status
Active connections: 2 
server accepts handled requests
         373      373     695 
Reading: 0 Writing: 1 Waiting: 1 


Active connections  # 当前活动的连接数
accepts             # 当前的总连接数TCP
handled             # 成功的连接数TCP
requests            # 总的http请求数

Reading             # 请求
Writing             # 响应
Waiting             # 等待的请求数,开启了keepalive

# 注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout  0;   # 类似于关闭长连接
keepalive_timeout  65;  # 65s没有活动则断开连接

nginx访问控制

基于IP的访问控制 http_access_module
基于用户登陆认证 http_auth_basic_module

#允许配置语法
Syntax:    allow address | CIDR | unix: | all;
Default:    —
Context:    http, server, location, limit_except
 
#拒绝配置语法
Syntax:    deny address | CIDR | unix: | all;
Default:    —
Context:    http, server, location, limit_except
  • 访问控制配置示例,拒绝指定的IP,其他全部允许
server {
    listen 80;
    server_name www.qiqi,com;
    access_log off;
 
    location /nginx_status {
        stub_status;
        deny 10.0.0.1;
        allow all;   
    }
}
  • 访问控制配置示例,只允许谁能访问,其他全部拒绝
server {
    listen 80;
    server_name www.qiqi.com;
    access_log off;
 
    location /nginx_status {
        stub_status;
        allow   10.0.0.0/24;
        allow   127.0.0.1;
        deny    all;
    }
}

登录认证模块

  • 基于用户登录认证配置语法
#访问提示字符串
Syntax: auth_basic string| off;
Default: auth_basic off;
Context: http, server, location, limit_except
 
#账户密码文件
Syntax: auth_basic_user_file file;
Default: -
Context: http, server, location, limit_except
  • 基于用户登录认证配置实践
#1.需要安装httpd-tools,该包中携带了htpasswd命令
[root@web01 ~]# yum install httpd-tools
#2.创建新的密码文件, -c创建新文件 -b允许命令行输入密码
[root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf zls zls #文件路径随意,验证时登录域名验证   


server {
        listen 10.0.0.8:80;
        server_name www.qiqi.com;
        access_log /var/log/nginx/www.qiqi.com.access.log main;
        charset utf-8,gbk;
        location /nginx_status{
                stub_status;
                stub_status;
                auth_basic "请输入密码";
                auth_basic_user_file auth_conf;
        }

    }

访问限制

在企业中经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,请求数、进行限制。

ngx_http_limit_conn_module模块可以根据定义的key来限制每个键值的连接数,如同一个IP来源的连接数。

limit_conn_module 连接频率限制

limit_req_module 请求频率限制

连接限制

vim /etc/nginx/nginx.conf #主配置文件

http {
    limit_conn_zone $remote_addr zone=suibian:10m;
}

限制请求数量

http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

server {
    location /search/ {
        limit_req zone=one burst=5 nodelay;
        limit_req_status 412;
        error_page 412 /xxx/err.html
    }
}
  • 使用ab工具进行压力测试
[root@oldboyedu ~]# yum install -y httpd-tools
[root@oldboyedu ~]# ab -n 20 -c 2  http://127.0.0.1/index.html
  • nginx日志结果
2018/10/24 07:38:53 [error] 81020#0: *8 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.driverzeng.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10"
2018/10/24 07:38:53 [error] 81020#0: *9 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.driverzeng.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10"
2018/10/24 07:38:53 [error] 81020#0: *10 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.driverzeng.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10"

nginx请求限制重定向

在nginx请求限制的过程中,我们可以自定义一个返回值,也就是错误页面的状态码。

默认情况下是503

server {
        listen 80;
        server_name module.driverzeng.com;
        charset utf-8,gbk;

        location / {
                root /code;
                index index.html index.htm;
                limit_req zone=req_zone burst=3 nodelay;
                limit_req_status 478
                #重定错误页面
                error_page 412 /err.html; #通常避免与过载冲突,定义412
        }
}

vim /code/err.html  
<img style='width:100%;height:100%;' src=https://www.driverzeng.com/zenglaoshi/478_page.png> #h5图片编写格式

nginx location 优先级

匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 3
!~ 区分大小写不匹配的正则 5
!~* 不区分大小写不匹配的正则 6
/ 通用匹配,任何请求都会匹配到

配置网站测试优先级

root@Nginx conf.d]# cat testserver.conf 
server {
    listen 80;
    server_name www.driverzeng.com;
    location / {
        default_type text/html;
        return 200 "location /";
    }
 
    location =/ {
        default_type text/html;
        return 200 "location =/";
    }
 
    location ~ / {
        default_type text/html;
        return 200 "location ~/";
    }
 
    # location ^~ / {
    #   default_type text/html;
    #   return 200 "location ^~";
    # }
}
版权声明:本文来源博客园,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.cnblogs.com/1naonao/p/11364506.html
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-11-09 10:40:19
  • 阅读 ( 1641 )
  • 分类:Go Web框架

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢