Nginx

Nginx是网络服务器,web server,接收并处理用户的请求。

 

~~~~~~~~~~~~~~~~~~~~~~~~~~

一、安装配置

【环境准备】

操作系统:CentOS Linux release 7.3.1611 (Core)
GCC编译环境:yum install -y gcc gcc-c++ autoconf automake make
模块依赖性:Nginx支持的功能模块需要有第三方的库支持,例如gzip的zlib库,rewrite重写需要的pcre库,HTTPS需要的openssl库等等。
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel wget httpd-tools vim

系统基础开发环境:yum groupinstall "Development Tools" "Basic Web Server"

确保防火墙关闭 iptables -F
关闭selinux
yum源配置正确
网络连接状态正常

【Nginx下载】

yum自动安装的话是不支持自由扩展第三方功能的
因此用源码编译安装
1.下载Nginx源代码 
[root@linux]# wget nginx.org/download/nginx-1.14.0.tar.gz
[root@linux ]#wget tengine.taobao.org/download/tengine-2.3.2.tar.gz
2.解压缩Nginx源代码
[root@linux Learn_Nginx]# tar -zxf tengine-2.3.2.tar.gz

3.复制Nginx默认提供的vim语法插件
[root@linux nginx-2.3.2]# mkdir ~/.vim
[root@linux nginx-2.3.2]# cp -r contrib/vim/* ~/.vim/

4.Nginx源代码目录介绍
auto 检测系统模块
CHANGES nginx更改记录文件
conf 存放nginx配置文件
configure 释放编译文件的定制脚本
contrib 提供了perl与vim插件
html 存放标准html页面语法
src 存放nginx源码

5.开始编译Nginx,扩展编译模块
#列出Nginx的编译选项,如制定安装路径,配置文件、日志文件等路径,指定开启模块功能等
[root@linux tenginx-2.3.2]# ./configure --help

#编译Nginx初步,
[root@linux tenginx-2.3.2]# ./configure --prefix=/home/Learn_Nginx/nginx/ --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio

6.执行make编译
make

7.首次编译安装,生成Nginx的可执行命令
make install

8.检查Prefix指定的安装目录
[root@linux nginx-1.14.0]# ls /home/Learn_Nginx/
nginx tengine-2.3.2  tengine-2.3.2.tar.gz

9.Nginx的程序目录
[root@linux nginx]# pwd
/home/Learn_Nginx/nginx
[root@linux nginx]# ls
conf html logs sbin

依次是配置文件,静态文件,日志,二进制命令目录

10.创建nginx的环境变量文件,修改如下,创建/etc/profile.d/nginx.sh脚本文件便于以后维护
[root@linux ~]# cat /etc/profile.d/nginx.sh
PATH=/home/Learn_Nginx/nginx/sbin:$PATH

11.退出会话,重新登录终端,此时可以正常使用nginx
[root@linux ~]# echo $PATH
/home/Learn_Nginx/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

12.检查nginx的编译模块信息
[root@linux ~]# nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/home/Learn_Nginx/nginx/ --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio

netstat -tunlp |grep 80

【nginx热部署】

nginx作为一个优秀的反向代理服务器,同时具备高可用的特性,Nginx也支持热部署。

热部署指的是在不重启或关闭进程情况下,新应用直接替换掉旧的应用

热部署大致流程

1.备份旧的二进制文件
2.编译安装新的二进制文件,覆盖旧的二进制文件
3.发送USR2信号给旧master进程
4.发送WINCH信号给旧master进程
5.发送QUIT信号给旧master进程
环境准备:
1.旧nginx版本 nginx version: nginx/2.3.2

2.新nginx版本 nginx version: nginx/2.3.3

nginx工作模式是master-worker(包工头-工人)

刚才所说的nginx支持reload重载仅仅是nginx的master进程,检查配置文件语法是否正确,错则返回错误、正确也不会改变已经建立连接的worker,只得等待worker处理完毕请求之后,杀死旧配置文件的worker,启动新配置文件的worker。

但是Nginx这里提提供了热部署功能,就是在不影响用户体验下,进行软件版本升级,也就是不主动杀死worker,替换软件的二进制文件。

1.目前运行的是旧的nginx版本,如下检查
[root@linux sbin]# ps -ef|grep nginx
root 20311 1 0 15:12 ? 00:00:00 nginx: master process nginx
nobody 20312 20311 0 15:12 ? 00:00:00 nginx: worker process
root 20314 13589 0 15:12 pts/0 00:00:00 grep --color=auto nginx

[root@linux sbin]# curl 127.0.0.1/123 #访问错误页面,返回nginx版本了

热部署具体操作

1.备份旧版本的nginx二进制文件
[root@linux sbin]# pwd
/home/Learn_Nginx/nginx/sbin
[root@chaogelinux sbin]# mv nginx nginx.old
[root@linux sbin]# ls
nginx.old

