09@nginx服务常用模块详解

1|0nginx服务常用模块详解

nignx是一个高性能的WEB服务器,nginx支持多个模块。

1|1一、nginx的autoindex模块

ngx_http_autoindex_module,作用是生成一个目录列表

1|01、格式

Syntax: autoindex on | off; # 该选项的参数 Default: autoindex off; # 该选项的默认值 Context: http, server, location # 该选项可以使用的模块

1|02、开启目录索引

server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; #指定的目录位置 autoindex on; #开启目录索引模块 } }

1|03、格式化文件大小

server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; autoindex on; autoindex_exact_size off; #显示文件具体大小,on 单位是bytes off 单位 K/M/G #使用格式化文件大小 } }

1|04、显示本地时间

server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; autoindex on; autoindex_exact_size off; autoindex_localtime on; #显示文件最后修改时间 #指定显示本机显示时间 } }

1|05、案例

# 部署一个yum仓库 # 创建一个yum仓库目录 [root@web01 ~]#mkdir /usr/share/nginx/yum #编写模块文件内容 [root@web01 ~]# cat /etc/nginx/conf.d/yum.conf server { listen 80; server_name www.yum.com; #定义一个模块名称,指定域名解析www.yum.com location / { root /usr/share/nginx/yum; autoindex on; #显示王网页目录列表 } } # 重启nginx [root@web01 ~]# nginx -t #检测nginx配置文件 [root@web01 ~]# systemctl restart nginx # 编写yum源 [root@web01 yum.repos.d]# cat /etc/yum.repos.d/test.repo [nginx-test] name='test nginx' baseurl=http://www.yum.com/ # 注:需要解析www.yum.com # 测试 [root@web01 yum.repos.d]# yum clean all [root@web01 yum.repos.d]# yum makecache

1|2二、访问控制模块ngx_http_access_module

ngx_http_access_module: 允许限制对某些客户端地址的访问。

1|01、格式

# 允许访问 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

1|02、测试禁止一个IP

