点击上方蓝字 关注大侠之运维
nginx是一款同时可以作为Web服务器以及方向代理负载均衡的软件,在netcraft网站上也可以看到,截止到本月,nginx在全球站点的占比中占据了30%多,目前有3.48亿。
图片来源:https://news.netcraft.com/archives/2022/07/28/july-2022-web-server-survey.html
在具体介绍nginx之前,先来聊下什么是反向代理?
为了提高单个服务器的处理能力和冗余性,在后端服务一组服务器之前搭建一组代理服务器,然后将域名解析到代理服务器上,由该服务器负责向后端转发请求。
反向代理服务器有如下特点:
1.统一流量入口
2.对后端节点可以进行健康检查,可以稳定的提供服务
3.可以有缓存功能,节省资源
4.屏蔽不同硬件或者系统的后端服务器
♦️
安装
nginx安装过程比较简单
#yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
#wget http://nginx.org/download/nginx-1.20.2.tar.gz
#tar -xvf nginx-1.20.2.tar.gz
#cd nginx-1.20.2
#./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-pcre
#make && make install
编译中:
--with-pcre 支持正则表达式
--with-http_stub_status_module 添加对页面状态的支持
--with-http_ssl_module 在nginx上添加对https的支持
--with-stream 1.9.0之后才加的功能,支持Tcp四层的协议转发
♦️
nginx配置 优化
nginx配置--基础配置
user root;
worker_processes 4;
#四核开启四个进程
worker_cpu_affinity 0001 0010 0100 1000;
pid data/nginxlog/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
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 data/nginxlog/access.log main;
error_log data/nginxlog/error.log ;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
include usr/local/nginx/conf/conf.d/*.conf;
gzip on;
gzip_min_length 40k;
gzip_comp_level 5;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/xml text/javascript application/json image/springframework.bpng image/gif image/jpeg;
gzip_vary on;
server {
listen 80;
server_name localhost;
absolute_redirect off;
client_header_buffer_size 10m;
client_max_body_size 200M;
large_client_header_buffers 16 10m;
location web-portal {
alias data/web-portal;
index index.html index.htm;
try_files $uri $uri/ index.html;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Max-Age' '1000' always;
add_header 'Access-Control-Allow-Methods' "POST, GET, OPTIONS, DELETE, PUT" always;
add_header 'Access-Control-Allow-Headers' "x-requested-with, Content-Type, origin, authorization, accept, client-security-token" always;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
nginx核心配置参数
worker_processs:配置工作进程,建议设置与服务器核一致
worker_cpu_affinity:这个经常被用作优化项目,将进程与CPU绑定,提高cpu cache的命中率,减少内存访问损耗
sendfile:对于静态大文件,启用sendfile加速文件读取
tcp_nopush:在linux socket上启用TCP_CORK选项,和sendfile一起用,加速大文件读取
下面是关于超时的一些配置:
client_header_timeout:客户端在指定时间内把请求header传输完成,用于抵挡慢速攻击,建议5s内。
client_body_timeout:nginx2次获取请求体的超时时间,建议设置5s或者以下值
keepalive_timeout:定义保活时间,建议65s
proxy_connect_timeout:nginx连接后端服务器的超时时间,5s或以下
proxy_send_timeout:nginx连续两次向后端服务器发送请求的超时时间,设置5s及以下
proxy_read_timeout:nginx连续读取后端服务器返回的超时时间,设置5s及以下
具体超时时间配置根据具体的业务场景进行调整
nginx负载均衡算法
轮询:也是用到最多的,不同的后端服务器按照请求轮询访问
最小连接数:下一个请求被转发到当前活动连接数最小的服务器
IP哈希:基于客户端IP来哈希
nginx日志切割
在nginx运行一段时间之后,access或者error日志会变得很大
不利于问题的排查,可以在服务器上加定时任务,每天进行日志切割
脚本可参考如下:
#crontab -e 可以添加定时任务
55 23 * * * bin/bash data/nginxlog/nginxlog.sh
#vim data/nginxlog/nginxlog.sh
#!/bin/bash
LOG_PATH="/data460/lr_server/nginxlog"
RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d+%H:%M)
PID=/data460/lr_server/nginxlog/nginx.pid
mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${RECORD_TIME}.log
mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${RECORD_TIME}.log
kill -USR1 `cat $PID`
♦️
监控
监控
在nginx编译的时候,有添加status的模块,可以用来做nginx 的监控
可以添加如下location来监控
location ng_status
{
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
Active connections: 活跃的连接数
server accepts handled requests 总共处理了311829个连接, 311829次握手创建, 1731个请求
Reading: 读取客户端的连接数
Writing: 响应数据到客户端的数量
Waiting: 开启keep-alive的情况下,这个值等于 前面两者的和
♦️
常见错误
排错 常见错误码
1.400 bad request
常见原因:请求的header过大
解决办法:调整如下:
client_header_buffer_size 16K;
large_client_header_buffers 4 64k;
2.413 Request Entity Too Large
原因:上传文件过大
调整配置
client_max_body_size 10m;
3.499 Client Closed Request
原因:客户端未等到服务端响应返回前就关闭了客户端的描述符
解决办法:调整后端口服务器器处理时间
4.502 Bad gateway 503 Service Unavailable
原因:后端服务器响应无法处理
解决:大部分情况为后端服务器异常
5.504 Gateway Timeout
原因:后端服务器处理时间超过了超时时间配置
解决:调整proxy_read/send_timeout配置
♦️
准备工作
👆点击查看更多内容👆
推荐阅读
elk8.0部署文档(rsyslog+kafka+logstash+elasticsearch+kibana))
ELK告警插件-elastalert2 实践,支持elk8.0版本,企微机器人告警实现
记得星标记一下,下次更容易找到我