Nginx 六个模块;location配置

一 Nginx常用模块

1.目录索引模块 ngx_http_autoindex_moudle

ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求,并生成目录列表。

当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。

1)语法

Syntax:	autoindex on | off;
Default:	autoindex off;
Context:	http, server, location

2)配置

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf 
server {
    listen 80;
    server_name www.autoindex.com;
    charset utf8;

    location / {
        root /code/autoindex;
        autoindex on;
    }
}

3)访问网站正常,加download跳转目录页面

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf 
server {
    listen 80;
    server_name www.autoindex.com;
    charset utf8;

    location / {
        root /code/autoindex;
        index index.html;
    }

    location /download {
        root /code/autoindex;
        autoindex on;
    }
}

#创建站点目录
[root@web01 ~]# mkdir /code/autoindex/download -p
[root@web01 ~]# echo "测试autoindex模块" > /code/autoindex/index.html

#访问
http://www.autoindex.com/		为主站
http://www.autoindex.com/download/		为下载文件的目录

4)常用优化参数

#显示文件字节大小,默认是显示字节大小,配置为off之后,显示具体大小 M/G/K
Syntax:	autoindex_exact_size on | off;
Default:	autoindex_exact_size on;
Context:	http, server, location

#显示文件的修改的具体时间,默认显示的时间与真实时间相差8小时,所以配置 on
Syntax:	autoindex_localtime on | off;
Default:	autoindex_localtime off;
Context:	http, server, location

5)完整配置

[root@web01 ~]# cat /etc/nginx/conf.d/www.autoindex.com.conf 
server {
    listen 80;
    server_name www.autoindex.com;
    charset utf8;

    location / {
	root /code/autoindex;
	index index.html;
    }

    location /download{
	root /code/autoindex;
	autoindex on;   #开启目录模块            
	autoindex_exact_size off;  #显示文件具体大小,on 单位是bytes  off 单位 K/M/G
	autoindex_localtime on;  	#显示文件最后修改时间
    }
}

2.Nginx访问控制模块 ngx_http_access_module

1)语法

#允许访问的语法
Syntax:	allow address | all;
Default:	—
Context:	http, server, location, limit_except

#拒绝访问的语法
Syntax:	deny address | all;
Default:	—
Context:	http, server, location, limit_except

#如果配置允许,则也要配置拒绝;配置拒绝可以单独配置

2)配置访问控制示例

1>拒绝指定的IP,其他全部允许
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf 
server {
    listen 80;
    server_name www.autoindex.com;
    charset utf8;

    location / {
        root /code/autoindex;
        index index.html;
    }

    location /nginx_status {
        stub_status;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        deny 10.0.0.1;  #禁止指定10.0.0.1访问
        allow all;     #允许其他所有人访问
    }
}
2>只允许指定IP能访问, 其它全部拒绝
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf 
server {
    listen 80;
    server_name www.autoindex.com;
    charset utf8;

    location / {
        root /code/autoindex;
        index index.html;
    }

    location /download {
        root /code/autoindex;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        allow 10.0.0.1;  # 允许10.0.0.1访问
        deny all;     #  其他全部拒绝。如果使用all,一定放在最后面
    }
}
3>只允许10.0.0.1访问,拒绝该网段其他IP
[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf 
server {
    listen 80;
    server_name www.autoindex.com;
    charset utf8;

    location / {
        root /code/autoindex;
        index index.html;
    }

    location /download {
        root /code/autoindex;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        allow 10.0.0.1;   #只允许10.0.0.1访问
        deny 10.0.0.0/24;    #拒绝该网段其他IP。如果使用all,一定放在最后面
    }
}

3.Nginx访问认证模块 ngx_http_auth_basic_module

1)语法

#开启的登录认证,没用
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

2)创建密码文件

#创建密码文件需要用到 htpasswd
[root@web01 ~]# htpasswd -c /etc/nginx/auth_basic lhd
New password: 
Re-type new password: 
Adding password for user lhd

#添加一个登录用户
[root@web01 ~]# htpasswd /etc/nginx/auth_basic egon   #创建第二对密码不加 -c 参数
New password: 
Re-type new password: 
Adding password for user egon

#密码文件内容
[root@web01 ~]# cat /etc/nginx/auth_basic
lhd:$apr1$A7d4BWYe$HzlIA7pjdMHBDJPuLBkvd/
egon:$apr1$psp0M3A5$601t7Am1BG3uINvuBVbFV0