server { listen 80; server_name www.deny.com; location / { root /usr/share/nginx/yum; index 50x.html #nginx索引目录 deny 192.168.15.1; #拒绝192.168.15.1访问 allow all; #允许所有 } }

1|03、只允许某一个IP访问

server { listen 80; server_name www.deny.com; location / { root /usr/share/nginx/yum; index index.html; allow 192.168.15.10; #允许192.168.15.1访问 deny all; #拒绝所有访问 } }

1|04、允许某一个网段的IP来访问

server { listen 80; server_name www.deny.com; location / { root /usr/share/nginx/yum; index index.html; allow 192.168.15.0/24; #允许192.168.15.0/24网段访问 deny all; #拒绝所有访问 } }

1|05、案例

# 只允许公司内部人员(192.168.15.0/24)来访问下载目录 server { listen 80; server_name www.game.com; location /download { root /usr/share/nginx/game/; autoindex on; autoindex_exact_size off; autoindex_localtime on; allow 192.168.15.0/24; deny all; } location / { root /usr/share/nginx/game; index index.html; } }

1|3三、nginx监控状态模块 ngx_http_stub_status_module

ngx_http_status_module :监控模块最主要的是监控nginx服务

1|01、格式

Syntax: stub_status; Default: — Context: server, location #使用模块位置

1|02、测试案例

Active connections: 2 server accepts handled requests 2 2 1 Reading: 0 Writing: 1 Waiting: 1 Active connections: # 活跃的连接数 accepts # TCP连接总数 handle # 成功的TCP连接数 requests # 请求数 Reading # 读取请求头部 Writing # 放回给客户端的头部 Waiting # 等待的请求数 # 长链接和短连接 keepalive_timeout 0; [root@web01 conf.d]# cat status.conf server { listen 80; server_name www.status.com; location / { stub_status; } }

1|03、nginx 七种状态

Active connections: 2 server accepts handled requests 4 4 56 Reading: 0 Writing: 1 Waiting: 1 Active connections: # 活跃的连接数 accepts # TCP连接总数 handle # 成功的TCP连接数 requests # 请求数 Reading # 读取请求头部 Writing # 放回给客户端的头部 Waiting # 等待的请求数 #注意:一次tcp连接,可以发起多次请求; keepalive_timeout 0; #类似于关闭长连接 keepalive_timeout 65; #最长65秒没有活动则断开连接

1|4四、访问认证密码模块 ngx_http_auth_basic_module

浏览器访问增加一个密码。

  • 格式
# 指定密码简介 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
  • 案例
# 创建密码文件 [root@web01 conf.d]#yum install httpd-tools #使用创建密码命令先安装http-tools软件包 [root@web01 conf.d]# yum provides htpasswd #检查生成密码文件工具 [root@web01 nginx]# htpasswd -c /etc/nginx/string nginx #创建密码文件指定用户 [root@web01 nginx]# htpasswd /etc/nginx/password nginx #创建第二对密码不加 -c 参数 [root@web01 nginx]# htpasswd -c -b 1 /etc/nginx/string nginx #指定创建的密码 # nginx 配置文件 server { listen 80; server_name www.auth.basic.com; location / { root /usr/share/nginx/game; index index.html; #charset utf-8,gbk; charset 'utf-8'; auth_basic "简介"; auth_basic "please input password!"; #指定登录时显示简介内容 auth_basic_user_file /etc/nginx/string; #指定密码文件位置 } } # 测试登录

1|5五、连接限制模块 ngx_http_limit_conn_module与ngx_http_limit_req_module

ngx_http_limit_conn_module:限制连接数

ngx_http_limit_req_module:限制速率

1|01、限制连接数

# 调用限制连接模块 Syntax: limit_conn zone number; Default: — Context: http, server, location # 定义限制连接模块 Syntax: limit_conn_zone key zone=name:size; Default: — Context: http #设置限制的空间 #调用模块 空间里的内容 空间=空间名字:空间大小 #位置 /etc/nginx/conf.d http { ...... limit_conn_zone $binary_remote_addr zone=addr:10m; .... } #模块内部配置 server { location /download/ { limit_coon conn_zone 1; #设置共享内存区域和设置最大允许连接数。当超过此限制时,服务器将返回 错误 以回复请求。 } }

1|02、限制速率

# 调用 Syntax: limit_req zone=name [burst=number] [nodelay | delay=number]; Default: — Context: http, server, location # 定义格式 Syntax: limit_req_zone key zone=name:size rate=rate [sync]; Default: — Context: http #设置限制请求的空间 #位置 /etc/nginx/conf.d #模块 空间里保存的内容 空间=空间名称:大小 速率 1r/s http { ..... limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; ... } #模块配置 server { location /search/ { limit_req zone=one burst=5; } } nodelay : 延时请求

1|03、测试请求限制

# ab工具使用测试 #安装ab测试工具 [root@web02 conf.d]# yum install httpd-tools -y #检查ab测试工具是否安装成功 [root@web02 conf.d]#ab -V This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ #ab测试工具基本参数详解 -n ----------requests,用于指定压力测试总共的执行次数 -c ----------concurrency,用于指定的并发数 -t ----------timelimit,等待响应的最大时间(单位:秒) -b ----------windowsize,TCP发送/接收的缓冲大小(单位:字节) #使用ab工具测试 [root@web02 conf.d]# ab -n 20 -c 2 http://www.host1.com/ Server Software: nginx/1.16.1 Server Hostname: www.host1.com Server Port: 80 Document Path: / Document Length: 581 bytes Concurrency Level: 2 Time taken for tests: 0.007 seconds Complete requests: 20 Failed requests: 19 (Connect: 0, Receive: 0, Length: 19, Exceptions: 0) Write errors: 0 Non-2xx responses: 19 Total transferred: 8125 bytes HTML transferred: 4324 bytes Requests per second: 3056.70 [#/sec] (mean) Time per request: 0.654 [ms] (mean) Time per request: 0.327 [ms] (mean, across all concurrent requests) Transfer rate: 1212.68 [Kbytes/sec] received

1|04、对比

ngx_http_limit_conn_module : 限制连接数据 ngx_http_limit_req_module :限制访问的频率

1|6六、Nginx location

使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置, 多个location的优先级该如何区分

1|01.语法

Syntax: location [ = | ~ | ~* | ^~ | / ] uri { ... } location @name { ... } Default: — Context: server, location

1|02.location匹配符

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

1|03.验证location匹配顺序

[root@web02 conf.d]# vim testlocation.conf server { listen 80; server_name www.linux.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 ^~"; } }

1|04.验证访问文件

[root@web01 conf.d]# cat testserver.conf server { listen 80; server_name www.server.com; location / { root /code; } location ~ \.php$ { root /php; } location ~ \.jsp$ { root /jsp; } location ~* \.(jpg|gif|png|js|css)$ { root /pic; } location ~* \.(sql|bak|tgz|tar.gz|git)$ { root /package; } } www.server.com/1.PHP www.server.com/2.JPG www.server.com/3.jsp www.server.com/4.tGz www.server.com/5.Gif

__EOF__

本文作者ଲ小何才露煎煎饺
本文链接https://www.cnblogs.com/zeny/p/15121575.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   ଲ小何才露煎煎饺  阅读(100)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示