+# 企业高性能web服务器
1、Nginx 编译安装
1.1 编译安装 Nginx
- 这里下载nginx-1.24.0.tar.gz和nginx-1.26.1.tar.gz
- 可以在官方网站上下载:https://nginx.org/en/download.html
示例:nginx-1.24.0.tar.gz
#提前将编译安装出现问题的安装包下载好,如下图所示
[root@nginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y
[root@nginx ~]# tar zxf nginx-1.24.0.tar.gz
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
#关闭debug功能
[root@nginx nginx-1.24.0]# vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g"
[root@nginx nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
#编译安装,这里可以使用./configure --help查看
[root@Nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \ # 指定nginx运行用户
--group=nginx \ # 指定nginx运行组
--with-http_ssl_module \ # 支持https://
--with-http_v2_module \ # 支持http版本2
--with-http_realip_module \ # 支持ip透传
--with-http_stub_status_module \ # 支持状态页面
--with-http_gzip_static_module \ # 支持压缩
--with-pcre \ # 支持正则
--with-stream \ # 支持tcp反向代理
--with-stream_ssl_module \ # 支持tcp的ssl加密
--with-stream_realip_module # 支持tcp的透传ip
#make完成后再看objs目录,又有新文件
[root@nginx nginx-1.24.0]# make && make install
1.2验证版本及编译参数
#将nginx软件的命令执行路陷阱添加到环境变量中
[root@nginx ~]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@nginx ~]# source ~/.bash_profile
#验证版本
[root@nginx ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --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 --
with-stream_realip_module
#当前nginx进程
[root@nginx nginx-1.24.0]# ps aux | grep nginx
root 47007 0.0 0.0 9864 2560 ? Ss 19:35 0:00 nginx: master process nginx
nginx 47008 0.0 0.1 13760 5236 ? S 19:35 0:00 nginx: worker process
root 48897 0.0 0.0 221664 2292 pts/2 S+ 22:41 0:00 grep --color=auto nginx
#脚本启动nginx
[root@nginx ~]# cd /usr/local/nginx/sbin/
#nginx的配置文件
[root@nginx sbin]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx sbin]# nginx -g "worker_processes 6"
nginx: [emerg] unexpected end of parameter, expecting ";" in command line
[root@nginx sbin]# nginx -g "worker_processes 6;"
[root@nginx sbin]# ps aux | grep nginx
root 49104 0.0 0.0 9864 932 ? Ss 23:24 0:00 nginx: master process nginx -g worker_processes 6;
nginx 49105 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
nginx 49106 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
nginx 49107 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
nginx 49108 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
nginx 49109 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
nginx 49110 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
root 49112 0.0 0.0 221664 2292 pts/2 S+ 23:24 0:00 grep --color=auto nginx
```bash
#nginx 启动文件
[root@nginx sbin]# vim /usr/local/nginx/conf/nginx.conf
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@nginx sbin]# systemctl daemon-reload
[root@nginx sbin]# nginx -s stop
[root@nginx sbin]# ps aux | grep nginx
root 49193 0.0 0.0 221664 2296 pts/2 S+ 23:28 0:00 grep --color=auto nginx
[root@nginx sbin]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx sbin]# ps aux | grep nginx
root 49227 0.0 0.0 9864 928 ? Ss 23:28 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 49228 0.0 0.1 13760 4748 ? S 23:28 0:00 nginx: worker process
root 49230 0.0 0.0 221664 2292 pts/2 S+ 23:28 0:00 grep --color=auto nginx
[root@nginx ~]# nginx -s quit
[root@nginx ~]# ps aux | grep nginx
root 48970 0.0 0.0 221664 2292 pts/2 S+ 22:48 0:00 grep --color=auto nginx
#前台运行
[root@nginx ~]# nginx -g "daemon off"
nginx: [emerg] unexpected end of parameter, expecting ";" in command line
示例:nginx-1.26.1.tar.gz
[root@nginx ~]# tar zxf nginx-1.26.1.tar.gz
[root@nginx ~]# cd nginx-1.26.1/
#开始编译新版本
[root@nginx nginx-1.26.1]# ./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx --add-module=/root/echo-nginx-module-0.63 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module
#只要make无需要make install,如果make install就会将原来的配置文件覆盖掉
[root@nginx nginx-1.26.1]# make
#二进制文件复制
[root@nginx nginx-1.26.1]# cd objs/
[root@nginx objs]# cp -f nginx /usr/local/nginx/sbin/nginx
#把之前的旧版的nginx命令备份
[root@nginx ~]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# cp nginx nginx.24
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx sbin]# ps aux | grep nginx
root 49020 0.0 0.0 221664 2268 pts/2 S+ 23:07 0:00 grep --color=auto nginx
#nginx worker ID
[root@nginx sbin]# kill -USR2 49020
2、平滑升级和回滚
有时候我们需要对Nginx版本进行升级以满足对其功能的需求,例如添加新模块,需要新功能,而此时Nginx又在跑着业务无法停掉,这时我们就可能选择平滑升级
[root@Nginx sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0 ##依旧是旧版本生生效
Date: Thu, 18 Jul 2024 07:45:58 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 18 Jul 2024 03:41:13 GMT
Connection: keep-alive
ETag: "66988ed9-267"
Accept-Ranges: bytes
#回收旧版本
[root@nginx sbin]# kill -WINCH 49227
[root@nginx sbin]# ps aux | grep nginx
root 49227 0.0 0.0 9864 928 ? Ss 23:28 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 49235 0.0 0.0 221664 2280 pts/2 S+ 23:34 0:00 grep --color=auto nginx
3、 Nginx 核心配置详解
3.1 配置文件说明
- 主配置文件:[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#主配置文件结构:四部分
main block:主配置段,即全局配置段,对http,mail都有效
#事件驱动相关的配置
event {
...
}
#http/https 协议相关配置段
http {
默认的nginx.conf 配置文件格式说明
...
}
#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {
...
}
#stream 服务器相关配置段
stream {
...
}
#默认的nginx.conf 配置文件格式说明
#全局配置端,对全局生效,主要设置nginx的启动用户/组,启动的工作进程数量,工作模式,Nginx的PID路
径,日志路径等。
user nginx nginx;
worker_processes 1; #启动工作进程数数量
events {
#events #设置快,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多
个网络连接,使用哪种事件驱动模型 #处理请求,每个工作进程可以同时支持的
最大连接数,是否开启对多工作进程下的网络连接进行序列化等。
worker_connections 1024; #设置单个nginx工作进程可以接受的最大并发,作为web服务器
的时候最大并发数为 #worker_connections *
worker_processes,作为反向代理的时候为
#(worker_connections * worker_processes)/2
}
http {
#http块是Nginx服务器配置中的重要部分,缓存、代理和日志格
式定义等绝大多数功能和第三方模块都 #可以在这设置,http块可
以包含多个server块,而一个server块中又可以包含多个location块,
#server块可以配置文件引入、MIME-Type定义、日志自定义、是
否启用sendfile、连接超时时间和 #单个链接的请求上限等。
include mime.types;
default_type application/octet-stream;
sendfile on; #作为web服务器的时候打开sendfile加快静态文件传输,指定是
否使用
#sendfile系统调用来传输文件
#sendfile系统调用在两个文件描述符之间直接传递数据(完全在
内核中操作)
#从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率
很高,被称之为零拷贝,
#硬盘 >> kernel buffer (快速拷贝到kernelsocket
buffer) >>协议栈。
keepalive_timeout 65; #长连接超时时间,单位是秒
server {
#设置一个虚拟机主机,可以包含自己的全局快,同时也可以包含多
个location模块
#比如本虚拟机监听的端口、本虚拟机的名称和IP配置,多个
server 可以使用一个端口比如都使用 #80端口提供web服务
listen 80; #配置server监听的端口
3.2 全局配置
Main 全局配置段常见的配置指令分类
正常运行必备的配置
优化性能相关的配置
用于调试及定位问题相关的配置
事件驱动相关的配置
全局配置说明:
server_name localhost; #本server的名称,当访问此名称的时候nginx会调用当前serevr
内部的配置进程匹配。
location / {
#location其实是server的一个指令,为nginx服务器提供比较
多而且灵活的指令
#都是在location中体现的,主要是基于nginx接受到的请求字符
串
#对用户请求的UIL进行匹配,并对特定的指令进行处理
#包括地址重定向、数据缓存和应答控制等功能都是在这部分实现
#另外很多第三方模块的配置也是在location模块中配置。
root html; #相当于默认页面的目录名称,默认是安装目录的相对路径,可以使
用绝对路径配置。
index index.html index.htm; #默认的页面文件名称
}
error_page 500 502 503 504 /50x.html; #错误页面的文件名称
location = /50x.html {
#location处理对应的不同错误码的页面定
义到/50x.html
#这个跟对应其server中定义的目录下。
root html; #定义默认页面所在的目录
}
}
#和邮件相关的配置
#mail {
# ...
# } mail 协议相关配置段
#tcp代理配置,1.9版本以上支持
#stream {
# ...
# } stream 服务器相关配置段
#导入其他路径的配置文件
#include /apps/nginx/conf.d/*.conf
}
示例:
#系统有几个cpu就启动几个子线程
[root@nginx ~]# lscpu
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf