nginx企业应用适配-3

简介: 一、ngx_hrrp_core_module二、ngx_hrrp_stub_status_module三、ngx_http_log_modul四、ngx_http_gzip_module

nginx企业应用适配-3

 

目录:

一、ngx_hrrp_core_module

二、ngx_hrrp_stub_status_module

三、ngx_http_log_modul

四、ngx_http_gzip_module

 

 

一、ngx_hrrp_core_module


1.ngx_hrrp_core_module模块

对客户端进行限制的相关配置


1)limit_ rate rate;

限制响应给客户端的传输速率,单位是bytes/second

默认值0表示无限制


2)limit_excelpt method ... { ...} ,仅用于location

限制客户端使用除了指定的请求方法之外的其它方法

method:GET, HEAD, POST, PUT, DELETE

MKCOL, COPY, MOVE, OPTIONS, PROPFIND,

PROPPATCH, LOCK, UNLOCK, PATCH

limit_except GET{

     allow 192.168.1.0/24;

     deny all;

}除了 GETHEAD之外其它方法仅允许192.168.1.0/24网段主机使用

OPTIONS:探测对方网站支持哪些方法。

例如:探测a.com网站

[root@centos7 ~] #curl -XOPTIONS http://www.a.com/

<html>

<head><title>405 Not Allowed</tit1e> </head>

<body bgcolor="white">

<center><h1>405 Not Allowed</h1></center>

<hr> <center>nginx</center>

</body>

</ html>

 

  location/ {

          root /data/sitea

          limit_exceptGET

               allow 192.168. 30.17/32;

              deny  all;

          }

  [root@centos7 ~]#curl -XOPTIONS http://www.a.com/

<html>

<head><title>405 Not Allowed</tit1e> </head>   //option即不支持

<body bgcolor="white">

<center><h1>405 Not Allowed</h1></center>

<hr> <center>nginx</center>

</body>

</ html>

 

  location/ {

          root /data/sitea

         limit_exceptGET

              allow 192.168. 30.17/32;

              deny  all;

          }

更换主机:192.168.30.27再次进行测试

[root@centos7 ~]#yy httpd

Loaded plugins: fastestmiror, langpacks

base                                     |I 3.6 kB 00:00:00

Loading mirror speeds from cached hostfile

Resolving Dependencies

-->Running transaction check

--->Package httpd.x86_64 0:2.4.6-67.e17.centos wi1l be installed

-->Processing Dependency: httpd-tools = 2.4.6-67.el7. centos for package: httpd-2.4.6-67 .el7.centos.x86_64

-->Processing Dependency: /etc/mi me.types for package: httpd-

entos.x86_64

-->Processing Dependency: libaprutil-1.so.0() (64bit)for package: httpd-2.4.6-67.e17.centos.x86_64

-->Processing Dependency -libapr-1.so.0()(64bit)for package: httpd-2.4.6-67.e17.centos.x86_64

kanapr-1.so.00(64bit) for package: httpd-2.4.6-67.e17 . centos.x86 64

-->Running transaction check

--->Package apr.x86_64 0:1.4.8-3.e17 will be installed

--->Packkage apr-uti 1.x86_64 0:1.5.2-6.e17 centos will be installe

--->Package httpd-tools.x86_64 0:2.4.6-67.el7.centos wi11 be installed

--->Package mailcap.noarch 0: 2.1. wi11 be installed

-->Finished Dependency Resolution

[root@centos7 ~]#systemctl start httpd

查看主机192.168.30.6

[root@centos7 ~] #curl -XOPTIONS http://192.168.30.27/

查看主机192.168.30.17

[root@centos7 ~] #curl -XOPTIONS -I http://192.168.30.27/

HTTP/1.1 200 OK

ate: Wed, 04 Ju1201809:17 :50 GMT

Server: Apache/2.4.6 (centos)

A11ow:OPTICNS,GET,HEAD,POST,TRACE

Content-Length:0

Content-Type: httpd/unix-di rectory

[root@centos7 ~] #curl -XOPTIONS -I http://192.168.30.7/

[root@centos7 ~] #cur1 -XOPTIONS -I http://192.168. 30.7/HTTP/1.1 405 Not A11owed

Server: nginx

Date: Wed, 04 Jul 2018 09:18:17 GMT

content -Type: text/html

Cơntent-Length: 166

Connection: keep-alive

[root@centos7 ~]#location


2.文件操作优化的配置


3)aio on | off | threads[= pool];

  是否启用aio功能


4)directio size | off;

当文件大于等于给定大小时,例如directio 4m ,同步(直接)写磁盘,而非写缓存


5)open_file__cache off;

open_ file_ cache max= N [inactive= time];

