nginx模块

发布时间:2022-06-26 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了nginx模块脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

nginx模块

Nginx中的模块(Python模块)

示例:
location / {
    autoindex on;
}

1、启用或禁用目录列表输出
语法:	autoindex on | off;
默认:	autoindex off;
语境:	http,server,location
2、指定是否应在目录列表中输出确切的文件大小(四舍五入)
语法: autoindex_exact_size on | off;
默认: autoindex_exact_size on;
语境: http,server,location
3、设置目录列表的格式
语法: autoindex_format html | xml | json | jsonp;
默认: autoindex_format html;
语境: http,server,location
4、指定目录列表中的时间是否应以本地时区或UTC输出
语法: autoindex_localtime on | off;
默认: autoindex_localtime off;
语境: http,server,location

询问限制模块

allow : 允许IP访问
deny : 禁止IP访问

案例1:只允许192.168.15.1来访问。
	1、允许192.168.15.1来访问
		allow 192.168.15.1;
					
	2、禁止其他所有IP来访问
		deny all;
			
案例2:只允许192.168.15.0来访问。
	1、允许192.168.15.0来访问
		allow 192.168.15.0/24;
				
	2、禁止其他所有IP来访问
		deny all;
				
案例3:要求禁止192.168.15.1来访问。
	1、禁止192.168.15.1来访问
		deny 192.168.15.1;
				
	2、允许其他所有的IP来访问
		allow all;

认证模块

yum install httpd-tools -y
	
Syntax:	auth_basic string | off;	如果跟string,就代表开启。
Default:	
auth_basic off;
Context:	http, server, location, limit_except

1、生成密码
[root@web02 conf.d]# htpasswd -c /etc/nginx/auth chenyang

2、配置加密
auth_basic "This is Auth Basic!";
auth_basic_user_file /etc/nginx/auth;
在命令行中的访问方式
[root@m01 ~]# curl -H'Host: index.test.com' http://chenyang:123456@192.168.15.8

Nginx状态模块

location /status {
	stub_status;
}

禁用IP和开放IP访问

allow	: 允许IP访问
deny	:禁止IP访问

案例1:只允许192.168.15.1来访问。
	
	1、允许192.168.15.1来访问
	
		allow 192.168.15.1;
		
	2、禁止其他所有IP来访问
	
		deny all;

案例2:只允许192.168.15.0来访问。

	
	1、允许192.168.15.0来访问
	
		allow 192.168.15.0/24;
	
	2、禁止其他所有IP来访问
	
		deny all;
	
案例3:要求禁止192.168.15.1来访问。

	1、禁止192.168.15.1来访问
	
		deny 192.168.15.1;
	
	2、允许其他所有的IP来访问
	
		allow all;

目录索引模块

# 开启目录索引
autoindex on;
# 格式化文件大小
autoindex_exact_size off;
# 输出的格式
autoindex_format html;
# 使用时区
autoindex_localtime on;

限制连接数模块

1、创建一个内存空间存放访问者的IP
2、设置每一个访问者的同时连接次数

示例:
http {
	# 创建一个叫addr的空间,主要用来存放客户端ip,大小为10m
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    ...
    server {
        ...
        location /download/ {
        	#调用addr的空间,限制连接数为1
            limit_conn addr 1;
        }
        
$binary_remote_add:客户端IP  cat /etc/nginx/nginx.conf中log_format下面的第一个
zone=addr:指定一个名字
10m:创建空间的大小

知识储备:
	ab : 创建请求的命令,(yum install httpd-tools -y )
		-c : 设置并发
		-n :  设置请求的总数
	ab -c 200 -n 10000 http://game.test.com/   #200个并发,10000个请求

限制请求数模块

1、创建一个内存空间存放访问者的IP

2、设置每一个访问者的同时请求次数
[root@web02 conf.d]# cat game.conf 
# 创建一个叫linux的空间,主要用来存放客户端IP,大小给10M
limit_conn_zone $remote_addr zone=linux:10m;

示例:

http {
	limit_req_zone $remote_addr  zone=python:10m rate=1r/s;

	server {
		server_name game.test.com;
		listen 80;
		location / {
			# 调用linux空间, 限制连接数为1
			# limit_conn linux 1;
			limit_req zone=python burst=5;
			root /usr/share/nginx/html5-mario;
			index index.html;
		}
	}
}


ab : 创建请求的命令,(yum install httpd-tools -y )

	-c : 设置并发
	-n :  设置请求的总数

脚本宝典总结

以上是脚本宝典为你收集整理的nginx模块全部内容,希望文章能够帮你解决nginx模块所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:并发