微服务架构 Nginx优化
Nginx介绍
- Nginx默认配置路径 /conf/nginx.conf文件,可在启动时 通过-c 指定安装路径
- Nginx启动原理
1.启动nginx会启动一个Master进程,这个进程不处理任何客户端请求,主要用来产生worker进程,一个worker进程用来处理一个request
2.单独worker之间互不影响,worker_connections可以配置,指每个进程里面可以启动多少个worker
优化思路
- 默认Nginx是经过优化的,而我们针对Nginx优化主要集中在配置调整上
- 如果优化后效果不理想,增加硬件,如机器,负载均衡
常见配置文件如下
- nginx.conf 应用程序基本配置 /核心配置
- mime.types MIME关联扩展文件
- fastcgi.conf 与fastcgi相关配置
- proxy.conf 与proxy相关配置
- sites.conf 配置Nginx提供的网站,包括虚拟主机
nginx.conf配置讲解,常用核心模块指令可以参考如下
nginx.conf配置
error_log logs/error.log crit
user root;
worker_processes 2;
worker_rlimit_nofile 15360;
events {
use epoll;
worker_connections 10240/20480;
}
//web反向代理
http {
include mime.types;//引入其他文件
include proxy.conf;//引入代理文件
default_type application/octet-stream;
access_log logs/access.log main
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
keys_zone=TEST:10m
inactive=5m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;
fastcgi_busy_buffers_size 8k;
fastcgi_temp_file_write_size 8k;
fastcgi_cache TEST;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
//负载均衡配置节点
upstream zachary.cn{
#server 127.0.0.1:9080 weight=5
server 127.0.0.1 weight=5
#server 127.0.0.1:1010
}
server{
listen 80;
server_name zachary.cn;
index index.php index.htm;
root /www/html/; 静态资源路径
location ~ .*/.(gif|jpg|jpeg|png|bmp|swf|ico)${
if(-f $request_filename){
#expires 30d;
break;
}
}
location ~ .*/.(html|htm|js|css)${
#expires 1d;
}
location /{
proxy_set_header Host $host;
proxy_set_header X-Porwarded-Por $remote_addr //获取真实ip
proxy_pass http://zachary.cn
proxy_next_upstream [error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off] //默认 error timeout
}
location ~ .*/.(php|php5)?${
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
log_format access '$remote_addr — $remote_user [$time_local] "$request"'
'$status $body_bytes_sent “$http_referer” '
'”$http_user_agent” $http_x_forwarded_for';
access_log /www/log/access.log access;
}
error_page 500 502 503 504 /501.html;
location = /501.html{
internal;
root errors/html
}
}
Nginx.conf 讲解
处理过程:
- 请求进来worker会监控80端口,过滤静态资源后,请求反向代理到后台服务器(如tomcat)后台服务器可以负载均衡配置多台,可通过权重如weight、iphash 指定服务器接收请求策略。
- 静态资源不代理到后台服务器 直接可以到专有静态资源服务器、分布式共享存储获取 如NFS\MFS
- nginx单点的HA高可用:proxy_next_upstream可配置(如果指向服务器 此时服务器1处于不正常,它会重新把请求指向下台服务器依次类推
详细讲解
- 并发总数是 worker_processes *worker_connections ,反向代理下要除以4
- events里面的I/O模型,Linux推荐使用epoll模型,FreeBSD推荐采用kqueue
- worker_processes建议配置服务器CPU总核数,或者2倍,性能会更好
- worker_rlimit_nofile打开最大文件数,配置Linux内核下文件打开数一致,可以通过ulimit -n查看,默认1024,可以修改/etc/security/limits.conf 增加到65535
- worker_connections 单个进程允许的客户端最大连接数
Nginx配置二级域名
server_name www.zachary.cn *.zachary.cn;
判断是二级域名,可自定义变量设置
if($host ~* (\b(?!www\b)\w+)\.\w+\.com){
set $zach1 $1;
set $zachurl $request_uri;
}
rewrite可以使用自定义变量
Nginx 配置HTTP主要有以下几块
http{ //协议级别
server{ //服务器级别
location/ {//请求级别
}
}
}
location区段通过指定模式来和客户端请求URL进行匹配,基本语法
location [=|~|~*|^~|@] pattern{.....}
server{
server_name zachary.cn
location /cc56{
......
}
}
区分大小写 如下匹配
http://zachary.cn/cc56
http://zachary.cn/cc56/
http://zachary.cn/cc56cincnb2
http://zachary.cn/cc56/?cccc=1&bbbb=2
server{
server_name zachary.cn
location ~* ^/cc56{
......
}
}
不区分大小写 如下匹配
http://zachary.cn/cc56
http://zachary.cn/CC56
http://zachary.cn/cc56/?cccc=1&bbbb=2
HTTP反向代理
常用proxy_pass, 如 location /{proxy_pass http://zachary.cn}
Nginx负载均衡
流行负载均衡 如(DNS轮询、硬件F5、软件LVS、Nginx)
Nginx通过upstream
upstream zachary.cn{
ip_hash
#server 127.0.0.1:9080 weight=5
server 127.0.0.1 weight=5
#server 127.0.0.1:1010
}
默认weight=1,不推荐使用ip_hash,客户端ip会变化,如动态ip、翻墙、代理
Eewrite模块,用来指定URL重定向,这个机制可以处理恶意访问url
优化过程
- 优化并发总数 worker_processes *worker_connections,worker_processes参考CPU核数,worker_connections每个进程允许连接数,1GB内存可以打开10W文件数
- keepalive_timeout 建议设置60-70左右
- client_header_buffer_size,建议设置请求缓存
- gzip_comp_level 开启Gzip压缩,建议设置3-5,高了耗损CPU
- access日志优化,如果使用其它统计工具,可关闭,减少磁盘写入,提高I/O效率
- sendfile 指定是否调用sendfile函数来输出文件,建议设置on
- buffersize优化,如果太小会导致nginx使用临时文件存储response,client_body_buffer_size 处理客户端请求体buffer大小,处理post请求数据、上传文件等
- tcp_nopush 建议开启提高吞吐量
- tcp_nodelay 不缓存数据,一段一段发送,建议开启
- client_header_timeout 设置请求头超时时间,建议10-20s
- client_body_timeout 设置请求体超时时间,建议10-20s
- reset_timeout_connection 关闭不响应客户端连接,建议开启
- send_timeout 客户端响应超时时间 默认60s,建议设置10s
总结
Nginx根据各系统的结构进行特定优化,优化顺序 (先优化单台配置、单台到达瓶颈后可通过硬件引流到多台机器)故达到高可用状态,参数调优可参考官方文档仔细斟酌测试
作者简介:张程 技术研究
更多文章请关注微信公众号:zachary分解狮 (frankly0423)