Nginx性能优化必备参数与说明

优化方向

  • CPU 亲和配置
  • 网络模型优化配置
  • 连接数优化配置
  • TCP连接优化配置
  • 日志优化配置
  • gzip压缩配置
# nginx运行时使用www用户,默认是nginx
user www;
# 针对cpu核树自动配置。nginx默认是没有开启利用多核cpu的配置的。需要通过增加worker_cpu_affinity配置参数来充分利用多核cpu
worker_processes  auto;
# nginx cpu自动亲和配置
worker_cpu_affinity auto;

# nginx错误日志的目录和级别设置  debug, info, notice, warn, error, crit  默认为 crit
error_log /var/log/nginx/error.log warn;
pid /run/nginx.pid;

# 能打开的最多文件描述符,可与ulimit -n 保持一致,负荷较高建议2-3w以上
worker_rlimit_nofile 35535;

# 个性化模块配置
include /usr/share/nginx/modules/*.conf;

events {
	# 网络io模型,nginx采用epoll事件模型,处理效率高
	use epoll;
	# 限制每个进程可以处理多少个连接请求,最大(worker_rlimit_nofile/worker_processes) 整体可打开文件数据量除以worker线程数量
	worker_connections 10240;
}

http {
	include /etc/nginx/mime.types;
	# 如果Web程序没设置,Nginx也没对应文件的扩展名,就用Nginx 里默认的 default_type定义的处理方式,application/octet-stream是nginx默认类型
	default_type application/octet-stream;
	#  隐藏nginx版本信息,安全性考虑
	server_tokens off;
	# 统一使用utf-8字符集
	charset utf-8;
	# 日志格式
	log_format  main  '[time:$request_time s] $remote_addr - $remote_user [$time_local] "$request" '  
			  '$status $body_bytes_sent "$http_referer" '
			  '"$http_user_agent" "$http_x_forwarded_for"'
			  '$upstream_addr $upstream_response_time $request_time $upstream_status '
					  '"$http_range" "$sent_http_content_range"'
					  '"$gzip_ratio"'
					  '"$query_string"' 
	'"-http_refer:$http_referer"';
	# 在http层,所有server都会打印,可以移动到server层细分
	access_log  /var/log/nginx/access.log main;

	####以下内容为核心调整#####

	# 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的负载
	sendfile on;
	# 必须在 sendfile 开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量(将响应头和正文的开始部分一起发送,而不一个接一个的发送)。
	tcp_nopush on
	# 也是防止网络阻塞,不过要包涵在keepalive_timeout参数才有效
	tcp_nodelay on
	 # 优化http连接 客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接
	keepalive_timeout 65;
	
	# nginx日志缓存,降低日志IO。
	# max;文件描述符 
	# inactive:日志文件在缓存中多长时间未使用就取消
	# min_uses: 在存活时间内日志被写入几次才会记录到缓存
	open_log_file_cache max=10240 inactive=60s valid=1m min_uses=2;
	
	##压缩配置
	gzip on;
	gizp_disable "MSIE [1-6]\."
	gzip_min_length 2k;
	gzip_buffers 4 16k;
	gzip_comp_level 3;
	gzip_vary on;
	gzip_types text/plain application/x-javascript application/javascript application/css  text/css application/xml;
# Server配置
	include /etc/nginx/conf.d/*.conf;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值