NGINX优化之路

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NGINX优化之路相关的知识,希望对你有一定的参考价值。

一、为了安全,隐藏nginx系统版本号,修改软件名称

[[email protected] ~]# vim  /home/tools/nginx-1.6.3/src/core/nginx.h 
 
  1 
  2 /*
  3  * Copyright (C) Igor Sysoev
  4  * Copyright (C) Nginx, Inc.
  5  */
  6 
  7 
  8 #ifndef _NGINX_H_INCLUDED_
  9 #define _NGINX_H_INCLUDED_
 10 
 11 
 12 #define nginx_version      1006003
#修改版本号
 13 #define NGINX_VERSION      "1.6.3"
#修改软件名
 14 #define NGINX_VER          "nginx/" NGINX_VERSION
#将nginx修改为想要修改的软件名称
 16 #define NGINX_VAR          "NGINX"
 17 #define NGX_OLDPID_EXT     ".oldbin"
 18 
 19 
 20 #endif /* _NGINX_H_INCLUDED_ */

二、更改系统默认用户,直接修改用户,nobody 或者编译安装时:直接指定用户参数:

指定用户、组
./configure --prefix=/usr/local/nginx  --user=www --group=www
[[email protected] ~]# grep "user" /usr/local/nginx/conf/nginx.conf.default 
#user  nobody;
    #log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
    #                  ‘"$http_user_agent" "$http_x_forwarded_for"‘;
[[email protected] ~]#

三、调整Nginx进程参数时,需要做性能测试,以及监控,当用户访问量、并发量等问题。

/usr/local/nginx/conf/nginx.conf
worker_processes  1;
使用命令查看
[[email protected] ~]#  grep "processor" /proc/cpuinfo |wc -l
1
[[email protected] ~]# 
根据服务器的配置进行计算

四、优化绑定不同的Nginx进程到不同CPU上,这个需要根据业务类型进行操作,此处不赘述,需要看业务来实施。


五、调整Nginx单个进程允许的客户端最大连接数


worker_connections  1024;
worker_connections的值要根据具体服务器性能和程序的内存使用量来指定(一个进程启动使用的内存根据程序确定)
events {
    worker_connections  20480;
}

六、Nginx最大连接数
Nginx worker进程的最大打开文件数,这个控制连接数的参数为worker_rlimit_nofile。
worker_rlimit_nofile 65535

七、开启高效文件传输

(1)设置参数:sendfile on;
   sendfile参数用于开启文件的高效传输模式,同时将tcp_nopush和tcp_nodelay两个指定设为on,可防止网络及磁盘I/O阻塞,提升Nginx工作效率。
官方说明:
syntax:    sendfile on|off  #参数语法
default:    sendfile off    #参数默认大小
context:    http,server,location,if in location #可放置的标签段
(2)设置参数:tcp_nopush on;
Syntax: tcp_nopush on | off; #参数语法
Default: tcp_nopush off; #参数默认大小
Context: http, server, location #可以放置标签段
参数作用:激活或禁用Linux上的TCP_CORK socker选项,此选项仅仅开启sendfile时才生效,激活这个tcp_nopush参数可以运行把http response header和文件的开始放在一个文件里发布,减少网络报文段的数量。
(3)设置参数:tcp_nodelay on;
用于激活tcp_nodelay功能,提高I/O性能
Syntax: tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: http, server, location
参数作用:默认情况下数据发送时,内核并不会马上发送,可能会等待更多的字节组成一个数据包,这样可以提高I/O性能,但是,在每次只发送很少字节的业务场景,使用tcp_nodelay功能,等待时间会比较长。

八、Nginx超时设置:
4.Nginx连接超时的参数设置

(1)设置参数:keeplived_timeout 60;
用于设置客户端连接保持会话的超时时间为60秒。超过这个时间,服务器会关闭该连接,此数值为参考值。
Syntax: keepalive_timeout timeout [header_timeout]; #参数语法
Default: keepalive_timeout 75s; #参数默认大小
Context: http, server, location #可以放置的标签段
参数作用:keep-alive可以使客户端到服务端已经建立的连接一直工作不退出,当服务器有持续请求时,keep-alive会使用正在建立的连接提供服务,从而避免服务器重新建立新连接处理请求。
(2)设置参数:client_header_timeout 15;
用于设置读取客户端请求头数据的超时时间,此处的数值15单位是秒。
Syntax: client_header_timeout time;
Default: client_header_timeout 60s;
Context: http, server
参数作用:设置读取客户端请求头数据的超时时间。如果超过这个时间,客户端还没有发送完整的header数据,服务端将数据返回“Request time out (408)”错误。
(3)设置参数:client_body_timeout 15;
用于设置读取客户端请求主体的超时时间,默认值是60
Syntax: client_body_timeout time;
Default: client_body_timeout 60s;
Context: http, server, location
参数作用:设置读取客户端请求主体的超时时间。这个超时仅仅为两次成功的读取操作之间的一个超时,非请求整个主体数据的超时时间,如果在这个超时时间内,客户端没有发送任何数据,Nginx将返回“Request time out(408)”错误,默认值是60.

(4)设置参数:send_timeout 25;
用户指定响应客户端的超时时间。这个超时时间仅限于两个链接活动之间的事件,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接,默认值为60s,可以改为参考值25s
Syntax: send_timeout time;
Default: send_timeout 60s;
Context: http, server, location
参数作用:设置服务器端传送http响应信息到客户端的超时时间,这个超时时间仅仅为两次成功握手后的一个超时,非请求整个响应数据的超时时间,如在这个超时时间内,客户端没有收到任何数据,连接将被关闭。

(九)php优化配置

化设置:

  1. http{}里面

  2. fastcgi_connect_timeout 240;

  3. fastcgi_send_timeout 240;

  4. fastcgi_read_timeout 240;

  5. fastcgi_buffer_size 64k;

  6. fastcgi_buffers 4 64k;

  7. fastcgi_busy_buffers_size 128k;

  8. fastcgi_temp_file_write_size 128k;

  9. #fastcgi_temp_path /data/ngx_fcgi_tmp;  需要有路径

  10. fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g;

  11. PHP缓存 可以配置在server标签和http标签

  12. fastcgi_cache ngx_fcgi_cache;

  13. fastcgi_cache_valid 200 302 1h;

  14. fastcgi_cache_valid 301 1d;

  15. fastcgi_cache_valid any 1m;

  16. fastcgi_cache_min_uses 1;

  17. fastcgi_cache_use_stale error timeout invalid_header http_500;

  18. fastcgi_cache_key http://$host$request_uri;


以上是关于NGINX优化之路的主要内容,如果未能解决你的问题,请参考以下文章

linux学习:Nginx--常见功能配置片段与优化-06

前端构建效率优化之路

架构师的成长之路初片~nginx优化篇

持续交付优化之路产品剖析

优化 C# 代码片段、ObservableCollection 和 AddRange

Nginx——Nginx启动报错Job for nginx.service failed because the control process exited with error code(代码片段