2.检查旧版本nginx的编译参数
[root@linux Learn_Nginx]# nginx.old -V
nginx version: nginx/2.3.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/home/Learn_Nginx/nginx/ --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio

3.编译安装新版本nginx
#下载新nginx源码
[root@linux Learn_Nginx]#wget http://tengine.taobao.org/download/tengine-2.3.3.tar.gz
#编译安装新版本nginx
[root@linux Learn_Nginx]# tar -zxf nginx-2.3.3.tar.gz
#开始编译
[root@linux Learn_Nginx]# cd nginx-2.3.3/
[root@linux nginx-2.3.3]#
[root@linux nginx-2.3.3]# ./configure --prefix=/home/Learn_Nginx/nginx/ --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio
#编译安装
[root@linux nginx-2.3.3]# make && make install

3.1.此时发现已存在2个版本nginx程序
[root@chaogelinux sbin]# ls
nginx nginx.old

3.2.[root@linux sbin]# nginx.old -V
[root@linux sbin]# nginx -V
说明此时系统安装有两个Nginx版本

4检查旧的nginx进程,请注意,这里用绝对路径启动nginx
请注意,这里用绝对路径启动nginx
请注意,这里用绝对路径启动nginx
例如
/home/Learn_Nginx/nginx/sbin/nginx

注意这里的PID和PPID(pid是当前进程的id号,ppid是启动该进程的pid,也就是父ID,可知该pid由谁启动)
[root@linux sbin]# ps -ef|grep nginx
root 20311 1 0 15:12 ? 00:00:00 nginx: master process nginx
nobody 20312 20311 0 15:12 ? 00:00:00 nginx: worker process
root 20314 13589 0 15:12 pts/0 00:00:00 grep --color=auto nginx

5.[root@linux sbin]#cat /home/Learn_Nginx/nginx/logs/nginx.pid

发现pid编码是旧进程的
6.发送USR2信号给旧版本主进程,使得nginx旧版本停止接收请求,切换为新nginx版本

[root@linux sbin]# kill -USR2 `cat /home/Learn_Nginx/nginx/logs/nginx.pid`

7.检查此时的nginx进程
nginx-master首先会重命名pid文件,在文件后面添加.oldbin后缀,然后会再启动一个新的master进程以及worker,且使用的是新版Nginx
nginx能够自动将新来的请求,过度到新版master进程下,实现平滑过度

#可以发现新的master进程由旧master启动,由PPID可看出
[root@linux sbin]# ps -ef|grep nginx
root 20311 1 0 15:12 ? 00:00:00 nginx: master process nginx
nobody 20312 20311 0 15:12 ? 00:00:00 nginx: worker process
root 20335 20311 0 15:13 ? 00:00:00 nginx: master process nginx
nobody 20336 20335 0 15:13 ? 00:00:00 nginx: worker process
root 20338 13589 0 15:13 pts/0 00:00:00 grep --color=auto nginx

[root@linux sbin]# ls ../logs/
access.log error.log nginx.pid nginx.pid.oldbin

8.发送WINCH信号给旧master进程,优雅的关闭旧worker进程
[root@chaogelinux sbin]# kill -WINCH `cat /home/Learn_Nginx/nginx/logs/nginx.pid.oldbin`

#再次检查进程情况,旧master的worker已经关闭了,旧master不会自己退出,用作版本回退
[root@linux sbin]# ps -ef|grep nginx
root 20311 1 0 15:12 ? 00:00:00 nginx: master process nginx
root 20335 20311 0 15:13 ? 00:00:00 nginx: master process nginx
nobody 20336 20335 0 15:13 ? 00:00:00 nginx: worker process
root 20607 20111 0 15:25 pts/0 00:00:00 grep --color=auto nginx

9.如果你觉得没问题了,可以关闭旧master进程
kill 20311
[root@chaogelinux sbin]# ps -ef|grep nginx
root 20335 1 0 15:13 ? 00:00:00 nginx: master process nginx
nobody 20336 20335 0 15:13 ? 00:00:00 nginx: worker process
root 20665 13589 0 15:28 pts/0 00:00:00 grep --color=auto nginx

热部署的坑

如果出现发送 kill -USR2信号后,未出现新的master进程

是因为:

旧的nginx必须用绝对路径启动,然后再发送kill -USR2信号

 

linux nginx.pid 丢失 解决方案

pkill -9 nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nginx -s reload

二、Nginx配置文件语法

nginx.conf由指令与指令块构成
每行语句由分号结束,指令和参数之间由空格分隔
指令块可以大括号{}组织多条指令块
配置文件中#号添加注释信息
支持$变量使用变量
支持include语句,组合多个配置文件
部分指令支持正则表达式

nginx.conf重要的指令块
核心功能都在于http{}指令块里,http{}块还包含了以下指令

server{} 指令块 ,对应一个站点配置,反向代理,静态资源站点
location{} ,对应一个url
upstream{} ,定义上游服务,负载均衡池

