• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

【Linux运维架构】第十篇 Nginx常用基础模块

Linux架构 lixian 4年前 (2020-02-22) 842次浏览 3个评论 扫描二维码
文章目录[隐藏]

一、Nginx目录索引模块

1.目录索引模块

ngx_http_autoindex_module模块处理以斜杠字符(’/’)结尾的请求,并生成目录列表。
当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。
nginx默认是不允许列出整个目录浏览下载。

2.autoindex常用参数

autoindex_exact_size off;
默认单位是bytes,实际大小。修改为off,显示出文件的大概大小,单位是kB或者MB或者GB。

autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。修改为on, 显示的文件时间为文件的最后修改时间。

3.中文乱码参数

charset utf-8;
默认中文是乱码,添加该参数可以解决乱码问题

4.目录索引实例

访问www.book.com,出现正常网站页面,访问www.book.com/download,出现目录页面。

server {
        listen       80;
        server_name  www.book.com;
        charset utf-8;

    location /         {
        root   /code/book;
        index  index.html index.htm;
                       }

    location /download {
        root /code/book;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
}
}

【Linux运维架构】第十篇 Nginx常用基础模块

二、Nginx状态监控模块

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

配置实例

server {
        listen       80;
        server_name  www.book.com;
    location / {
        root   /code/book;
        index  index.html index.htm;
    }
    location /status {
        stub_status;
    }
}

【Linux运维架构】第十篇 Nginx常用基础模块

状态详解:
Active connections: 2 ———– # 当前活动的连接数
server accepts handled requests
373 373 695
Reading: 0 Writing: 1 Waiting: 1

accepts ———– # 当前的总连接数TCP
handled ———– # 成功的连接数TCP
requests ———– # 总的http请求数
Reading ———– # 请求
Writing ———– # 响应
Waiting ———– # 等待的请求数,开启了keepalive

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

三、Nginx访问控制模块

基于IP的访问控制模块: http_access_module

1)访问控制配置示例,拒绝指定的IP,其他全部允许

server {
        listen       80;
        server_name  www.book.com;

    location / {
        root   /code/book;
        index  index.html index.htm;
        deny 10.0.0.1;
        allow all;
    }
}

2) 访问控制配置示例, 只允许谁能访问, 其它全部拒绝

server {
        listen       80;
        server_name  www.book.com;

    location / {
        root   /code/book;
        index  index.html index.htm;
        allow 10.0.0.1;
        deny all;
    }
}

四、Nginx登陆认证模块

(1).需要安装httpd-tools,该包中携带了htpasswd命令

[root@web01 ~]# yum install httpd-tools

(2).创建新的密码文件, -c创建新文件,相当于覆盖文件 , -b允许命令行输入密码
如果没有加绝对路径那么密码文件必须要放在/etc/nginx/目录下,如果要写其他路径,在配置文件中要写绝对路径

[root@web01 ~]# htpasswd /etc/nginx/auth_conf lx
New password: 
Re-type new password: 
Updating password for user lx
[root@web01 ~]# cat /etc/nginx/auth_conf 
lx:$apr1$lBjA5o6T$BglppRiHeDJ7U0R/Zo0gh1

(3).编写conf配置文件

server {
        listen       80;
        server_name  www.book.com;

    location / {
        root   /code/book;
        index  index.html index.htm;
        auth_basic "Auth access Blog Input your Passwd!";
        auth_basic_user_file auth_conf;
    }

【Linux运维架构】第十篇 Nginx常用基础模块
(4).实例:在站点内某个页面设置密码认证

[root@web01 /etc/nginx/conf.d]# cat host.conf 
server{
	listen 80;
	server_name www.book.com;
	
	location / {
	root /code/youxi;
	index index.html;
}
	location /sc {
	root /code/youxi;
	index index.html;
	auth_basic "Auth access Blog Input your Passwd!";
        auth_basic_user_file auth_conf;
	}
}

浏览器访问www.book.com可以直接访问,不需要密码认证,但是如果访问www.book.com/sc/index.html 会密码认证。
location 后面的/sc 并不是根目录下的sc,而是浏览器域名输入的/sc,然后把/sc放到下面root对应的/code/youxi/sc,再去下面寻找index.html。

五、Nginx连接限制模块

1.语法

设置限制的空间
调用模块 ,空间里的内容 ,空间=空间名字:空间大小
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http

调用上面的空间
Syntax: limit_conn zone number;
Default: —
Context: http, server, location

2.配置

nginx配置文件http层

http {
	... ...
	limit_conn_zone $remote_addr zone=conn_zone:10m;
	... ...
}

server层:

server {
	... ...
	#设置共享内存区域和设置最大允许连接数。当超过此限制时,服务器将返回 错误 以回复请求。
	limit_coon conn_zone 1;
}

六、Nginx请求限制模块

1.语法

设置限制请求的空间
模块 空间里保存的内容 空间=空间名称:大小 速率 1r/s
Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default: —
Context: http

调用上面空间
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default: —
Context: http, server, location

2.配置

limit_req_zone $remote_addr zone=req_zone:1m rate=1r/s;
server {
    listen 80;
    server_name www.host1.com;

    location / {
        root /code;
        index index.html;
        limit_req zone=req_zone;
        limit_req_status 412;
    }
}

3.测试请求限制

#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

[root@web02 conf.d]# 

七、Nginx location

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

1.语法

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

2.location匹配符

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

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 ^~";
    }
}

本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:【Linux运维架构】第十篇 Nginx常用基础模块
喜欢 (2)

您必须 登录 才能发表评论!

(3)个小伙伴在吐槽
  1. 博主,真帅,求好看的图篇
    匿名2020-03-06 11:47
  2. 来了老弟
    匿名2020-03-06 11:46
  3. Great content! Super high-quality! Keep it up! :)