3)配置访问登录

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf 
server {
    listen 80;
    server_name www.autoindex.com;
    charset utf8;
    access_log /var/log/nginx/www.autoindex.com.log main;

    location / {
        root /code/autoindex;
        index index.html;
    }

    location /download {
        root /code/autoindex;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        auth_basic "please password!";
        auth_basic_user_file /etc/nginx/auth_basic;
    }
}

4.Nginx状态监控模块 ngx_http_stub_status_module

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

1)语法

Syntax:	stub_status;
Default:	—
Context:	server, location

2)配置

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf 
server {
    listen 80;
    server_name www.autoindex.com;
    charset utf8;
    access_log /var/log/nginx/www.autoindex.com.log main;

    location / {
        root /code/autoindex;
        index index.html;
    }

    location /download {
        root /code/autoindex;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        auth_basic "please password!";
        auth_basic_user_file /etc/nginx/auth_basic;
    }

    location = /basic_status {
        stub_status;    # 状态监控
    }
}

3)访问

#访问 http://www.autoindex.com/basic_status

#nginx七种状态
Active connections: 2 
server accepts handled requests
 		2 		2 		2 
Reading: 0 Writing: 1 Waiting: 1

Active connections		#活跃的连接数
accepts					#TCP连接总数
handled					#成功的TCP连接数
requests				#成功的请求数
Reading					#读取的请求头
Writing					#响应
Waiting					#等待的请求数,开启了keepalive

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

4)监控网站的PV

[root@web01 ~]# curl -s http://www.autoindex.com/basic_status | awk 'NR==3 {print $3}' 61

5. 连接限制模块 ngx_http_limit_conn_module

1) 语法

#设置限制的空间 
Syntax: limit_conn_zone key zone=name:size; 
Default: — 
Context: http
limit_conn_zone	 #设置空间的模块
key				 #指定空间存储的内容
zone			 #指定空间
=name 			 #空间名字
:size			 #空间的大小

#调用限制的空间 
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
limit_conn 		 #调用空间的模块 
zone 			 #空间的名字 
number			 #指定可以同时连接的次数

2) 配置

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf 
limit_conn_zone $remote_addr zone=conn_zone:10m; 
server { 
	listen 80; 
	server_name www.autoindex.com;
    charset utf8;
    limit_conn conn_zone 1; 
    
    location / {
   	 	root /code/autoindex; 
    	index index.html;
     }
}

6. 请求限制模块 (限制request请求) ngx_http_limit_req_module

1)语法

#设置空间的语法 
Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default: — 
Context: http 
limit_req_zone		 #设置空间的模块 
key					 #空间存储的内容 
zone 				 #指定空间 
=name 				 #空间的名字 
:size 				 #空间的大小 
rate=rate [sync];    #读写速率

#调用的语法 
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default: — 
Context: http, server, location 
limit_req 			 #调用控件模块
zone=name			 #指定空间=空间的名字 
[burst=number]		 #允许多请求几次
[nodelay | delay=number] #延时

2)配置

[root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf

limit_conn_zone $remote_addr zone=conn_zone:10m; 
limit_req_zone $remote_addr zone=req_zone:10m rate=1r/s;
server { 
	listen 80; 
	server_name www.autoindex.com;
    charset utf8; limit_conn conn_zone 1;
    limit_req zone=req_zone;
    #limit_req zone=req_zone burst=5 nodelay; 
    
    location / {
    	root /code/autoindex; 
    	index index.html;
        } 
}

3) 测试

[root@web01 ~]# ab -n 20000 -c 20 http://www.autoindex.com/index.html

二、nginx的location配置

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

1. 语法

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

2.location匹配符

3.Locaiton应用场景

# 通用匹配,任何请求都会匹配到 
location / { 
	... 
	}
	
# 严格区分大小写,匹配以.php结尾的都走这个location 
location ~ .php$ { 
	... 
	}
	
# 严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ .jsp$ {
	... 
	}

# 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
location ~* .*.(jpg|gif|png|js|css)$ {
	...
    }	

3.优先级验证

[root@web01 ~]# vim /etc/nginx/conf.d/youxianji.conf 
server {
	listen 80; 
	server_name linux.test.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 ^~"; 
        } 
 }
原文地址:https://www.cnblogs.com/caodan01/p/14716680.html