nginx热部署及常用模块介绍

热部署(方案一)

1、查看源编译参数

  • 升级一般是添加新的模块,或者升级版本,所以要参考以前编译的模块,如果不添加,那么以前的模块就不 能使用了
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --prefix=/usr/local/nginx/ --user=nginx --group=nginx

2、预编译&&编译&& 安装

[root@localhost ~]# cd /usr/src/nginx-1.16.1/
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx/
[root@localhost nginx-1.16.1]# make && make install

3、升级

[root@localhost nginx-1.16.1]# make upgrade

热部署(方案二)

1、查看原编译参数

  • 升级一般是添加新的模块,或者升级版本,所以要参考以前编译的模块,如果不添加,那么以前的模块就不 能使用了
[root@localhost nginx-1.16.1]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --prefix=/usr/local/nginx/

2、预编译&&编译&&安装

[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost nginx-1.16.1]# make && make install

3、生成新的master进程

[root@localhost sbin]# ps -ef | grep nginx  --查看nginx的进程状态
root       4326      1  0 17:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      4327   4326  0 17:30 ?        00:00:00 nginx: worker process
root       4332   1656  0 17:31 pts/0    00:00:00 grep --color=auto nginx
[root@localhost sbin]# kill -USR2 `cat  /usr/local/nginx/logs/nginx.pid`
[root@localhost sbin]# ps -ef | grep nginx
root       4326      1  0 17:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      4327   4326  0 17:30 ?        00:00:00 nginx: worker process
root       4334   4326  0 17:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      4335   4334  0 17:32 ?        00:00:00 nginx: worker process
root       4337   1656  0 17:32 pts/0    00:00:00 grep --color=auto nginx

注意:这个操作启动nginx命令必须是绝对路径启动

4、优雅的退出老的进程

[root@localhost sbin]# kill -WINCH  4326      --向老的master进程发信号
[root@localhost sbin]# ps -ef | grep nginx
root       4326      1  0 17:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root       4334   4326  0 17:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      4335   4334  0 17:32 ?        00:00:00 nginx: worker process
root       4341   1656  0 17:41 pts/0    00:00:00 grep --color=auto nginx

5、抉择:

  • 在nginx升级过程中假如发现nginx服务有异常可以通过回滚还原到以前的样子

5.1回滚:

5.1.1、重新拉回老的master进程
[root@localhost sbin]# kill -HUP 4326
[root@localhost sbin]# ps -ef |grep nginx
root       4326      1  0 17:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root       4334   4326  0 17:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      4335   4334  0 17:32 ?        00:00:00 nginx: worker process
nginx      4348   4326  0 17:49 ?        00:00:00 nginx: worker process
root       4350   1656  0 17:49 pts/0    00:00:00 grep --color=auto nginx
5.1.2、退出新的master进程
[root@localhost sbin]# kill -QUIT 4334
[root@localhost sbin]# ps -ef |grep nginx
root       4326      1  0 17:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      4348   4326  0 17:49 ?        00:00:00 nginx: worker process
root       4352   1656  0 17:52 pts/0    00:00:00 grep --color=auto nginx

5.2、新生

  • 经过一段时间的测试,发现服务器一切正常,退出老的master
[root@localhost sbin]# kill -QUIT 4326
[root@localhost sbin]# ps -ef |grep nginx
root       4354      1  0 17:55 ?        00:00:00 nginx

> :

master process /usr/local/nginx/sbin/nginx
nginx      4355   4354  0 17:55 ?        00:00:00 nginx: worker process
root       4361   1656  0 17:57 pts/0    00:00:00 grep --color=auto nginx

nginx常用模块

1、 access模块:

  • 访问控制模块 ,该模块可以实现简单的防火墙功能,过滤特定的主机。这个模块在我们编译nginx时会 默认编译进nginx的二进制文件中。

1.1、语法

  • Syntax: allow address | CIDR | unix: | all;
  • Default: —
  • Context: http, server, location, limit_except
  • Syntax: deny address | CIDR | unix: | all;
  • Default: —
  • Context: http, server, location, limit_excep
  • allow: 允许访问的IP或者网段。
  • deny: 拒绝访问的ip或者网段。
  • 从语法上看,它允许配置在http指令块中,server指令块中还有locatio指令块中,这三者的作用域有所 不同。
  • 如果配置在http指令段中,将对所有server(虚拟主机)生效;
  • 配置在server指令段中,对当前虚拟主机生效;
  • 配置在location指令块中,对匹配到的目录生效。
  • ps: 如果server指令块,location指令块没有配置限制指令,那么将会继承http的限制指令,但是一旦配 置了会覆盖http的限制指令。
  • 或者说作用域小的配置会覆盖作用域大的配置

1.2、http指令块配置:

1.2.1、对单个IP进行限制:
  • 在http指令块下配置单个IP限制
