一、什么是Nginx
Nginx是一个高性能的HTTP和反向代理服务器,特点是占用内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好
二、安装
1、准备Nginx的源码并解压
2、解压,开始安装
2.1关闭debug
vim auto/cc/gcc
2.2在 server1 安装模块
首次安装模块缺少服务
yum install -y gcc
yum install -y pcre-devel
yum install -y openssl-devel
模块安装成功
编译
安装部署
安装成功
三、在server1创建一个nginx用户
useradd -d /usr/local/nginx/ -M -s /sbin/nologin nginx
四、将服务部署到server2与server3
scp -r /usr/local/nginx server2:/usr/local/
scp -r /usr/local/nginx server3:/usr/local/
五、安全优化
cd /root/nginx-1.20.1
vim src/core/nginx.h
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-file-aio --with-http_ssl_module --with-threads
make
make install
cd objs/
\cp -f nginx /usr/local/nginx/sbin/nginx
nginx -s stop
nginx
curl -I localhost
添加nginx至系统进程
scp nginx.service root@172.25.70.1:/usr/lib/systemd/system/nginx.service
下载至/usr/lib/systemd/system/nginx.service
当启动系统级nginx服务需要先kill之前nginx进程
ps ax | grep nginx / kill -9 23171 / kill -9 23172
systemctl start nginx
systemctl status nginx
systemctl enable nginx ##开机自启动
3、Nginx 配置文件的介绍
/usr/local/nginx/conf/nginx.conf ##主配置文件
/usr/local/nginx/logs ##日志文件
/usr/local/nginx/html ##静态文件
主配置文件层级结构
主配置文件介绍
# main 段配置信息(全局配置,对全局生效)
user nginx; ## 运行用户,默认即是 Nginx,可以不进行设置
worker_processes auto; ## Nginx 进程数,一般设置为和 CPU 核数一样 也可表示2个cpu(01 10)4个cpu(0001 0010 0100 1000)
error_log /var/log/nginx/error.log warn; ## Nginx 的错误日志存放目录
pid /var/run/nginx.pid; ## Nginx 服务启动时的 pid 存放位置 nginx主进程号
# events 段配置信息(配置影响 Nginx 服务器与用户的网络连接)
events {
use epoll; ## 使用 epoll 的 I/O 模型(如果你不知道 Nginx 该使用哪种轮询方法,会自动选择一个最适合你操作系统的)
worker_connections 1024; ## 每个进程允许最大并发数
}
# http 段配置信息(配置使用最频繁的部分,代理、缓存、日志定义等绝大多数功能和第三方模块的配置都在这里设置)
http {
# 设置日志模式
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 /var/log/nginx/access.log main; ## Nginx 访问日志存放位置
sendfile on; ## 开启高效传输模式
tcp_nopush on; ## 减少网络报文段的数量
tcp_nodelay on;
keepalive_timeout 65; ## 保持连接的时间,也叫超时时间,单位秒
# server段配置信息
server {
listen 80; ## 配置监听的端口
server_name localhost; ## 配置的域名
# location段配置信息
location / {
root /usr/share/nginx/html; ## 网站根目录
index index.html index.htm; ## 默认首页文件
deny xxx.xxx.xxx.xxx; ## 禁止访问的 ip 地址,可以为 all
allow xxx.xxx.xxx.xxx; ## 允许访问的 ip 地址,可以为 all
}
error_page 500 502 503 504 /50x.html; ## 默认 50x 对应的访问页面
location = /50x.html {
root html;
}
}
}
六、并发优化
当Nginx处于高压状态时如:某一用户同时占用多个系统资源会出现“Too many open files”问题
lscpu ##查看cpu信息
ulimit -a ##限制系统用户对shell资源的访问
sysctl -a |grep file ##查看程序能接受最大文件连接数
命令修改 ##ulimit -n 65535
配置文件修改 ##/etc/security/limits.conf
grep 'open files' /proc/$(cat /usr/local/nginx/logs/nginx.pid)/limits ##查看文件最大连接数
free -m
七、Nginx 更新
1、平滑升级
当服务器在运行的时候需要升级版本的情况下,需要用到平滑升级,平滑升级可以在服务器运行的状态下最大限度的保持数据的完整性。
cd /usr/local/nginx/sbin
\cp -f nginx nginx.old
curl -I localhost
cd
tar zxf nginx-1.21.1.tar.gz
cd /nginx-1.21.1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-file-aio --with-http_ssl_module --with-threads
make
mkse install
cd objs/
\cp -f nginx /usr/local/nginx/sbin/nginx
ps ax | gerp nginx
kill -USR2 masterID ##杀死原主进程ID
kill -WINCH masterID ##新版本可能会出现一些问题,此命令保留master杀死worker
curl -I localhost
2、平滑回退
回退的过程与更新相反,先还原nginx程序,唤醒旧进程,退出并关闭新版本
cd /usr/local/nginx/sbin
\cp -f nginx.old nginx
ps ax | grep nginx
kill -HUP masterID ##唤醒旧版本master中worker
kill -WINCH masterID ##回收新版本中master中的worker
kill -QUIT masterID ##退出新版本进程ID
curl -I localhost