Nginx 调优经验记录

1.2017年连续爆出5.x版本xshell安全问题和intel的cpu设计漏洞 ,此时我就注意到尽量少暴露自己线上使用的工具以及版本。例如:mysql版本,以及缓存层策略,服务器版本等,以下为 隐藏 nginx的版本号方法:

nginx配置中:

http段中,加入配置

server_token off;

或者修改源码中,字符串定义。编辑src/core/nginx.h, 

#define NGINX_VER "nginx/" NGINX_VERSION

修改后需要重新编译。

2.优化nginx进程数,一般将nginx进程数设置为cpu核数,有突来的并发时可设置为核数2倍,

work_processes 4; //main段

此外还可以利用 worker_cpu_affinity 绑定进程到指定cpu,充分利用cpu。

3.开发web应用时经常会遇到文件传输的场景,nginx有开启高效文件传输模式。

sendfile on|off;

配合 tcp_nopush|tcp_nodelay 防止网络I/O阻塞,提升效率

tcp_nopush on;

tcp_nodelay on; //激活延时,提高I/O功能

4.连接超时设置

keeplive_timeout 90; //客户端连接 保持会话

client_header_timeout //设置读取客户端请求头数据的超时时间

send_timeout 40; //指定相应客户端的超时时间

5.nginx gzip压缩配置

gzip            on;
gzip_min_length 1000; //允许压缩的页面最小字节数
gzip_proxied    expired no-cache no-store private auth;
gzip_types      text/plain application/xml;  //压缩的类型,

http://nginx.org/en/docs/http/ngx_http_gzip_module.html

6.nginx expires设置

设置一个缓存过期时间。

expires 1y;

原文地址:https://www.cnblogs.com/guixiaoming/p/8504704.html