09@nginx服务常用模块详解

nginx服务常用模块详解

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

一、nginx的autoindex模块

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

1、格式
Syntax:		autoindex on | off;		# 该选项的参数
Default:	autoindex off;			# 该选项的默认值
Context:	http, server, location	# 该选项可以使用的模块
2、开启目录索引
server {
	listen 80;
	server_name localhost;       

	location / {
		root /usr/share/nginx/html;      #指定的目录位置
    		autoindex on;                #开启目录索引模块
	}
}
3、格式化文件大小
server {
	listen 80;
	server_name localhost;

	location / {
			root /usr/share/nginx/html;
    		autoindex on;
    		autoindex_exact_size off;    #显示文件具体大小,on 单位是bytes  off 单位 K/M/G       #使用格式化文件大小
	}
}
4、显示本地时间
server {
	listen 80;
	server_name localhost;
	location / {
		root /usr/share/nginx/html;
    	autoindex on;
		autoindex_exact_size off;
		autoindex_localtime on;      #显示文件最后修改时间  #指定显示本机显示时间
	}
}
5、案例
# 部署一个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




二、访问控制模块ngx_http_access_module

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

1、格式
# 允许访问
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

2、测试禁止一个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;                    #允许所有
        }

}
3、只允许某一个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;                    #拒绝所有访问
	}
}
4、允许某一个网段的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;                     #拒绝所有访问
	}
}
5、案例
# 只允许公司内部人员(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;
	}
	
}

三、nginx监控状态模块 ngx_http_stub_status_module

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

1、格式
Syntax:	stub_status;
Default:	—
Context:	server, location     #使用模块位置
2、测试案例
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;
	}
}

3、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秒没有活动则断开连接

四、访问认证密码模块 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;	  #指定密码文件位置

	}

}

# 测试登录

五、连接限制模块 ngx_http_limit_conn_module与ngx_http_limit_req_module

ngx_http_limit_conn_module:限制连接数

ngx_http_limit_req_module:限制速率

1、限制连接数
# 调用限制连接模块
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;   #设置共享内存区域和设置最大允许连接数。当超过此限制时,服务器将返回 错误 以回复请求。
    }
}
2、限制速率
# 调用
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 : 延时请求
3、测试请求限制


#  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





4、对比
ngx_http_limit_conn_module : 限制连接数据

ngx_http_limit_req_module :限制访问的频率

六、Nginx location

使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置, 多个location的优先级该如何区分
1.语法
Syntax:	location [ = | ~ | ~* | ^~ | / ] uri { ... }
		location @name { ... }
Default:	—
Context:	server, location
2.location匹配符
匹配符匹配规则优先级
=精确匹配1
^~以某个字符串开头2
~区分大小写的正则匹配3
~*不区分大小写的正则匹配4
/通用匹配,任何请求都会匹配到5
3.验证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 ^~";
    }
}

4.验证访问文件
[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
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值