zoukankan      html  css  js  c++  java
  • NGINX_深度优化实践

    一  优化web版本号

    1. 在Nginx配置文件的http模块内加入
    http {
        server_tokens on;
    }
    2. 通过源码修改版本信息
    (1) 
     [root@web01 ~]# vim /tools/nginx-1.6.3/src/core/nginx.h
    [root@web01 ~]# sed -n "13,17p" /tools/nginx-1.6.3/src/core/nginx.h
    #define NGINX_VERSION      "1.6.3"
    #define NGINX_VER          "NGINX_VERSION/" NGINX_VERSION
    #define NGINX_VAR          "NGINX"
    #define NGX_OLDPID_EXT     ".oldbin"
    (2) 
    [root@web01 ~]# vim  /tools/nginx-1.6.3/src/http/ngx_http_header_filter_module.c 
    [root@web01 core]# sed -n "49p" /tools/nginx-1.6.3/src/http/ngx_http_header_filter_module.c 
    static char ngx_http_server_string[] = "Server: NGINX_VERSION" CRLF;
    (3)
    [root@web01 ~]# vim  /tools/nginx-1.6.3/src/http/ngx_http_special_response.c
    [root@web01 core]# sed -n '21,29p' /tools/nginx-1.6.3/src/http/ngx_http_special_response.c 
    static u_char ngx_http_error_full_tail[] =
    "<hr><center>" ningpengju "</center>" CRLF
    "</body>" CRLF
    "</html>" CRLF
    static u_char ngx_http_error_tail[] =
    "<hr><center>NGINX_VERSION</center>" CRLF
     
    安装之前修改,若已经安装完则需要重新编译!!!
     
    二. 更改Nginx的默认用户
     
    1.编译时 ./configure --user指定nginx用户,如果不指定则为nobody
    [root@web01 ~]# grep '#user' /application/nginx/conf/nginx.conf.default
    #user  nobody;
     
    三. 优化Nginx服务的进程数
     
     1.worker_processes 1;  #<==指定nginx开启的进程数。可以设置等于CPU的核数。
    (1) TOP命令然后按 “1” 查看cpu核数
    (2) 或者使用cat /proc/cpuinfo命令 
    [root@iZ255aksz8rZ ~]# grep -c ‘processor’  /proc/cpuinfo
    2
     
    2.调整CPU亲和力(让CPU平均运行NGIXN进程)
    worker_cpu_affinity 0001 0010 0100 1000;
    #<==就是设置nginx进程CPU亲和力的参数,吧不同的进程分给不同的CPU处理,这里的0001 0010 0100 1000是掩码,分别代表第1-4核CPU核心,由于worker_processes的进程数为4,因此,上述配置会把每个进程分配一核CPU处理,默认情况下进程不会绑定任何CPU,参数位置为MAIN段
    八核配置如下:
    worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
    (taskset可以指定mysql的CPU亲和力设置)
     
    四. 处理模型优化
     
    1. 在events模块内指定epoll模型
    events{
    use epoll;
               }
    2. 单个进程允许的客户端最大连接数(单个worker的最大并发连接)
    events{
    use epoll;
                worker_connections  4096;
               }
    理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。
     
    3. 配置进程worker进程最大打开的文件数
    events{
    worker_rlimit_nofile 65535;    
    }
    #<==最大打开文件数,可设置为系统优化后ulimit -HSn的结果。
     
    4. 开启高效文件传输模式
    http {
    sendfile on;
    tcp_nopush off;       #在一个数据包里发送所有头文件

                tcp_nodelay on;      #告诉nginx不要缓存数据

    }
     
    5.优化服务器域名的hash表大小
    server_names_hash_bucket_size 128;
    server_names_hash_max_size 512;
    #保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小
     
    6. nginx连接超时参数设置

    keepalive_timeout 60;

    keepalive超时时间。

    7. 设置用户请求头的超时时间

    client_header_timeout

    含义:设置用户请求头的超时时间。

    语法:client_header_timeout <time>

    缺省:1m

    作用域:http.server.location

    示例:client_header_timeout 3m;

    注意:只有请求头需要被1次以上读取时,该超时时间才会被设置。且如果这个时间后用户什么都没发,nginx会返回requests time out 408

  • 相关阅读:
    USB小白学习之路(6) IIC EEPROM读取解析
    USB小白学习之路(5) HID鼠标程序
    USB小白学习之路(4)HID键盘程序
    USB小白学习之路(3) 通过自定义请求存取外部RAM
    USB小白学习之路(2)端点IN/OUT互换
    USB小白学习之路(1) Cypress固件架构解析
    LeetCode -- 14 最长公共前缀
    初识docker——对docker的理解
    洛谷 P5461 赦兔战俘
    知识碎片 —— 数组 与 伪数组
  • 原文地址:https://www.cnblogs.com/liuqiang0/p/8527564.html
Copyright © 2011-2022 走看看