nginx模块

nginx模块

官方模块和第三方模块

官方模块

1、--with-http_stub_status_module:nginx的客户端状态

http_stub_status_module配置

语法:stub_status;

配置段:server、location

打开文件/etc/nginx/conf.d/default.conf

检查配置语法

nginx -tc /etc/nginx/nginx.conf 

重载服务

nginx -s reload -c /etc/nginx/nginx.conf

打开浏览器访问

http://192.168.137.11/mystatus

上面图片中内容介绍

  • Active connections:nginx当前活跃的连接数
  • server accepts handled requests:三个数分别表示nginx接收握手的总次数、nginx处理的连接总数、总请求数
  • Reading:正在读的
  • Writing:正在写的
  • Waiting:正在等待,开启keeplive后

2、--with-http_random_index_module:目录中选择一个随机主页

random_index_module配置

语法:random_index on|off;

默认:random_index off;

配置段:location

在/opt/app目录下准备不同的主页

重载nginx后,多次访问首页会显示不同主页

3、--with-http_sub_module:HTTP内容替换

http_sub_module配置

  • 替换内容

语法:sub_filter string replacement;

配置段:http、server、location

<html>
<head>
        <meta charset="utf-8">
</head>
<body>
<h1>shhnwangjian</h1></p>
<h1>shhnwangjian</h1></p>
<h1>shhnwangjian</h1>
</body>
</html>

  • 缓存场景

语法:sub_filter_last_modified on|off;

默认:sub_filter_last_modified off;

配置段:http、server、location

  • 匹配,匹配第一个字符串,off匹配所有内容

语法:sub_filter_once on|off;

默认:sub_filter_once on;

配置段:http、server、location

4、ngx_http_limit_conn_module 连接频率限制

limit_conn_module配置

  • 语法:limit_conn_zone $key zone=name:size;

配置段:http

该指令描述会话状态存储区域。键的状态中保存了当前连接数,键的值可以是特定变量的任何非空值(空值将不会被考虑)。$key定义键,zone=name定义区域名称,后面的limit_conn指令会用到的。size定义各个键共享内存空间大小。

  • 语法:limit_conn zone number

配置段:http、server、location

http://www.ttlsa.com/nginx/nginx-limited-connection-number-ngx_http_limit_conn_module-module/

5、ngx_http_limit_req_module请求频率限制

limit_req_module配置

  • 语法:limit_req_zone $key zone=name:size rate=rate;

配置段:http

设置一块共享内存限制域用来保存键值的状态参数。 特别是保存了当前超出请求的数量。 键的值就是指定的变量(空值不会被计算)。

  • 语法: limit_req zone=name [burst=number] [nodelay];

配置段: http, server, location

[burst=number] 表示 限制平均每秒不超过一个请求,同时允许超过频率限制的请求数不多于number个

如果不希望超过的请求被延迟,可以用nodelay参数

http://www.ttlsa.com/nginx/nginx-limiting-the-number-of-requests-ngx_http_limit_req_module-module/

6、ngx_http_access_module:基于IP的访问控制

http_access_module配置

  • allow

语法:     allow address | CIDR | unix: | all;

配置段:     http, server, location, limit_except

  • deny

语法:     deny address | CIDR | unix: | all;

配置段:     http, server, location, limit_except

http_access_module局限性

方法一:采用别的HTTP头信息控制访问,如:HTTP_X_FORWORD_FOR

方法二:结合geo模块

方法三:通过HTTP自定义变量传递

7、ngx_http_auth_basic_module:用户认证

http_auth_basic_module配置

  • 语法:     auth_basic string | off;

默认值:     auth_basic off;

配置段:     http, server, location, limit_except

默认表示不开启认证,后面如果跟上字符,这些字符会在弹窗中显示。

  • 语法:     auth_basic_user_file file;

默认值:     —

配置段:     http, server, location, limit_except

用户密码文件

http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

yum install httpd-tools -y

使用htpasswd命令生成密码文件

htpasswd -c ./auth_conf shhnwangjian
原文地址:https://www.cnblogs.com/shhnwangjian/p/7788183.html