nginx可以缓存以下三E种信息:

 (1)文件元数据:文件的描述符、文件大小和最近一次的修改时间

 (2)打开的目录结构

 (3)没有找到的或者没有权限访问的文件的相关信息

 max=N :可缓存的缓存项上限;达到上限后会使用LRU算法实现管理

 inactive=time : 缓存项的非活动时长,在此处指定的时长内没有人访问、未被命中的或命中的次数少于open_file_cache__min__uses指令所指定的次数的缓存项即为非活动项,将被删除


6)open_file_cache_errors on| off;

是否缓存查找时发生错误的文件一类的信息

默认值为off


7)open_file_cache__min__uses number;

open_file_cache指令的inactivel数指定的时长内,至少被命中此处指定的次数方可被归类为活动项

默认值为1


8)open_file_cache_valid time;

缓存项有效性的检查频率

默认值为60s


3.ngx_http_access_module模块

实现基于ip的访问控制功能

1)allow address| CIDR| unix:| all;

2)deny address| CIDR | unix:| all;

 http, server, location, limit_ except

自上而下检查 ,一旦匹配,将生效,条件严格的置前示例:

示例:

location / {

  deny 192.168.1.1;

  allow 192.168.1.0/24;

  allow 10.1.1.0/16;

  allow 2001:0db8::/32;

  deny all;

}

    server_name www.a.com ;

    root/data/sitea;

    error page 404 /404. html;

              location / {

                        root data/sitea;

                        #a1low 192.168. 30.17 ;deny al1 ;

                        deny all;

              }

}

生效次序从上往下检查,一旦匹配就不用再进行查看。故应将条件严格、即条件越小的指令往前放。

[root@centos7 ~] #cur1 -XPUT -I http://192.168.30.7/

HTTP/1.1 403 Forbi dden

Server: nginx

Date :Wed, 04 Jul 2018 09: 31: 37 GMT

content-Type:text/htm1

content-Length:162

onnection:pen-alive

"a.com.conf" 11L 202C writtion

[root@centos7 ~] #cur1 -XPUT -I http://192.168.30.7/

HTTP/1.1 403 Forbi dden

Server: nginx

Date :Wed, 04 Jul 2018 09: 31: 37 GMT

content-Type:text/htm1

content-Length:162

onnection:pen-alive

"a.com.conf" 11L 202C writtion

无论从何处访问都拒绝


4.ngx_hrrp_core_module模块

实现基于用户的访问控制,使用basic机制进行用户认证

1)auth__basic string I off;

2)auth__basic_user_file file; :

location /admin/{

     auth_basic " Admin Area";

    auth_basic_user__file /etc/ nginx/.ngxpasswd;(局限:由于本身没有文件,nginx只能通过httdp工具进行创建用户账号文件)

}

[root@centos7 sitea]#rpm -ql httpdntoolsusr/bin/ab

usr/bin/htdbm

usr/bin/htdigest

usr/bin/htpasswd

usr/bin/httxt2dbm

usr/bin/logresolve

usr/share/doc/httpd-tools-2.4.6

usr/share/doc/httpd-tools-2.4.6/LICENSEusr/share/doc/httpd-tools-2.4.6/NOTICEusr/share/man man1/ab.1.gz

usr/shareman/man1/htdbm.1.gz

usr/share/man man1/htdi gest.1.gz

usr/shareman man1/ htpasswd.1.gz

/usr/share/man man1/httxt2dbm.1.gz

 

a.com.conf13L, 258C written     //进行账号的第一次创建

[root@centos7 vhosts]#nginx

^c[root@centos7 vhosts]#pwd

/etc/nginx/conf.d/vhosts

[root@centos7 vhosts]#ls

a.com.confb.com.confc.com.conf

[r oot@centos7 vhosts ] #htpasswd -cm ngi nxuser httpuser1New pas sword:

Re-type new password:

Adding password for user httpuser1

[root@centos7 vhosts ]#cat ngi nxuser

httpuser1: Sapr1$52zcUNaY $QoIoFKtj7HQ1/YBPSovjo .

[root@centos7 vhosts]#htpasswd -m nginxuser httpuser2

New password:

Re-type new password:

Adding password for user httpuser 2

[root@centos7 vhosts ]#cat nginxuser

httpuser1:Sapr1$52zcUNaY$QoIoFKtj7HQ1/YBPSovjo.

//对特定目录进行验证


5.用户口令文件

1BJXX4 : tItname:password:comment

2)加密文本:htpasswd命令实现

  httpd-tools所提供

 

 

二、ngx_hrrp_stub_status_module


1.ngx_hrrp_stub_status_module模块

用于输出nginx的基本状态信息

输出信息示例:

 Active connections: 291

 server accepts handled requests

1663094816630948 31070465

 上面三个数字分别对应accepts,handled,requests三个值Readinq: 6 Writinq: 1 79 Waitinq: 106

Active connections:当前状态,活动状态的连接数

accepts:统计总值,已经接受的客户端请求的总数

handled:统计总值,已经处理完成的客户端请求的总数

requests:统计总值,客户端发来的总的请求数

