nginx工作进程

nginx工作进程一般设置为CPU核心数或CPU核心数x2。
设置: worker_processes 4;

cpu参数worker_cpu_affinity

nginx默认是没有开启利用多核cpu的配置的。需要通过增加worker_cpu_affinity配置参数来充分利用多核cpu,cpu是任务处理,当计算最费时的资源的时候,cpu核使用上的越多,性能就越好。

如双核配置:

worker_processes     2;
worker_cpu_affinity 01 10;

01表示启用第一个cpu内核, 02表示启用第二个cpu内核。

nginx最大打开文件数

worker_rlimit_nofile 65535;

nginx事件处理模型

events {
  use epoll;
  worker_connections 65535;
  multi_accept on;
}
  • 使用epoll事件处理模型,处理效率高。
  • worker_connections设置每个工作进程的最大连接数,实际最大连接数是worker_process 乘以 woker_connections。
  • multi_accept设置为on后,多个worker按串行方式来处理连接,也就是一个连接只有一个worker被唤醒,其他的处于休眠状态,在服务器吞吐量不大时可以降低负载。

开启高效传输

http {
  include mime.types;
  default_type application/octet-stream;
  ……

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  ……
}
  • sendfile on:开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
  • tcp_nopush on:必须在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量(将响应头和正文的开始部分一起发送,而不一个接一个的发送。)
  • 小包组成成大包,提高带宽利用率, 也就是著名的nagle算法。

超时时间设置

http {
 .....
    keepalive_timeout 65;
    open_file_cache max=65535 inactive=20s;
    open_file_cache_valid 20s;
    open_file_cache_min_uses 1;
    client_header_timeout 10;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 15;
    server_tokens off;
    client_max_body_size 100m;
 .....
}
  • keepalive_timeout: 客户端保持会话的连接时间,如果超过这个时间,会与客户端断开连接。
  • open_file_cache: 将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
  • open_file_cache_valid: 是指多长时间检查一次缓存的有效信息。
  • open_file_cache_min_uses: 表示在open_file_cache配置中inactive时间内文件最小使用次数,超过该次数,那么文件描述符一直在缓存中,否则它将被移除。
  • client_header_timeout: 设置请求头超时时间。
  • client_body_timeout: 设置请求体超时时间。
  • reset_timeout_connection: 关闭不使用的客户端连接,节省内存。
  • send_timeout: 在这个时间内,客户端没有活动信息,那么将断开连接。
  • server_tokens: 隐藏错误页面nginx版本信息。
  • client_max_body_size: 上传文件大小限制。

开启zip压缩

http {
  .........

    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_comp_level 5;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  ........

}
  • gzip on: 开启zip压缩
  • gzip_vary on: 启用应答头"Vary: Accept-Encoding"
  • gzip_disable: (IE5.5和IE6 SP1使用msie6参数来禁止gzip压缩 )指定哪些不需要gzip压缩的浏览器(将和User-Agents进行匹配),依赖于PCRE库
    • gzip_buffers:设置gzip申请内存的大小,其作用是按块大小的倍数申请内存空间
    • gzip_comp_level:设置gzip压缩等级,等级越底压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大
    • gzip_types:设置需要压缩的MIME类型,非设置值不进行压缩

fastcgi调优

http {
      fastcgi_connect_timeout 600;
      fastcgi_send_timeout 600;
      fastcgi_read_timeout 600;
      fastcgi_buffer_size 64k;
      fastcgi_buffers 4 64k;
      fastcgi_busy_buffers_size 128k;
      fastcgi_temp_file_write_size 128k;
      fastcgi_temp_path /var/cache/temp_fastcgi_cache;
      fastcgi_intercept_errors on;
}
  • fastcgi_connect_timeout 600 :指定连接到后端FastCGI的超时时间。

  • fastcgi_send_timeout 600 :向FastCGI传送请求的超时时间。

  • fastcgi_read_timeout 600 :指定接收FastCGI应答的超时时间。

  • fastcgi_buffer_size 64k :指定读取FastCGI应答第一部分需要用多大的缓冲区,默认的缓冲区大小为。fastcgi_buffers指令中的每块大小,可以将这个值设置更小。

  • fastcgi_buffers 4 64k :指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求,如果一个php脚本所产生的页面大小为256KB,那么会分配4个64KB的缓冲区来缓存,如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp_path指定的路径中,但是这并不是好方法,因为内存中的数据处理速度要快于磁盘。一般这个值应该为站点中php脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设置为“8 32K”、“4 64k”等。

  • fastcgi_busy_buffers_size 128k :建议设置为fastcgi_buffers的两倍,繁忙时候的buffer。

  • fastcgi_temp_file_write_size 128k :在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍,该数值设置小时若负载上来时可能报502BadGateway。

  • fastcgi_temp_path :缓存临时目录。

  • fastcgi_intercept_errors on :这个指令指定是否传递4xx和5xx错误信息到客户端,或者允许nginx使用error_page处理错误信息。注:静态文件不存在会返回404页面,但是php页面则返回空白页!

