nginx服务(六)_常用模块的配置使用

访问控制模块
日志模块
压缩模块
ssl模块
升级opensll版本

 


访问控制模块

ngx_http_access_module模块:实现基于ip的四层访问控制功能
1、allow address | CIDR | unix: | all;
2、deny address | CIDR | unix: | all;
应用于:`http, server, location, limit_except``

例:配置基于ip的访问控制

location /about {
alias /data/nginx/html/pc;
index index.html;
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all; #先允许⼩部分,再拒绝⼤部分
}

ngx_http_auth_basic_module模块:实现基于用户的访问控制,使用basic机制进行用户认证;在编译安装nginx的时候需要添加编译参数–withhttp_stub_status_module,否则配置完成之后监测会是提⽰语法错误。
1、auth_basic "string | off";
2、auth_basic_user_file "file";
ngx_http_stub_status_module模块:用于输出nginx的基本状态信息;
应用于:server, location

例: Nginx账户认证功能

[root@s2 ~]# yum install httpd-tools -y
[root@s2 ~]# htpasswd -cbm /apps/nginx/conf/.htpasswd user1 123456
Adding password for user user1
[root@s2 ~]# htpasswd -bm /apps/nginx/conf/.htpasswd user2 123456
Adding password for user user2
[root@s2 ~]# tail /apps/nginx/conf/.htpasswd
user1:$apr1$Rjm0u2Kr$VHvkAIc5OYg.3ZoaGwaGq/
user2:$apr1$nIqnxoJB$LR9W1DTJT.viDJhXa6wHv.

[root@s2 ~]# vim /apps/nginx/conf/conf.d/pc.conf
location = /login/ {
root /data/nginx/html/pc;
index index.html;
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
}
重启Nginx并访问测试

例: nignx状态页

location /nginx_status {
stub_status;
allow 192.168.0.0/16;
allow 127.0.0.1;
deny all;
}

状态⻚⽤于输出nginx的基本状态信息:
输出信息⽰例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上⾯三个数字分别对应accepts,handled,requests三个值
Reading:6 Writing: 179 Waiting: 106

Active connections: 当前处于活动状态的客⼾端连接数,包括连接等待空闲连接数。
accepts:统计总值,Nginx⾃启动后已经接受的客⼾端请求的总数。
handled:统计总值,Nginx⾃启动后已经处理完成的客⼾端请求的总数,通常等于accepts,除⾮有因
worker_connections限制等被拒绝的连接。
requests:统计总值,Nginx⾃启动后客⼾端发来的总的请求数。
Reading:当前状态,正在读取客⼾端请求报⽂⾸部的连接的连接数。
Writing:当前状态,正在向客⼾端发送响应报⽂过程中的连接数。
Waiting:当前状态,正在等待客⼾端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于 active –
(reading+writing),


日志模块

ngx_http_log_module模块:日志模块配置日志指定的格式。
1、log_format name string ...;string可以使用nginx核心模块及其它模块内嵌的变量;
2、access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=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:验正缓存中各缓存项是否为活动项的时间间隔;
介绍
$remote_addr:远程IP
$remote_user:远程用户
$time_local:收到用户请求的服务器本地时间
$request:请求的URL
$status:状态吗
$body_bytes_sent:发送的响应报文的字节数
$http_referer:从哪里跳转到当前页面的
"$http_user_agent":client浏览器类型
"$http_x_forwarded_for":如果是一个服务器那么就向后端服务器传递client的真实IP
"$gzip_ratio"':压缩比

例: ⾃定义访问⽇志

log_format nginx_format1 '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$server_name:$server_port';
access_log logs/access.log nginx_format1;

#重启nginx并访问测试⽇志格式

==> /apps/nginx/logs/access.log <==
192.168.0.1 - - [22/Feb/2019:08:44:14 +0800] "GET /favicon.ico HTTP/1.1" 404 162 "-"
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/2
0100101 Firefox/65.0" "-"www.magedu.net:80

 

例: ⾃定义json格式⽇志
Nginx 的默认访问⽇志记录内容相对⽐较单⼀,默认的格式也不⽅便后期做⽇志统计分析,⽣产环境中通常将nginx⽇志转换为json⽇志,然后配合使⽤ELK做⽇志收集-统计-分析。

log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
access_log /apps/nginx/logs/access_json.log access_json;

#重启Nginx并访问测试⽇志格式

{"@timestamp":"2019-02-
22T08:55:32+08:00","host":"192.168.7.102","clientip":"192.168.0.1","size":162,"response
time":0.000,"upstreamtime":"-","upstreamhost":"-
","http_host":"www.magedu.net","uri":"/favicon.ico","domain":"www.magedu.net","xff":"-
","referer":"-","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; Win64;
x64; rv:65.0) Gecko/20100101 Firefox/65.0","status":"404"}

json格式的日志访问统计

#!/usr/bin/env python2
#coding:utf-8
#Author:Zhang ShiJie
status_200= []
status_404= []
with open("access_json.log") as f:
	for line in f.readlines():
		line = eval(line)
		if line.get("status") == "200":
			status_200.append(line.get)
		elif line.get("status") == "404":
			status_404.append(line.get)
		else:
			print("状态码 ERROR")
f.close()

print "状态码200的有--:",len(status_200)
print "状态码404的有--:",len(status_404)

保存⽇志⽂件到指定路径并进测试:
root@s2 ~]# python nginx_json.py
状态码200的有--: 1910
状态码404的有--: 13

注: python3 print需要加[] 否则会报错,如print [test]


压缩模块

ngx_http_gzip_module:压缩模块,gpg、jgp。。、mp3等本身就有压缩,所以我们通常都对一些文本类的信息做压缩,压缩比建议不要太高,浪费CPU的时间片,6压缩比就可以了,如果对我们来说带宽是个昂贵的资源,CPU对我们来讲无所谓,那么相当建议压缩,带宽一年下来能节约不少
应用于:http, server, location, if in location

1、gzip on | off;是否启用gzip

2、gzip_comp_level level;指定压缩比,压缩⽐由低到⾼从1到9,默认为1

3、gzip_disable regex ...;对那种类型的浏览器不进行压缩

4、gzip_min_length length;启用压缩功能的响应报文最小size(最少多大才进行压缩);

5、gzip_buffers number size;支持实现压缩功能时为其配置的缓冲区数量及每个缓存区的大小;默认32 4k|16 8k;

6、gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any|any;nginx作为代理服务器接收到从被代理服务器发送的响应报文后,在何种条件下启用压缩功能的;
off:对代理的请求不启用
expired :如果响应报文首部带有Expires这个首部,我们就启用压缩功能
no-cache, no-store,private:表示从被代理服务器收到的响应报文首部的Cache-Control的值为此三者中任何一个,则启用压缩功能;
no_last_modified:表示包含Last-Modified就启用压缩功能
no_etag:表示包含ETag就启用压缩功能Authorization
auth :表示包含Authorization就启用压缩功能
any:任何类型都进行压缩

7、gzip_types mime-type ...;压缩过滤器,仅对此处设定的MIME类型的内容启用压缩功能;

8、gzip_vary:我们是否在响应报文中插入Vary: Accept-Encoding这个首部信息,表示是否发生变化

 
例:

		gzip  on;
		gzip_comp_level 6;
		gzip_min_length 64;
		gzip_proxied any;
		gzip_types text/xml text/css  application/javascript;
 		gzip_static off;
        gzip  on;
        gzip_disable "MSIE [1-6]\.";
        gzip_min_length 1024;
        gzip_comp_level 5;
        gzip_buffers 4 16k;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

ssl模块

ngx_http_ssl_module模块:
1、ssl on | off;是否启用ssl功能
2、ssl_certificate file;当前虚拟主机使用PEM格式的证书文件;
3、ssl_certificate_key file;当前虚拟主机上与其证书匹配的私钥文件;
4、ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];支持ssl协议版本,默认为后三个;
5、ssl_session_cache off | none | [builtin[:size]] [shared:name:size];会话缓存,缓存协商秘钥算法等信息
builtin[:size]:使用OpenSSL内建的缓存,此缓存为每worker进程私有;
[shared:name:size]:在各worker之间使用一个共享的缓存;
6、ssl_session_timeout time;客户端一侧的连接可以复用ssl session cache中缓存 的ssl参数的有效时长;

自签证书过程
创建证书–生成私钥->生成证书签署请求—>获得证书->修改配置文件

例: 实现多域名HTTPS

[root@s2 certs]# cat /apps/nginx/conf/conf.d/mobile.conf
server {
listen 80;
server_name mobile.magedu.net;
location / {
root html;
index index.html index.htm;
}
location /linux {
root /data/nginx/mobile/html;
index index.html index.htm;
}
location /python {
root /data/nginx/mobile/html;
index index.html index.htm;
}
}
server {
listen 443 ssl;
server_name mobile.magedu.net;
ssl_certificate /apps/nginx/certs/mobile.magedu.net.crt;
ssl_certificate_key /apps/nginx/certs/mobile.magedu.net.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
location / {
root html;
index index.html index.htm;
}
location /linux {
root /data/nginx/mobile/html;
index index.html index.htm;
}
location /python {
root /data/nginx/mobile/html;
index index.html index.htm;
}
}

升级OpenSSL版本:

⼼脏出⾎(英语:Heartbleed),也简称为⼼⾎漏洞,是⼀个出现在加密程序库OpenSSL的安全漏洞,该程序库
⼴泛⽤于实现互联⽹的传输层安全(TLS)协议。它于2012年被引⼊了软件中,2014年4⽉⾸次向公众披露。只要
使⽤的是存在缺陷的OpenSSL实例,⽆论是服务器还是客⼾端,都可能因此⽽受到攻击。此问题的原因是在实现
TLS的⼼跳扩展时没有对输⼊进⾏适当验证(缺少边界检查),因此漏洞的名称来源于“⼼跳”(heartbeat)。该程
序错误属于缓冲区过读,即可以读取的数据⽐应该允许读取的还多。

准备OpenSSL源码包:
wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz
# pwd
/usr/local/src
# tar xvf openssl-1.1.1d

编译安装Nginx并制定新版本OpenSSL路径:
# cd /usr/local/src/nginx-1.16.1/
#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --
with-http_v2_module --with-http_realip_module --with-http_stub_status_module --withhttp_
gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --withstream_
realip_module --with-select_module --with-file-aio --addmodule=/
usr/local/src/echo-nginx-module --with-openssl=/usr/local/src/openssl-1.1.1d

# make && make install

验证并启动Nginx:
# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
# /apps/nginx/sbin/nginx

root@ubuntu:/usr/local/src/nginx-1.16.1# nginx -V
nginx version: nginx/1.16.1
built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 
built with OpenSSL 1.1.1d  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/nginx-1.16.1/src/echo-nginx-module --with-file-aio --add-module=/usr/local/src/nginx-1.16.1/src/openssl-1.1.1d
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一些常用Nginx 模块及其安装方法: 1. ngx_http_ssl_module:提供 HTTPS 支持。 安装命令:`sudo apt-get install nginx-extras` 2. ngx_http_gzip_module:开启 gzip 压缩功能,可以加快网页加载速度。 安装命令:`sudo apt-get install nginx-extras` 3. ngx_http_geoip_module:根据客户端 IP 地址,提供地理位置信息。 安装命令:`sudo apt-get install nginx-extras` 4. ngx_http_realip_module:让 Nginx 获取真实的客户端 IP 地址。 安装命令:`sudo apt-get install nginx-extras` 5. ngx_http_stub_status_module:提供 Nginx 自身状态的监控信息。 安装命令:无需安装,Nginx 默认已经安装。 6. ngx_http_limit_conn_module:限制客户端连接数。 安装命令:`sudo apt-get install nginx-extras` 7. ngx_http_limit_req_module:限制客户端请求速率。 安装命令:`sudo apt-get install nginx-extras` 以上模块都可以通过编译 Nginx 来安装。在编译 Nginx 时,可以添加相应的选项来开启或禁用这些模块。 ### 回答2: 常用nginx模块安装有以下几种方法: 1. 源码编译安装:首先从官方网站或GitHub上下载nginx源代码包,解压后进入目录,然后执行配置命令 "./configure",根据需要添加所需模块的参数,最后执行 "make" 和 "make install" 命令进行编译和安装。 2. 使用apt-get或yum等包管理工具:使用适合的命令进行nginx的安装,如"apt-get install nginx"或"yum install nginx"。这样安装的nginx一般是编译时默认带有一些常用模块的。 3. 通过第三方工具安装:有一些第三方工具可以帮助自动化编译和安装nginx及其模块,例如通过使用openresty工具集、ngx_openresty或nginx-build等来实现。 4. 动态模块nginx 1.9.11及以上版本支持动态加载模块,可以通过在配置文件中使用 "load_module" 指令加载模块。首先编译动态模块,然后在配置文件中添加 "load_module" 指令指定模块文件路径即可。 在安装nginx模块时,需要注意源代码包版本与nginx版本兼容性,以及模块之间的依赖关系。根据实际需求,选择合适的安装方法进行配置和安装常用nginx模块,如gzip模块、proxy模块、rewrite模块、ssl模块等。正确安装和配置模块可以提高nginx的性能,并扩展其功能,更好地满足网站或应用的需求。 ### 回答3: 常用nginx模块安装一般通过以下几个步骤完成: 1. 查看已安装的nginx版本: 使用`nginx -v`或`nginx -V`命令查看已安装的nginx版本。该命令会显示nginx的版本信息,包括编译参数。 2. 下载所需的模块源码: 到nginx官方网站(http://nginx.org/)或其他第三方网站上下载所需的模块源码。通常模块源码会以`.tar.gz`或`.zip`等压缩格式提供。 3. 解压模块源码: 使用相应的解压工具(例如tar或unzip)解压下载的模块源码。 4. 进入nginx源码目录: 使用命令行进入已安装的nginx源码目录。该目录通常位于`/usr/local/nginx`或`/etc/nginx`。 5. 配置编译参数: 使用文本编辑器打开nginx配置文件`nginx.conf`,在文件中找到`./configure`命令,并在其后面添加需要安装的模块的编译参数。常用模块包括HttpProxyModule、HttpSslModule、HttpRewriteModule等。例如,要启用HttpProxyModule,可以在`./configure`后添加`--with-http_proxy_module`参数。 6. 编译和安装: 使用命令`make`编译修改后的配置文件。编译完成后,使用命令`make install`安装编译后的nginx。 7. 验证模块安装: 使用`nginx -t`命令验证nginx配置文件是否正确。如果配置文件无误,使用`nginx -s reload`命令重新加载nginx配置。 通过以上步骤,常用nginx模块就可以顺利安装并生效了。如果遇到编译错误或其他问题,可以参考相应的错误提示或查询相关的文档和社区讨论。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值