配置文件热重读

配置文件重读
在nginx正在运行时,如实修改了nginx.conf或是新增了一些功能配置,让其生效,可能需要重启整个nginx进程、

但是你不能保证某个时间段没有用户在访问nginx,重启会断开用户的连接,造成莫名的故障

因此nginx提供了reload重载功能,不停止服务,更新配置文件

1.修改nginx.conf
worker_processes 3;   #修改nginx工作进程数为3个

2.重载配置文件
nginx -s reload

3.检查linux进程
[root@linux nginx114]# vim conf/nginx.conf
[root@linux nginx114]#
[root@linux nginx114]# ps -ef|grep nginx
root 6191 1 0 10:33 ? 00:00:00 nginx: master process nginx
nobody 6213 6191 0 10:33 ? 00:00:00 nginx: worker process
nobody 6214 6191 0 10:33 ? 00:00:00 nginx: worker process
nobody 6215 6191 0 10:33 ? 00:00:00 nginx: worker process
root 6345 5283 0 10:38 pts/0 00:00:00 grep --color=auto nginx

 

信号集nginx -s 对应参数

nginx -s 对应参数 信号 含义 English
stop TERM 强制关闭整个服务 Shut down quickly.
null INT 强制关闭整个服务 Shut down quickly.
quit QUIT 优雅地关闭整个服务 Shut down gracefully.
reopen USR1 重新打开日志记录 Reopen log files.
reload HUP 重新读取配置文件,并且优雅地退出老的worker Reload configuration, start the new worker process with a new configuration, and gracefully shut down old worker processes.
null USR2 平滑升级到新版本 Upgrade the nginx executable on the fly.
null WINCH 优雅地关闭worker(在热更新的时候必用) Shut down worker processes gracefully.

Nginx命令行

1、启停指令

nginx -s stop
nginx -s reload
nginx 首次输入表示启动

2、nginx帮助指令

[root@linux nginx114]# nginx -h
nginx version: nginx/1.14.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h : this help #帮助信息
-v : show version and exit #显示版本
-V : show version and configure options then exit #显示编译信息与版本
-t : test configuration and exit #测试配置文件语法
-T : test configuration, dump it and exit #测试语法且输出内容
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload #发送信号,stop立即停止,quit优雅停止,reload重读配置文件,reopen重新记录日志
-p prefix : set prefix path (default: /home/Learn_Nginx/nginx114//)
-c filename : set configuration file (default: conf/nginx.conf) #使用指定配置文件
-g directives : set global directives out of configuration file #覆盖默认参数

三、日志切割

 

1.查看当前nginx日志

[root@linux logs]# ll
总用量 12
-rw-r--r-- 1 root root 1645 2月 11 15:30 access.log
-rw-r--r-- 1 root root 2370 2月 11 15:30 error.log
-rw-r--r-- 1 root root 6 2月 11 15:13 nginx.pid

#看下日志内容
[root@linux logs]# tail -2 access.log
192.168.178.12 - - [11/Feb/2020:15:32:22 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
192.168.178.12 - - [11/Feb/2020:15:32:23 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"

2.给文件重命名,注意用mv而不是cp(涉及到文件inode知识)

[root@linux logs]# mv access.log access.log$(date +"%Y-%m-%d--%H:%M:%S")
[root@linux logs]# ll
总用量 16
-rw-r--r-- 1 root root 4630 2月 11 15:32 access.log2020-02-11--15:43:49
-rw-r--r-- 1 root root 2370 2月 11 15:30 error.log
-rw-r--r-- 1 root root 6 2月 11 15:13 nginx.pid

3.发送USR1信号给nginx-master,重新打开日志记录,生成新的日志文件

nginx -s reopen

#等同于 Kill -USR1 nginx.pid

注意,在以上的nginx重命名日志切割,不要着急立即对文件修改,且要sleep 等待1秒
由于nginx的工作模式,master下发指令给worker只是做了标记,当业务量大的时候,这个修改操作可能会慢一点,不会理解生效

4.在生产环境

在生产环境下,主要以crontab形式,执行cut_nginx_log.sh脚本

[root@bogon sbin]# pwd
/home/Learn_Nginx/nginx/sbin
[root@bogon sbin]# cat cut_nginx_log.sh
#!/bin/bash
# 脚本写入crontab,每天0点执行,这是一个nginx日志切割脚本

#nginx日志存放点
logs_path="/home/Learn_Nginx/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y-%m-%d").log

kill -USR1 `cat /opt/tngx232/logs/nginx.pid`

6.写入crontab
crontab -l
0 0 * * * /bin/bash /home/Learn_Nginx/nginx/sbin/cut_nginx_log.sh

原文链接: https://www.cnblogs.com/-mistyrain/p/15757727.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Nginx

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/183093

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 上午10:19
下一篇 2023年2月12日 上午10:20

相关推荐