nginx平滑升级与配置文件详解
nginx平滑升级与配置文件详解
文章目录
- nginx平滑升级
- nginx配置文件
- nginx.conf配置详解
- 用于调试、定位问题的配置参数
- 正常运行必备的配置参数
- 优化性能的配置参数
- 网络连接相关的配置参数
- nginx作为web服务器时使用的配置:http{}段的配置参数
- http{}段配置指令:
- server {}:定义一个虚拟主机,示例如下:
- listen:指定监听的地址和端口
- log_format 定义日志格式
- location区段,通过指定模式来与客户端请求的URI相匹配
nginx平滑升级
所谓平滑升级,就是一种在热升级手段,在不中断服务的情况下升级软件
- 平滑升级过程:
- 获取老版本的编译信息
- 获取新版本的安装包或功能包
- 配置新版本或功能,配置时加上老版本的编译信息和新版本或功能(–add-module)
- 编译,编译完后不能执行安装操作
- 备份老版本程序,使用复制的方式
- 停掉老本版程序的进程
- 将新版本程序复制到奥版本所在位置替换掉老版本
- 启动新版本
下载新版本的nginx
[root@nginx ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@nginx ~]# ls
anaconda-ks.cfg nginx-1.20.2 nginx-1.20.2.tar.gz nginx-1.22.0.tar.gz
获取原版本nginx的编译信息
[root@nginx ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module
配置新版本和功能
#下载工具来拉取gitee仓库的功能
[root@nginx ~]# dnf -y install git
[root@nginx ~]# git clone https://gitee.com/wujunze/nginx_module_echo.git
Cloning into 'nginx_module_echo'...
remote: Enumerating objects: 80, done.
remote: Total 80 (delta 0), reused 0 (delta 0), pack-reused 80
Unpacking objects: 100% (80/80), 14.32 KiB | 1.43 MiB/s, done.
[root@nginx ~]# ls
anaconda-ks.cfg nginx-1.20.2 nginx-1.20.2.tar.gz nginx-1.22.0.tar.gz nginx_module_echo#解压
[root@nginx ~]# tar xf nginx-1.22.0.tar.gz
[root@nginx ~]# ls
123 anaconda-ks.cfg guazai lv0 nginx-1.20.2 nginx-1.20.2.tar.gz nginx-1.22.0 nginx-1.22.0.tar.gz nginx_module_echo
[root@nginx ~]# cd nginx-1.22.0/
[root@nginx nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=../nginx_module_echo
make进行编译,千万不能make install安装
[root@nginx nginx-1.22.0]# make
进行替换
#将老版本的nginx备份,然后杀掉nginx进程,将新版本的nginx强行复制到老版本的位置,然后启动nginx服务
[root@nginx nginx-1.22.0]# cp /usr/local/nginx/sbin/nginx{,.bak};pkill nginx;\cp objs/nginx /usr/local/nginx/sbin/;systemctl start nginx
[root@nginx nginx-1.22.0]# nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=../nginx_module_echo
nginx配置文件
主配置文件:/usr/local/nginx/conf/nginx.conf
默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
可以在启动nginx时通过-c选项来指定要读取的配置文件
nginx常见的配置文件及其作用
配置文件 | 作用 |
---|---|
nginx.conf | nginx的基本配置文件 |
mime.types | MIME类型关联的扩展文件 |
fastcgi.conf | 与fastcgi相关的配置 |
proxy.conf | 与proxy相关的配置 |
sites.conf | 配置nginx提供的网站,包括虚拟主机 |
nginx.conf配置详解
nginx.conf的内容分为以下几段:
- main配置段:全局配置段。其中main配置段中可能包含event配置段
- event {}:定义event模型工作特性
- http {}:定义http协议相关的配置
配置指令:要以分号结尾,语法格式如下:
derective value1 [value2 ...];
支持使用变量:内置变量:模块会提供内建变量定义
自定义变量:set var_name value
用于调试、定位问题的配置参数
daemon {on|off}; //是否以守护进程方式运行nginx,调试时应设置为off
master_process {on|off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别; //配置错误日志
error_log里的位置和级别能有以下可选项:
位置 | 级别 |
---|---|
file stderr syslog:server=address[,parameter=value] memory:size |
debug:若要使用debug级别,需要在编译nginx时使用–with-debug选项 info notice warn error crit alert emerg |
正常运行必备的配置参数
user USERNAME [GROUPNAME]; //指定运行worker进程的用户和组
pid /path/to/pid_file; //指定nginx守护进程的pid文件
worker_rlimit_nofile number; //设置所有worker进程最大可以打开的文件数,默认为1024
worker_rlimit_core size; //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
优化性能的配置参数
worker_processes n; //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
worker_cpu_affinity cpumask ...; //将进程绑定到某cpu中,避免频繁刷新缓存
//cpumask:使用8位二进制表示cpu核心,如:0000 0001 //第一颗cpu核心0000 0010 //第二颗cpu核心0000 0100 //第三颗cpu核心0000 1000 //第四颗cpu核心0001 0000 //第五颗cpu核心0010 0000 //第六颗cpu核心0100 0000 //第七颗cpu核心1000 0000 //第八颗cpu核心
timer_resolution interval; //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数
worker_priority number; //指明worker进程的nice值
6.5 事件相关的配置:event{}段中的配置参数
accept_mutex {off|on}; //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求
lock_file file; //accept_mutex用到的互斥锁锁文件路径
use [epoll | rtsig | select | poll]; //指明使用的事件模型,建议让nginx自行选择
worker_connections #; //每个进程能够接受的最大连接数
网络连接相关的配置参数
keepalive_timeout number; //长连接的超时时长,默认为65s
keepalive_requests number; //在一个长连接上所能够允许请求的最大资源数
keepalive_disable [msie6|safari|none]; //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off; //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number; //读取http请求报文首部的超时时长
client_body_timeout number; //读取http请求报文body部分的超时时长
send_timeout number; //发送响应报文的超时时长
fastcgi的相关配置参数
LNMP:php要启用fpm模型
配置示例如下:
location ~ \.php$ {root html;fastcgi_pass 127.0.0.1:9000; //定义反向代理fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;include fastcgi_params;
}
nginx作为web服务器时使用的配置:http{}段的配置参数
http{…}:配置http相关,由ngx_http_core_module模块引入。nginx的HTTP配置主要包括四个区块,结构如下:
http {//协议级别include mime.types;default_type application/octet-stream;keepalive_timeout 65;gzip on;upstream {//负载均衡配置...}server {//服务器级别,每个server类似于httpd中的一个<VirtualHost>listen 80;server_name localhost;location / {//请求级别,类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系root html;index index.html index.htm;}}
}
http{}段配置指令:
server {}:定义一个虚拟主机,示例如下:
server {listen 80;server_name www.idfsoft.com;root "/vhosts/web";
}
listen:指定监听的地址和端口
listen address[:port];
listen port;
server_name NAME [...]; //后面可跟多个主机,名称可使用正则表达式或通配符
当有多个server时,匹配顺序如下:
- 先做精确匹配检查
- 左侧通配符匹配检查,如*.idfsoft.com
- 右侧通配符匹配检查,如mail.*
- 正则表达式匹配检查,如~ ^.*.idfsoft.com$
- default_server
root path; 设置资源路径映射,用于指明请求的URL所对应的资源所在的文件系统上的起始路径
alias path; 用于location配置段,定义路径别名
index file; 默认主页面
index index.php index.html;error_page code […] [=code] URI | @name 根据http响应状态码来指明特用的错误页面,例如 error_page 404 /404_customed.html
[=code]:以指定的响应码进行响应,而不是默认的原来的响应,默认表示以新资源的响应码为其响应码,例如 error_page 404 =200 /404_customed.html
log_format 定义日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;//注意:格式名main可自己定义,但要一一对应,另外此处可用变量为nginx各模块内建变量
location区段,通过指定模式来与客户端请求的URI相匹配
//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能//语法:location [ 修饰符 ] pattern {......}
常用修饰符说明:
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
- 查找顺序和优先级:由高到底依次为
- 带有=的精确匹配优先
- 带有^~修饰符的,开头匹配
- 正则表达式按照他们在配置文件中定义的顺序
- 带有或*修饰符的,如果正则表达式与URI匹配
- 没有修饰符的精确匹配
优先级顺序如下:
( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )
展示效果:
//首先修改配置文件,这里匹配的有=修饰符,^~修饰符,无修饰符
##注意我这里配置文件里能使用echo,是添加了echo模块
…………server {listen 80;server_name localhost;location / { //模糊匹配"/"开头的URI,这个优先级最低,当其他的都匹配不到时,匹配这个echo "/";}location = /abc { //精确匹配URI为"/abc"echo "= abc";}location ^~ /abc { //匹配以"/abc"开头的URI,优先级低于修饰符"="echo "^abc";}location ~ ^/a { //使用正则表达式匹配以"a"开头的URI,优先级低于修饰符"^~"echo "^/a";[root@nginx ~]# systemctl reload nginx.service //进行测试##1.这次访问的的是"/abc",它的URI就是"/abc",被"location = /abc"精确匹配到,所以说修饰符"="优先级最高,打印了"=abc"
[root@nginx ~]# curl http://127.0.0.1/abc
= abc
##注意"?"后面部分不属于URI,所以说还能被"location = /abc",精确匹配到
[root@nginx ~]# curl http://127.0.0.1/abc?aa
= abc##2.访问这个的URI是"/abc/",所以说不能被"location = /abc"精确匹配到,但是这个URI是以"/abc"开头的,所以说被"location ^~ /abc"优先匹配到
[root@nginx ~]# curl http://127.0.0.1/abc/
^abc
[root@nginx ~]# curl http://127.0.0.1/abc/abc
^abc
[root@nginx ~]# curl http://127.0.0.1/abcd/a
^abc
[root@nginx ~]# curl http://127.0.0.1/abcdef?aa
^abc##3.只要不被前两条匹配到,且以"a"开头的URI,就都会被"location ~ ^/a"匹配到
[root@nginx ~]# curl http://127.0.0.1/a
^/a
[root@nginx ~]# curl http://127.0.0.1/ab
^/a
[root@nginx ~]# curl http://127.0.0.1/a/a?a
^/a##4.只要上面的条件都不被匹配到"/",符合条件的只有"location /"这一条
[root@nginx ~]# curl http://127.0.0.1
/
[root@nginx ~]# curl http://127.0.0.1/
/
[root@nginx ~]# curl http://127.0.0.1/b
/
[root@nginx ~]# curl http://127.0.0.1/b/a
/
[root@nginx ~]# curl http://127.0.0.1/b/a?a
/
nginx平滑升级与配置文件详解相关推荐
- NGINX 安装、启停、平滑升级、配置文件详解
NGINX安装.启停.平滑升级 一.NGINX 安装 1.下载nginx 2.nginx解压安装 3.预先安装 4.nginx编译 5.安装nginx 6.查看安装路径 7.启动nginx 二.NGI ...
- Nginx三部曲之一【配置文件详解】
初学Nginx,感觉Nginx配置文件中指令以及参数各类繁多,总结成博客备忘,也便广大Linux爱好者学习交流,因为时间原因,总结的不够全面后续会不断完善此博文,笔者水平有限,如有疏漏不妥之处,还请不 ...
- Nginx综合介绍以及配置文件详解
Nginx介绍(事件驱动框架和异步处理) Nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:Nginx可以作为一个HTTP服务 ...
- Nginx编译安装及配置文件详解
写在前面 Centos版本:Centos 7.6 - 64bit Nginx版本:1.20.2 一.什么是Nginx Nginx (engine x) 是一款轻量级的Web 服务器 .反向代理服务器及 ...
- nginx服务器安装及配置文件详解
nginx服务器安装及配置文件详解 seanlook 5月26日 发布 4 推荐 97 收藏,9.2k 浏览 nginx在工作中已经有好几个环境在使用了,每次都是重新去网上扒博客,各种编译配置,今天自 ...
- Nginx(二):反向代理原理 与 配置文件详解
相关阅读: Nginx(一):Nginx原理概述 与 安装步骤详解 Nginx(二):反向代理原理 与 配置文件详解 Nginx(三):负载均衡策略 与 Nginx静态服务器 Nginx(四):Ngi ...
- nginx.conf 配置文件详解
本文来说下nginx.conf 配置文件详解 文章目录 Nginx 配置文件概述 nginx.conf 配置文件 通用(这里是 windows 系统系 nginx1.14.2 下 nginx.conf ...
- nginx的配置文件详解
文章目录 1. nginx的配置文件详解 2. nginx.conf配置详解 3. 用于调试.定位问题的配置参数 4. 正常运行必备的配置参数 5. 优化性能的配置参数 6. 事件相关的配置:even ...
- 配置nginx方向代理,实现URL隐形转发 (附带nginx配置文件详解)
配置nginx方向代理,实现URL隐形转发 (附带nginx配置文件详解) 免费领取满减阿里云红包 项目名称:[域名解析–隐形URL转发]–centos 7.3 环境: 阿里云服务器之上有多个tomc ...
最新文章
- 植物根系微生物组的调控入选中科院科技创新亮点成果筛选
- c++中的数组和指针,引用
- Qt5.7 + VS2015 环境搭建
- [Android学习笔记二] View转化Bitmap
- Scala模式匹配:类型匹配
- 3dContactPointAnnotationTool开发日志(三三)
- 综合学生信息管理系统(JSP+JDBC)
- python字典和集合双向索引_Python字典和集合
- 论文学习17-Global Normalization of Convolutional Neural Networks(联合实体关系抽取CNN+CRF)2017
- 自己动手架设linux下Web服务器(图)3
- 工商银行:应用多k8s集群管理及容灾实践
- python爬虫之requests模块2
- mysql双一参数_MySQL 的双1设置-数据安全的关键参数(案例分享)
- 数据存储过程之MySQL与ORACLE数据库的差别
- JavaScript经纬度和地图坐标相互转换
- endnote找不到国标_实验差距惊人!揭秘雅迪高于新国标的品质标准测试
- 消息队列在linux下实现进程间的通信;消息盒子在单片机中的代码实现
- 多系统启动菜单的修复EasyBoot
- 禁止迅雷极速版强制升级方法
- 《光剑拾字编》甲子篇: 天干地支
热门文章
- 【Docker Desktop】Docker Desktop的安装与使用:
- Mybatis | Mybatis标签association一对一的使用
- nginx 指定配置文件 启动 重启
- 我的家乡阳江----“漠阳文化”介绍
- 东华大学计算机学院迎新晚会,东华大学学生艺术团2014-2015学年迎新晚会、快闪演出(9月28-29日)...
- 论文学习笔记(4):Intrinsic and Isotropic Resampling for 3D Point Clouds(内在控制的各向同性3D点云重采样)
- HTML5 Video 添加字幕,操作简单,不需要剪辑
- 威联通 ipv6设置
- Windows Batch bat批处理脚本 停止和启动IIS服务,可用于自动重启方案
- 笔记本哪个cpu好(intel 酷睿双核)