• nginx优化


    nginx的模型

    nginx采用epoll模型,这种模型是在linux内核2.6之后采用的,nginx采用epoll模型,异步非阻塞,而Apache采用select模型

    select特点:select选择句柄的时候,是遍历所有句柄,也就是说句柄有事件响应时,select需要遍历所有句柄才能获取到哪些句柄事件通知,因此效率非常低

    epoll特点:epoll对于句柄事件的选择不是遍历的,是事件响应的,就是句柄上事件来就马上选择出来,不需要遍历整个句柄链表,因此效率非常高

    nginx工作模式

    master-worker模式

    该模式下nginx启动成功后,会有一个master进程和至少一个worker进程。master进程负责处理系统信号,加载配置,管理worker进程。worker进程负责处理具体的业务逻辑,正真提供服务的是worker进程

    优点:

    稳定性高,只要还有worker进程存活,就能够提供服务,并且一个worker进程挂掉master进程会立即重启一个新的worker进程,保证worker进程数量不变,降低服务中断的概率

    配合linux的cpu亲和性配置,可以充分利用多核cpu优势,提升性能

    处理信号/配置重新加载/升级时可以做到尽可能少或者不中断服务

    单进程模式

    单进程模式下,nginx启动后只有一个进程,nginx的所有工作都由这个进程负责。

    优点:由于只有一个进程,因此可以很方便地利用gdp等工具进行调试

    缺点:单进程模式不支持nginx的平滑升级功能,任何的信号处理都可能造成服务中断,并且由于是单进程,进程挂掉后,在没有外部监控的情况下,无法重启服务

    nginx配置优化

    worker_processes

    表示启动几个工作进程,建议和本机CPU核数保持一致

    worker_rlimit_nofile 65535

    表示nginx进程打开的最多文件描述符数目,最好与ulimit -n的值保持一致

    nginx处理事件模型

    events {

        use epoll;

        worker_connections 65535;

        multi_accept on;}

      use epoll

      使用epoll模式的事件驱动模型,该模型为Linux系统下最优方式

      multi_accept on

      使每个进程尽可能处理多个请求

      worker_connections

      单个后台worker_connections进程的最大并发连接数

    tcp_nodelay on

    提高数据的实时响应性

    tcp_nopush on

    防止网络阻塞

    nginx http服务器优化

    keepalive-timeout

    定义长连接的超时时间,建议30s

    client_max_body_size

    允许客户端请求的最大单文件字节数、

    large_client_header_buffers 4 16k

    指定客户端请求中较大的消息头的缓存最大数量和大小,4为个数;16k为最大缓存4个4kb

    client_header_buffer_size

    客户端请求头部的缓冲区大小,建议设为4k

    Client_header_timeout

    设置客户端请求读取超过事件,如果超过这个时间,客户端还没有发送任何数据nginx返回:requite time out

    Client_body_timeout

    用于设置客户端请求主题读取超过时间,默认值为60

    send_timeout

    指定响应客户端的超时时间,这个超时仅限于两个连接活动之前的时间,如果超过这个时间客户端没有任何获得,nginx将会关闭连接

    open_file_cache max=102400 inactive=20s

    这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。

    open_file_cache_valid 30s

    这个是指多长时间检查一次缓存的有效信息

    open_file_cache_min_uses 1

    open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除

    server_names_hash_bucket_size 128

    服务器名的hash库大小

    net.ipv4.tcp_max_tw_buckets = 6000

    timewait的数量,默认是1024

    net.ipv4.ip_local_port_range = 1024 65000

    允许系统打开端口的范围

    net.ipv4.tcp_tw_recycle = 1

    开启timew_wait sockes的快速回收,默认为0表示关闭

    net.ipv4.tcp_tw_reuse = 1

    开启重用,允许timewait sockes重新用于新的tcp连接,默认为0表示关闭

    net.ipv4.tcp_syncookies = 1

    当出现SYN等待队列溢出时,启用cookies来处理,课防范少量SYN攻击,默认为空表示关闭

    net.core.netdev_max_backlog = 262144

    每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目

    net.ipv4.tcp_fin_timeout = 1

    如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间

    net.ipv4.tcp_keepalive_time = 30

    当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时。

    net.ipv4.tcp_syn_retries = 1

    在内核放弃建立连接之前发送SYN包的数量。

    一个完整的内核优化配置

    net.ipv4.ip_forward = 0
    net.ipv4.conf.default.rp_filter = 1
    net.ipv4.conf.default.accept_source_route = 0
    kernel.sysrq = 0
    kernel.core_uses_pid = 1
    net.ipv4.tcp_syncookies = 1
    kernel.msgmnb = 65536
    kernel.msgmax = 65536
    kernel.shmmax = 68719476736
    kernel.shmall = 4294967296
    net.ipv4.tcp_max_tw_buckets = 6000
    net.ipv4.tcp_sack = 1
    net.ipv4.tcp_window_scaling = 1
    net.ipv4.tcp_rmem = 4096 87380 4194304
    net.ipv4.tcp_wmem = 4096 16384 4194304
    net.core.wmem_default = 8388608
    net.core.rmem_default = 8388608
    net.core.rmem_max = 16777216
    net.core.wmem_max = 16777216
    net.core.netdev_max_backlog = 262144
    net.core.somaxconn = 262144
    net.ipv4.tcp_max_orphans = 3276800
    net.ipv4.tcp_max_syn_backlog = 262144
    net.ipv4.tcp_timestamps = 0
    net.ipv4.tcp_synack_retries = 1
    net.ipv4.tcp_syn_retries = 1
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_mem = 94500000 915000000 927000000
    net.ipv4.tcp_fin_timeout = 1
    net.ipv4.tcp_keepalive_time = 30
    net.ipv4.ip_local_port_range = 1024 65000
    View Code

    一个简单的nginx优化配置文件

    user www www;
    worker_processes 8;
    worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000;
    error_log /www/log/nginx_error.log crit;
    pid /usr/local/nginx/nginx.pid;
    worker_rlimit_nofile 204800;
    
    events
    {undefined
    use epoll;
    worker_connections 204800;
    }
    
    http
    {undefined
    include mime.types;
    default_type application/octet-stream;
    
    charset utf-8;
    
    server_names_hash_bucket_size 128;
    client_header_buffer_size 2k;
    large_client_header_buffers 4 4k;
    client_max_body_size 8m;
    
    sendfile on;
    tcp_nopush on;
    
    keepalive_timeout 60;
    
    fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
    keys_zone=TEST:10m
    inactive=5m;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 16k;
    fastcgi_buffers 16 16k;
    fastcgi_busy_buffers_size 16k;
    fastcgi_temp_file_write_size 16k;
    fastcgi_cache TEST;
    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;
    
    open_file_cache max=204800 inactive=20s;
    open_file_cache_min_uses 1;
    open_file_cache_valid 30s;
    
    
    
    tcp_nodelay on;
    
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    
    
    server
    {undefined
    listen 8080;
    server_name ad.test.com;
    index index.php index.htm;
    root /www/html/;
    
    location /status
    {undefined
    stub_status on;
    }
    
    location ~ .*\.(php|php5)?$
    {undefined
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fcgi.conf;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
    {undefined
    expires 30d;
    }
    
    log_format access '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" $http_x_forwarded_for';
    access_log /www/log/access.log access;
    }
    }

    nginx配置优化-httpgzip:

    gzip on; //开启gzip功能
      gzip_min_length 1024; //设置请求资源超过该数值才进行压缩,单位字节
      gzip_buffers 16 8k; //设置压缩使用的buffer大小,第一个数字为数量,第二个为每个buffer的大小
      gzip_comp_level 6; //设置压缩级别,范围1-9,9压缩级别最高,也最耗费CPU资源
      gzip_types text/plain application/x-javascript text/css application/xml image/jpeg image/gif image/png; //指定哪些类型的文件需要压缩
      gzip_disable "MSIE 6\."; //IE6浏览器不启用压缩
  • 相关阅读:
    Frame内容页向Frame页传值的问题。
    Silverlight 要求使用更新版本
    Siverlight 中RichTextBox 中注入控件无法使用的问题
    PHP_判断是否为数字
    CentOS中vsftp安装与配置
    全面认识F5负载均衡
    如何让php自动进行二级域名泛解析
    【Thinkphp教程】 如何实现URL伪静态
    LVS+heartbeat+ldirectord高可用负载均衡集群解决方案
    ThinkPHP小结
  • 原文地址:https://www.cnblogs.com/junhao86/p/16006445.html
Copyright © 2020-2023  润新知