Reading:当前状态,正在读取客户端请求报文首部的连接的连接数Writing:当前状态,正在向客户端发送响应报文过程中的连接数Waiting:当前状态,正在等待客户端发出请求的空闲连接数

1 stub__status;

   示例:

    location /status {

   stub_status;   //状态页会暴露服务器信息,因此为启用安全考虑,应进行访问权限的设置

    allow 172.16.0.0/16;

    deny all;

 

三、ngx_http_log_modul


1.ngx_http_log_module模块

指定日志格式记录请求

1)log_format name string ...  //定义日志格式

示例

log_format combined'$remote_addr-$remote_user[$time_ lycal]'

                 '$request'$ status $body_bytes_sent

                 '$http_referer"" $http_user_agent'

access_log/spool/logs/nginx-access.logcompression buffer=32k;(生产环境添加buffer,而测试环境中不添加)

   string可以使用nginx核心模块及其它模块内嵌的变量


2)access_log path [format [buffer= size] [gzip[= level]] [flush= timeif= condition]];

   access_ log off;

   访问日志文件路径,格式及相关的缓冲的配置

    buffer=size

     flush=time


3)open_log_file_cache max=N[inactive=time][min__uses= N]

[valid =time];

open_log_file_cache off;

 缓存各日志文件相关的元数据信息

max :缓存的最大文件描述符数量

min_uses :inactive指定的时长内访问大于等于此值方可被当作活动项

inactive :非活动时长

valid:验证缓存中各缓存项是否为活动项的时间间隔

 

四、ngx_http_gzip_module

gzip方法压缩响应数据,节约带宽


1.gzip on|off;

启用或禁用gzip压缩


2. gzip_comp_level level;

压缩比由低到高:19

默认:1


3.gzip_disable regex

匹配到客户端浏览器不执行压缩


4.gzip_min_length length

启用压缩功能的响应报文大小阈值


5.gzip__http__version 1.0 | 1.1;

设定启用压缩功能时,协议的最小版本

默认: 1.1


6.gzip_buffers number size;

支持实现压缩功能时缓冲区数量及每个缓存区的大小

默认:324k168k


7.gzip_types mime-type ...

指明仅对哪些类型的资源执行压缩操作;即压缩过滤器

默认包含有text/html ,不用显示指定,否则出错


8.gzip_vary on | off;

如果启用压缩,是否在响应报文首部插入"Vary: Accept- Encoding"


9.gzip__proxied off|expired|no-cache|no-store| private|

no_last_modified| no_etag| auth| any ...

  nginx充当代理服务器时,对于后端服务器的响应报文,在何种条件下启用压缩功能.

   off :不启用压缩

   expired , no-cache, no-store , private :对后端服务器的响应报文首部Cache-Control值任何一个,启用压缩功能

示例 :

 gzip on;

 gzip__comp_level 6;

 gzip_min_length 64;   //达到64字节进行压缩

 gzip_proxied any;

 gzip_types text/xml text/Css application/javascript; //对何种类型的文本进行压缩。而默认包含的有text/html ,其不用显示指定,否则将会出错。

不压缩时,文件十分庞大,将会导致浪费资源。

可以在响应报文首部插入"Vary: Accept- Encoding",以表示此间已压缩。生产过程中,服务器通常都需要压缩。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
缓存 算法 Unix
nginx企业应用配置-3 | 学习笔记(一)
快速学习nginx企业应用配置-3。
1323 0
|
安全 Unix 应用服务中间件
nginx 企业应用配置-1 | 学习笔记(二)
快速学习nginx 企业应用配置-1。
191 0
|
缓存 安全 JavaScript
nginx 企业应用配置-3 | 学习笔记(二)
快速学习 nginx 企业应用配置-3。
111 0
|
存储 应用服务中间件 Linux
nginx 企业应用配置-2 | 学习笔记
快速学习 nginx 企业应用配置-2。
93 0
nginx 企业应用配置-2 | 学习笔记
|
存储 应用服务中间件 开发工具
nginx企业应用配置-2
一、ngx_hrrp_core_module 二、HTTP请求报文
nginx企业应用配置-2
|
缓存 安全 Unix
nginx企业应用配置
一、nginx配置 二、ngx_http_core_module
|
1月前
|
移动开发 前端开发 JavaScript
前端vue2、vue3去掉url路由“ # ”号——nginx配置(一)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
90 0
|
1月前
|
前端开发 JavaScript 应用服务中间件
前端vue2、vue3去掉url路由“ # ”号——nginx配置(二)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
86 0
|
1天前
|
应用服务中间件 nginx
Nginx 配置,自定义日志格式 log_format
Nginx 配置,自定义日志格式 log_format
10 0
|
5天前
|
Java Serverless 应用服务中间件
Serverless 应用引擎产品使用合集之Web函数启动的Spring Boot项目可以通过什么方式配置Nginx
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。