NGINX优化之路(一)

1、为了安全,隐藏Nginx系统版本号,修改软件名称node

[root@blog ~]# 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_ */

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

指定用户、组
./configure --prefix=/usr/local/nginx  --user=www --group=www
[root@blog ~]# 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"';
[root@blog ~]#

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

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

4、优化绑定不一样的Nginx进程到不一样CPU上,这个须要根据业务类型进行操做,此处不赘述,须要看业务来实施。缓存


5、调整Nginx单个进程容许的客户端最大链接数安全


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

6、Nginx最大链接数
Nginx worker进程的最大打开文件数,这个控制链接数的参数为worker_rlimit_nofile。
worker_rlimit_nofile 65535服务器

7、开启高效文件传输网络

(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功能,等待时间会比较长。

8、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.tcp

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

(九)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;

相关文章
相关标签/搜索