缓存

fastcgi_cache

fastcgi_cache的作用是缓存fastcgi生成的内容,很多情况是php生成的动态的内容,减少了nginx与php的通信次数,更减轻了php和数据库的压力。

    fastcgi_cache_path /var/cache/fastcgi_cache levels=1:2 keys_zone=fastcgi_cache:128minactive=1d max_size=10g;
    fastcgi_cache fastcgi_cahce;
    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;

fastcgi_cache_path: fastcgi_cache缓存目录,可以设置目录层级,比如1:2会生成16*256个子目录,cache_fastcgi是这个缓存空间的名字,cache是用多少内存(这样热门的内容nginx直接放内存,提高访问速度),inactive表示默认失效时间,如果缓存数据在失效时间内没有被访问,将被删除,max_size表示最多用多少硬盘空间。

fastcgi_cache cache_fastcgi :#表示开启FastCGI缓存并为其指定一个名称。开启缓存非常有用,可以有效降低CPU的负载,并且防止502的错误放生。cache_fastcgi为proxy_cache_path指令创建的缓存区名称。

fastcgi_cache_valid 200 302 1h :#用来指定应答代码的缓存时间,实例中的值表示将200和302应答缓存一小时,要和fastcgi_cache配合使用。

fastcgi_cache_valid 301 1d :将301应答缓存一天。

fastcgi_cache_valid any 1m :将其他应答缓存为1分钟。

fastcgi_cache_min_uses 1 :该指令用于设置经过多少次请求的相同URL将被缓存。

fastcgi_cache_key http://request_uri :该指令用来设置web缓存的Key值,nginx根据Key值md5哈希存储.一般根据request_uri(请求的路径)等变量组合成proxy_cache_key 。

proxy_cache

proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态的,缓存减少了nginx与后端通信的次数,节省了传输时间和后端宽带。

 http {
    .....
    proxy_temp_path /var/cache/proxy_cache/temp;
    proxy_cache_path /var/cache/proxy_cache/cache levels=1:2 keys_zone=web_zone:10m inactive=300s max_size=5g;
    .....
    server {
            location / {
            .......
            proxy_cache web_zone;  # 使用缓存区 缓存键空间名要和proxy_cache_path 对应上
            proxy_cache_valid 200 304 12h;  #指定状态码的缓存时间
            proxy_cache_valid 301 302 1m; # 缓存301,302一个月。
            proxy_cache_key $host$uri$is_args$args; #指定键key的格式
            expires 1d;        #过期时间
            .......
        }
    }
}
  • proxy_temp_path缓存临时目录,
  • proxy_cache_path 真正缓存存放目录, 与proxy_temp_path必须放在同一配置区。
  • proxy_connect_timeout: 后端服务器连接的超时时间_发起握手等候响应超时时间。

完整nginx示例

#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    use epoll;
    worker_connections 65535;
    multi_accept on;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    keepalive_timeout 65;
    open_file_cache max=65535 inactive=20s;
    open_file_cache_valid 20s;
    open_file_cache_min_uses 1;
    client_header_timeout 10;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 10;
    server_tokens off;
    client_max_body_size 100m;

    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;


    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    fastcgi_connect_timeout 600;
    fastcgi_send_timeout 600;
    fastcgi_read_timeout 600;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    fastcgi_temp_path /var/cache/temp_fastcgi_cache;
    fastcgi_intercept_errors on;


    proxy_temp_path /var/cache/proxy_cache/temp;
    proxy_cache_path /var/cache/proxy_cache/cache levels=1:2 keys_zone=web_zone:10m inactive=300s max_size=5g;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }