nginx
nginx 简介
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。
官网
nginx.org
nginx.com
nginx与apache对比
安装配置
安装包从官方获取
rpm包安装:
rpm -ivh nginx.rpm
源码包安装:
tar zxf nginx-1.20.1.tar.gz #解压
cd nginx-1.20.1/
预编译 ,检测模块和环境是否可以满足安装
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
若报错出现依赖性,则需要逐个安装依赖包
yum install -y pcre-devel openssl-devel gcc
--prefix=/usr/local/nginx
安装路径
--with-http_ssl_module
添加https支持,openssl模块
--with-http_stub_status_module
监控模块,nginx访问情况
--with-threads
启用thread pool支持
--with-file-aio
启用file aio支持,异步读写
编译,将源码编译为二进制文件。编译完成之后,在./obj目录下会生成nginx可执行文件
make
安装,使用 Makefile 安装软件。会在prefix所指定的目录生成nginx安装文件。
make install
开启nginx,测试
/usr/local/nginx/sbin/nginx #预编译时指定的路径下
curl localhost
配置软连接
将nginx二进制文件链接进系统PATH 内,可以直接使用nginx命令启用nginx
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
精简二进制文件
在之前编译安装的目录中,清理之前的编译残留文件
make clean
关闭gcc的debug模式,修改nginx 版本
vim auto/cc/gcc
171 #CFLAGS="$CFLAGS -g"
vim src/core/nginx.h
13 #define NGINX_VERSION "ultra-nginx"
重新预编译,以及编译,编译成功后会在objs中生成可执行文件,此时文件大小远小于之前。
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
make
先关闭nginx将此nginx的二进制文件,放入之前的路径下即可更新/usr/local/nginx/sbin/
,nginx的更新只需要替换二进制文件即可
/usr/local/nginx/sbin/nginx -s stop
cp objs/nginx /usr/local/nginx/sbin/
/usr/local/nginx/sbin/nginx
测试 :
配置用户/最大连接数
在/usr/local/nginx/conf/nginx.conf
中可以定义nginx使用的用户,以及worker数,一般worker数与cpu核心数一致。使用lscpu
命令可以查看。
user nginx;
worker_processes auto;#默认与核心数保持一致,auto为自动识别
events {
worker_connections 65535; #最大连接数
}
若使用nginx用户,则需要添加该用户
useradd -d /usr/local/nginx -M -s /sbin/nologin nginx
一个应用程序能开启的最大连接数是受到限制的,内核限制>系统限制>应用软件限制。
首先需要查看内核的文件限制,这与系统的内存有关,所设置的最大连接数小于此数目即可;
sysctl -a | grep file
操作系统限制文件储存在,/etc/security/limits.conf
,编辑文件添加内容,此值同样需要大于等于所设定的最大连接数。
vim /etc/security/limits.conf
nginx - nofile 65535 #
刷新nginx,查看worker数
nginx -s reload
配置自定义 systemctl
编辑/usr/lib/systemd/system/nginx.service
添加内容
[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
重新加载daemon, 注,在配置前一定关闭nginx,否则会报错。如报错,杀两次占用80端口的进程,第一次为master 第二次为worker
systemctl daemon-reload
systemctl start nginx
反向代理&负载均衡
nginx提供反向代理服务,同时也提供不同的负载均衡算法。
在两个后端配置nginx服务,默认连接数为1024即可,无需设定用户。
设定不同的web默认页面以示区别。
172.25.52.2
172.25.52.3
在调度端设置默认发布页面,来进行区分。
在调度端,编辑主配置文件,添加负载均衡器
在http模块中添加负载均衡器,默认为rr调度,可指定权重,设置本机为备份。
upstream westos{
server 172.25.52.2:80 weight=2;
server 172.25.52.3:80;
server localhost:80 backup;
}
在http模块中添加虚拟主机,设定虚拟域名以及负载均衡器。
server {
listen 80;
server_name www.westos.com;
location / {
proxy_pass http://westos;
}
}
在宿主机配置解析虚拟主机名至调度端
echo "172.25.52.1 www.westos.com" > /etc/hosts
测试负载均衡:
当后端nginx都停止服务时,使用backup节点。
平滑升级
升级
下载新版本的nginx,与之前的配置相同。
vim auto/cc/gcc
#注释debug
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &>/dev/null
make &>/dev/null
不修改版本号,跟原版区分下。
备份/usr/local/nginx/sbin/nginx
,在objs目录下,找到编译生成的nginx二进制文件,替换原来的二进制文件。
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
mv objs/nginx /usr/local/nginx/sbin/nginx
查看进程id
发送升级信号,会在原master不关闭的情况下关闭其worker,并且新开master以及worker,使用新的nginx文件
kill -USR2 25833
kill -WINCH 25833
升级前版本为 ultra-nginx
,升级后为1.21.1
回滚
针对升级前的master进程,开启woker节点
kill -WINCH 25833
kill -HUP 25833
关闭新版本的woker节点:
kill -WINCH 25907
测试,版本从1.21.1
回退至ultra-ngin
会话保持模块
在多台后台服务器的环境下,为了确保一个客户只和一台服务器通信,势必使用长连接。常见的有使用nginx自带的ip_hash来做,但如果前端是CDN,或者说一个局域网的客户同时访问服务器,导致出现服务器分配不均衡,以及不能保证每次访问都粘滞在同一台服务器。如果基于cookie可以在保持长连接的同时还保证了服务器的压力均衡,ngx_http_sticky_module可以实现此功能。
前提是浏览器或访问工具具备cookie
下载解压ngx_http_sticky_module,在nginx源码文件夹中重新预编译,添加此模块。
unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip #解压
cd nginx-1.21.1 #进入源码目录
make clean #清理上次编译的结果
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42 #重新预编译
重新编译,覆盖之前的二进制文件,编辑配置文件
\cp -f nginx /usr/local/nginx/sbin/
vim /usr/local/nginx/conf/nginx.conf
nginx -s reload
upstream westos{
sticky; #开启sticky
server 172.25.52.2:80 weight=2;
server 172.25.52.3:80;
}
curl访问测试,无cookie,不做会话保持
浏览器访问,可以加载cookie
限流
限制并发
vim /usr/local/nginx/conf/nginx.conf
在http模块中添加
# 通过访问的IP 来开辟内存域 ,大小为10m 限制download的连接数为1
limit_conn_zone $binary_remote_addr zone=addr:10m;
# 在localhost:80 server中添加
location /download/ {
limit_conn addr 1; #限制并发数
limit_rate 50k; #限制带宽
}
创建download目录,在其中放入文件,提供测试。
ab -c10 -n 10 http://172.25.52.1/download/vim.jpg
此时,在日志中可以看到真实nginx对与访问的响应,可能与压力测试返回结果有所出入
限制请求率
使用此配置,NGINX每秒处理不超过1的请求
在http模块中添加
# 表示允许相同标识的客户端的访问频次,这里限制的是每秒1次
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
# 在localhost:80 server中添加
location /download/ {
limit_req zone=one; #限制请求速率
}
nginx -s reload
仅仅正常处理了第一个请求,超过速率的9个全部失败。
- 添加队列
#超出zone限制的请求会被放入数目为5的队列中
location /download/ {
limit_req zone=one burst=5;
}
reload之后再次进行测试
nginx -s reload
ab -c1 -n 10 http://172.25.52.1/download/vim.jpg
没有失败的请求,耗时较久,平均一秒一个请求
- 配置延时
在队列外的超限访问请求直接拒绝
自动索引
当访问location时未指定路径,且路径下无主页文件时,开启自动索引将会显示该location的目录结构。
location /download/ {
autoindex on;
}
reload并测试:
Nginx expire缓存配置
缓存可以降低网站带宽,加速用户访问。
在server模块内添加参数
location ~ .*\.(gif|jpg|png)$ { #缓存的文件格式
expires 365d; #缓存存放时间
root html;
}
访问server/path/x.jpg时,将会访问nginx工作目录下的html下的path内的x.jpg
测试缓存;
日志监控
编写脚本用于记录日志,添加crontab定时任务
vim /opt/scripts/nginxlog.sh
#!/bin/bash
cd /usr/local/nginx/logs && mv access.log access_$(date +%F -d -1day).log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
添加crontab任务
crontab -e
00 00 * * * /opt/scripts/nginxlog.sh &> /dev/null
设定日志权限
chmod -R 700 /usr/local/nginx/logs
编辑nginx主配置文件,添加内容
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location ~ .*\.(js|jpg|jpeg|png|css|bmp|gif)$ {
access_log off;
}
location ~ ^/images/.*\.(sh|php)$ {
deny all;
}
charset utf-8;
访问监控页面测试:
IP访问失败,本机访问成功
重定向
防止恶意解析
防止域名恶意解析到服务器IP,需要在访问时配置拒绝的域名到服务器ip的解析:
server {
listen 80;
server_name www.westos.com;
return 500;
}
图中的负载均衡器可以注释掉
- 也可以配置重写
rewrite ^(.*) http://www.westos.org permanent;
80重定向443
编辑主配置文件,启用443,修改证书密钥文件名
添加重写规则
使用脚本生成pem文件
将生成的pem拷贝至nginx conf目录下
cp /usr/local/nginx/cert.pem /usr/local/nginx/conf/cert.pem
重新加载nginx,查看端口是否打开
访问http:server,会重定向至https
- 网页重写策略
www.westos.org/bbs 重定向bbs.westos.org:
rewrite ^/bbs$ http://bbs.westos.org permanent;
rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;
bbs.westos.org 重定向www.westos.org/bbs:
if ($host = "bbs.westos.org"){
rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent;
}
nginx 防盗链
location ~ \.(jpg|png)$ {
valid_referers none blocked www.westos.org;
if ($invalid_referer) {
return 403;
#rewrite ^/ http://www2.westos.org/daolian.jpg;
}
}