http {
    include       mime.types;
    .....
    deny 192.168.2.20;      ---限制192.168.2.20这个IP不能访问
    include    /usr/local/nginx/conf/vhost/*.conf;
    ......
}
  • 重新加载配置文件
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s reload
  • 在192.168.2.20机器上访问来验证
[root@localhost ~]# curl  -I 192.168.2.10
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1

可以看到只对192.168.2.20这个ip生效了,如果有配置虚拟主机,那么这个ip将都不能访问。

1.2.2、对整个网段做限制
  • 在http指令块做整个网段的限制
http {
    include       mime.types;
    .....
    deny 192.168.2.0/24;      ---限制192.168.2.0这个网段不能访问
    include    /usr/local/nginx/conf/vhost/*.conf;
    ......
}
  • 重新加载配置文件
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s reload
  • 在自己的服务器上验证是否能够访问
[root@localhost nginx]# curl -I 192.168.2.10
HTTP/1.1 403 Forbidden
Server: nginx/1.16.1

可以看到本机都不能访问,可以证明对整个网段限制起效了。

  • server指令块配置和location指令块配置和上面的基本一样。

2、auth_basic模块:

  • 用户认证模块,对访问目录进行加密,需要用户权限认证:
  • 这个功能特性是由ngx_http_auth_basic_module提供的,默认编译nginx时会编译好,主要有以下两个 指令。

2.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
  • 认证的配置可以在http指令块,server指令块,location指令块配置。
  • auth_basic string :定义认证的字符串,会通过响应报文返给客户端,也可以通过这个指令关闭认 证。
  • auth_basic_user_file file :定义认证文件。

2.2、对指定目录加密

对匹配目录加密: location /img { 
        auth_basic "User Auth";         
        auth_basic_user_file /usr/local/nginx/conf/auth.passwd;        
        }
	
2.2.1、生成认证文件
yum  -y  install httpd-tools
htpasswd  -c /usr/local/nginx/conf/auth.passwd admin 
输入两次密码确定。

3 2.2.2 重启服务后,访问验证:

  • 现在当我们访问img目录时,需要输入用户名及密码。
  • 它不仅可以设置访问指定目录时认证,还可以直接在访问首页时认证。
  • 如果需要关闭可以使用auth_basic off;或者直接将这两段注释掉。
2.2.3 htpasswd命令工具
  • htpasswd(选项)(参数)
  • 选项
  • -c:创建一个加密文件;
  • -m:默认采用MD5算法对密码进行加密;
  • -d:采用CRYPT算法对密码进行加密;
  • -p:不对密码进行进行加密,即明文密码;
  • -s:采用SHA算法对密码进行加密;
  • -b:在命令行中一并输入用户名和密码而不是根据提示输入密码;
  • -D:删除指定的用户。
  • 添加用户名及密码:
 htpasswd -b /usr/local/nginx/conf/auth.passwd  ys 123
  • 更新密码:
htpasswd  -D  /usr/local/nginx/conf/auth.passwd  ys 
htpasswd -b  /usr/local/nginx/conf/auth.passwd  ys 123456

2.3 对首页加密:

对匹配目录加密:
 location   /  {         
 auth_basic "User Auth";         
 auth_basic_user_file /usr/local/nginx/conf/auth.passwd;        
 }

3、 stub_status 模块:

  • 状态查看模块 ,该模块可以 输出nginx的基本状态信息 。

3.1、语法

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

3.2、配置

	location  = /status {
		stub_status;
		allow	192.168.2.10;
		deny	all;
}

注意:假如没有安装–with-http_stub_status_module这个模块需要安装模块才可以

3.3、本机验证

[root@v1 nginx]# curl 192.168.2.10/status
Active connections: 1 
server accepts handled requests
 2 2 2 
Reading: 0 Writing: 1 Waiting: 0 
  • Active connections:当前状态,活动状态的连接数
  • accepts:统计总值,已经接受的客户端请求的总数
  • handled:统计总值,已经处理完成的客户端请求的总数
  • requests:统计总值,客户端发来的总的请求数
  • Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
  • Writing:当前状态,正在向客户端发送响应报文过程中的连接数
  • Waiting:当前状态,正在等待客户端发出请求的空闲连接数

4、log模块:

  1. 日志模块,定义客户端请求资源格式

4.1、语法

  • Syntax: log_format name [escape=default|json|none] string …;
  • Default:
  • log_format combined “…”;
  • Context: http
  • Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
  • access_log off;
  • Default: access_log logs/access.log combined;
  • Context: http, server, location, if in location, limit_except
  • log_format只能在http指令块中定义,access_log可以在多个地方定义。

4.2、设置日志压缩

  • 设置日志压缩,需要zlib支持,默认yum安装的,不支持,源码编译安装默认是支持得。
[root@v1 nginx]# vim conf/nginx.conf
server    {
        .....
        access_log  logs/v1.jfedu.log.gz main  gzip;
        location  / {
           .....
}
       
}

[root@v1 nginx]#  /usr/local/nginx/sbin/nginx  -s reload
[root@v1 nginx]#  /usr/local/nginx/sbin/nginx -s reopen
[root@v1 nginx]# zcat logs/v1.jfedu.log.gz 
192.168.2.10 - - [20/Apr/2020:22:14:47 +0800] "GET / HTTP/1.1" 403 153 "-" "curl/7.29.0" "-"
192.168.2.10 - - [20/Apr/2020:22:15:44 +0800] "GET / HTTP/1.1" 200 10 "-" "curl/7.29.0" "-"

4.3 设置json格式:

4.3.1、在nginx配置文件修改如下:
http {
    include       mime.types;
    default_type  application/octet-stream;
    这里默认的main格式给注释掉
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    添加json格式
    log_format json '{"@timestamp":"$time_iso8601",'               
                    '"status":"$status",'               
                    '"client_ip":"$remote_addr",'               
                    '"method":"$request_method",'               
                    '"size":"$body_bytes_sent",'               
                    '"http_host":"$host",'               
                    '"request_uri":"$request_uri",'               
                    '"xri":"$http_x_real_ip",'               
                    '"referer":"$http_referer",'               
                    '"agent":"$http_user_agent"}';
     ......
  server  {
  ....
  把之前的main格式修改为json
  access_log  logs/v1.jfedu.log json;
  ....
  }
  }
4.3.2、查看json格式日志
[root@v1 nginx]# cat logs/v1.jfedu.log
{"@timestamp":"2020-04-21T11:11:52+08:00","status":"200",       "client_ip":"192.168.2.10",       "method":"GET",       "size":"10",       "http_host":"v1.jfedu.net",       "request_uri":"/",       "xri":"-",       "referer":"-",       "agent":"curl/7.29.0"}

5、referer 模块

  • 该模块可以进行防盗链设置。
  • 盗链的含义是网站内容本身不在自己公司的服务器上,而通过技术手段,直接调用其他公司的服务器网 站数据,而向最终用户提供此内容。

5.1、语法:

  • Syntax: valid_referers none | blocked | server_names | string …;
  • Default: —
  • Context: server, location

5.2、配置防盗链:

5.2.1、找张图片放在192.168.2.10服务器/usr/local/nginx/html/下
  • 安装locate查找工具
[root@v1 nginx]# yum -y install mlocate.x86_64 
[root@v1 nginx]# updatedb

注意:假如你有locate这个命令,不用安装

  • 查找图片并复制到/usr/local/nginx/html下
 [root@v1 nginx]# locate *.png
[root@v1 nginx]# cp /usr/share/pixmaps/fedora-logo-sprite.png  /usr/local/nginx/html/sxy.png
  • 验证是否可以看到图片
    在这里插入图片描述
5.2.2、在192.168.2.20服务器上盗取上面的图片并验证
[root@localhost ~]# vim /usr/share/nginx/html/index.html
<html> 
 <h1>welcome to nginx</h1> 
<img src="http://192.168.2.10/sxy.png"> 
 </html>
[root@localhost ~]# systemctl restart nginx

注意:我这台服务器nginx是yum安装的

  • 验证
    在这里插入图片描述
5.2.3、在192.168.2.10服务器上配置防盗链

[root@v1 ~]# vim /usr/local/nginx/conf/nginx.conf

location  ~* \.(gif|jpg|png|swf|flv)$	{
			valid_referers	none	blocked	 192.168.2.10  *.sxy.com;
			root 	/usr/local/nginx/html;
				if ($invalid_referer)	{
					return	403;
		}
		}
  valid_referers 表示合法的referers设置 
  none: 表示没有referers,直接通过浏览器或者其他工具访问。
  blocked: 表示有referers,但是被代理服务器或者防火墙隐藏;
  192.168.2.10: 表示通过192.168.2.10访问的referers; 
  *.sxy.com:  表示通过*.sxy.comt访问的referers,*表示任意host主机。
5.2.4、防盗链测试
  • 在192.168.2.20服务器上再次访问看可不可以获取得到图片
    在这里插入图片描述

5.3、配置返回200的防盗链

  • 修改配置文件
[root@v1 ~]# cd /usr/local/nginx/
[root@v1 nginx]# vim conf/nginx.conf
location  ~* \.(gif|jpg|png|swf|flv)$	{
			valid_referers	none	blocked	 192.168.2.10  *.sxy.com;
			root 	/usr/local/nginx/html;
				if ($invalid_referer)	{
					return	200;  这里配置返回200状态码。
		}
		}
  • 重新加载配置文件
[root@v1 nginx]# /usr/local/nginx/sbin/nginx -s reload
  • 验证防盗链
    在这里插入图片描述

5.4、配置返回网页的防盗链

  • 修改配置文件
[root@v1 ~]# cd /usr/local/nginx/
[root@v1 nginx]# vim conf/nginx.conf
location  ~* \.(gif|jpg|png|swf|flv)$	{
			valid_referers	none	blocked	 192.168.2.10  *.sxy.com;
			root 	/usr/local/nginx/html;
				if ($invalid_referer)	{
					return	http://192.168.2.10/sxy.html;  这里配置返回给对方一个网页
		}
		}

  • 创建网页文件
[root@v1 nginx]# vim html/sxy.html
兄弟,做人地道点,我流量不多,请收下留情。
  • 重新加载配置文件
[root@v1 nginx]# /usr/local/nginx/sbin/nginx -s reload
  • 验证防盗链
    在这里插入图片描述在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值