zoukankan      html  css  js  c++  java
  • 5、Nginx 性能优化

    系统优化

    $ cat /etc/sysctl.conf
    net.ipv4.tcp_max_syn_backlog = 65536
    net.core.netdev_max_backlog =  36768
    net.core.somaxconn = 36768
     
    net.core.wmem_default = 8588608
    net.core.rmem_default = 8588608
    net.core.rmem_max = 16877216
    net.core.wmem_max = 16877216
     
    net.ipv4.tcp_synack_retries = 2
    net.ipv4.tcp_syn_retries = 2
     
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_tw_reuse = 1
     
    net.ipv4.tcp_mem = 94500000 915000000 927000000
    net.ipv4.tcp_max_orphans = 3376800
    net.ipv4.ip_local_port_range = 1024  65535
    
    $ sysctl -p
    $ cat /etc/security/limit.conf
    *  hard  nofile  65535
    *  soft  nofile  65535

    Nginx配置 优化

    #user  nobody;
    worker_processes  4;
    worker_cpu_affinity 0001 0010 0100 1000;
    worker_rlimit_nofile 65535;
    
    #pid        logs/nginx.pid;
    
    events {
            use epoll;
            worker_connections 65535;
            multi_accept on;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #access_log  logs/access.log  main;
        log_format  main  '$http_X_Real_IP $http_CLIENTIP $remote_addr $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" $request_time';
    
        sendfile        on;
        tcp_nopush     on;
    
        keepalive_timeout  60;
        keepalive_requests 10240;
        tcp_nodelay on;
        client_header_buffer_size 4k;
        open_file_cache max=102400 inactive=20s;
        open_file_cache_valid 30s;
        open_file_cache_min_uses 1;
        client_header_timeout 15;
        client_body_timeout 15;
        reset_timedout_connection on;
        send_timeout 15;
        server_tokens off;
        client_max_body_size 10m;
    
        gzip  off;
        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;
    
        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 /usr/local/nginx/fastcgi_temp;
    
        server {
    
            listen       80;
            server_name  localhost;
            access_log  /usr/local/logs/nginx/access.log  main;
            root        html;
            index       index.html index.htm index.php;
    
            #图片缓存时间        
            location ~* .(ico|jpe?g|gif|png|bmp|swf|flv)$ {
              expires 30d;
              #log_not_found off;
              access_log off;
            }
            #JS和CSS缓存时间
            location ~* .(js|css)$ {
              expires 7d;
              log_not_found off;
              access_log off;
            }
    
            error_page   500 502 503 504  /50x.html;
            location / {
                    try_files $uri $uri/ @rewrites;
            }
    
            location @rewrites {
                    rewrite ^ /index-development.php last;
            }
    
            location = /robots.txt {
                    access_log off;
                    log_not_found off;
             }
    
            location ~ .php$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
        }
        include conf.d/*;
    }
    • worker_processes
      nginx运行工作进程个数,一般设置cpu的核心或者核心数x2,如:worker_processes 4;
    • worker_cpu_affinity
      运行CPU亲和力,与worker_processes对应,如:worker_cpu_affinity 0001 0010 0100 1000;
    • worker_rlimit_nofile
      Nginx最多可以打开文件数,与ulimit -n保持一致,如:worker_rlimit_nofile 65535;
    • events
      事件处理模型。如:
    events {
      use epoll;
      worker_connections 65535;
      multi_accept on;
    }
    use epoll:nginx采用epoll事件模型,处理效率高
    work_connections:是单个worker进程允许客户端最大连接数,这个数值一般根据服务器性能和内存来制定,实际最大值就是worker进程数乘以work_connections,实际我们填入一个65535,足够了,这些都算并发值,一个网站的并发达到这么大的数量,也算一个大站了!
    multi_accept :告诉nginx收到一个新连接通知后接受尽可能多的连接,默认是on,设置为on后,多个worker按串行方式来处理连接,也就是一个连接只有一个worker被唤醒,其他的处于休眠状态,设置为off后,多个worker按并行方式来处理连接,也就是一个连接会唤醒所有的worker,直到连接分配完毕,没有取得连接的继续休眠。当你的服务器连接数不多时,开启这个参数会让负载有一定的降低,但是当服务器的吞吐量很大时,为了效率,可以关闭这个参数。


    • http
    高效传输模式,如:
    http {
    include mime.types;
    default_type application/octet-stream;
    ……
    sendfile on;
    tcp_nopush on;
    ……
     
    Include mime.types: 媒体类型,include 只是一个在当前文件中包含另一个文件内容的指令
    default_type:默认媒体类型,如: application/octet-stream;
    sendfile :开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
    注意:如果图片显示不正常把这个改成off
    tcp_nopush:必须在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量(将响应头和正文的开始部分一起发送,而不一个接一个的发送。)

    • 连接超时时间
    主要目的是保护服务器资源,CPU,内存,控制连接数,因为建立连接也是需要消耗资源的,如:
    keepalive_timeout 60;
    keepalive_requests 10240;
    tcp_nodelay on;
    client_header_buffer_size 4k;
    open_file_cache max=102400 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;
    client_header_timeout 15;
    client_body_timeout 15;
    reset_timedout_connection on;
    send_timeout 15;
    server_tokens off;
    client_max_body_size 10m;
     
    keepalived_timeout 60:客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接
    keepalive_requests 10240:参数限制了一个 HTTP 长连接最多可以处理完成的最大请求数, 默认是 100。当连接处理完成的请求数达到最大请求数后,将关闭连接。
    tcp_nodelay:也是防止网络阻塞,不过要包涵在keepalived参数才有效
    client_header_buffer_size 4k:客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过 1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。
    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 时间内一次没被使用,它将被移除。
    client_header_timeout:设置请求头的超时时间。我们也可以把这个设置低些,如果超过这个时间没有发送任何数据,nginx将返回request time out的错误
    client_body_timeout:设置请求体的超时时间。我们也可以把这个设置低些,超过这个时间没有发送任何数据,和上面一样的错误提示
    reset_timeout_connection:告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。
    send_timeout:响应客户端超时时间,这个超时时间仅限于两个活动之间的时间,如果超过这个时间,客户端没有任何活动,nginx关闭连接
    server_tokens:并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。
    client_max_body_size:上传文件大小限制
     
     
  • 相关阅读:
    P3746 [六省联考2017]组合数问题 矩阵乘法
    P3322 [SDOI2015]排序 暴搜
    P2877 [USACO07JAN]Cow School G 斜率优化+分数规划
    P3283 [SCOI2013]火柴棍数字 DP
    AT2005 [AGC003E] Sequential operations on Sequence 单调栈+二分+差分
    CF568C New Language 2-SAT
    P4410 [HNOI2009]无归岛 仙人掌图
    CF505D Mr. Kitayuta's Technology 并查集 拓扑排序
    Algorithms: Design and Analysis, Part 1
    双目测距项目
  • 原文地址:https://www.cnblogs.com/whxiao/p/13439907.html
Copyright © 2011-2022 走看看