zoukankan      html  css  js  c++  java
  • Nginx 参数配置相关

    Nginx参数配置相关

    by:授客 QQ1033553122

    目的:

    Nginx配置的点滴学习总结,主要目的在于分析Nginx与性能相关的一些参数设置,以便性能调优时选择最优配置

     

    环境:

    $ cd /usr/local/nginx/sbin/

    $ ./nginx -v

    nginx version: nginx/1.8.0

     

    配置文件说明:

    # cat /usr/local/nginx/conf/nginx.conf

    # 使用的用户和组

    #user  nobody;

    说明:

    语法:user user [group];

    默认值: user nobody nobody;

     

    # 定义工作衍生进程(worker processe)数

    worker_processes  1;

     

    说明:

    语法:worker_processes number | auto; number:指定进程数量,auto:自动调整为可获取的CPU核数

    默认值:worker_processes 1;

    worker_processes的最佳值依赖很多因素,包括(但不局限于)CPU核数,存储数据的硬盘驱动数量,负载模式。如果拿捏不定的话,设置为可获取的CPU核数或者auto

    1.3.81.2.5版本开始,才支持auto参数

    参考连接:

    http://nginx.org/en/docs/ngx_core_module.html#worker_processes

     

    nginx有一个主进程和一些工作进程(Worker processes)。主进程主要用于读取和评估配置,维护工作进程。工作进程真正执行请求的处理。nginx采用基于事件的模型(event-based model)和依赖操作系统的机制有效的分发请求到不同的工作进程中。

    参考连接:

    http://nginx.org/en/docs/beginners_guide.html

     

    # [ debug | info | notice | warn | error | crit ]

    # 可以在下方直接使用 [ debug | info | notice | warn | error | crit ]  参数

    #error_log  logs/error.log;

    #error_log  logs/error.log  notice;

    #error_log  logs/error.log  info;

     

    说明:

    语法:error_log file [level];

    默认值:error_log logs/error.log error;

     

    配置日志。如果没指定日志文件和日志级别,则使用默认文件和默认级别。

    第一个参数:file - 定义存储日志的文件,建议存储路径:/var/log/nginx.error_logstderr - 选择标准错误文件;可通过指定”syslog:”前缀配置日志记录到sysLog,指定“memory: 前缀和buffer大小来配置日志记录到循环内存缓冲区,这个通常用于调试,比如 error_log memory:32m debug;

    第二个参数: 定义日志级别,默认为error,可以是以下参数之一:

    debug, info, notice, warn, error, crit, alert, emerg。所列日志级别按严重程度升序排序。指定某个日志级别后,级别在其之后(更严重级别)的日志也会被记录。比如指定了warn,那么error, crit, alert, emerg级别的日志都会被记录。

    如果需要调试日志,编译nginx时需要携带 --with-debug选项编译,更多查看 A debugging log

     

    参考连接:

    http://nginx.org/en/docs/debugging_log.html#memory

    http://nginx.org/en/docs/ngx_core_module.html#error_log

     

    # 指定 pid 存放的路径,建议:pid /var/run/nginx.pid;

    #pid        logs/nginx.pid;

     

    events {

        # 设置可以被单个工作进程打开的最大并发连接数

        worker_connections  1024;

    }

    设置可以被单个工作进程打开的最大并发连接数。需要注意的是,该设置包含了所有的连接(比如和代理服务的连接),不仅仅是同客户端的连接。另外,实际并发连接数不能超过当前可打开最大文件描述符数限制,可通过修改work rlimit nofile来修改当前可打开最大文件描述符数限制。如下:

    worker_processes  1;

    worker_rlimit_nofile 65535;

     

    说明:

    语法:worker_rlimit_nofile number;

     

    修改工作进程可打开的文件描述符数。用于不重启main进程的情况下,增加可打开的文件描述符数。number的值不能大于操作系统的进程能打开的最大的文件句柄数

    events 中还可以指定连接处理方式,如下

    events {

    worker_connections  1024;

        # 指定连接处理方式,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;

    use kqueue;

     

    }

    说明:

    语法: use method;

    注意,通常没必要显示指定连接的处理方式。Nginx会自动采用最有效率的连接方式。

     

    语法: accept_mutex on | off;

    默认值: accept_mutex off;

    如果开启accept_mutexworker进程将轮流接收新的连接。否则,所有进程都会收到新连接的通知,这样,如果果新连接数量很少的情况下,可能会导致一些工作进程浪费系统资源。

     

    对于支持EPOLL除外标识的系统,或者使用reuseportlisten 后面携带reuseport选项),没必要开启accept_mutexThere is no need to enable accept_mutex on systems that support the EPOLLEXCLUSIVE flag (1.11.3) or when using reuseport.

     

    1.11.3版本之前,默认值为on

    参考连接:

    http://nginx.org/en/docs/ngx_core_module.html#worker_connections

    http://nginx.org/en/docs/ngx_core_module.html#worker_rlimit_nofile

    http://nginx.org/en/docs/events.html

    http://nginx.org/en/docs/ngx_core_module.html#accept_mutex

     

    http {

        include       mime.types;

        default_type  application/octet-stream;

     

        # 设置日志格式

        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

        #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    更多详情参考:http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format

       

        # 设置访问日志,如果不必要的话,可以设置关闭 access_log off;让读取磁盘IO操作更快

    #access_log  logs/access.log  main;

    更多详情参考:http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

     

    # sendfile()起作用,建议开启

        sendfile        on;

        # 仅在 sendfile开启的情况下使用tcp_nopush

    #tcp_nopush     on;

    更多详情参考:http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nopush

     

    # tcp_nodelaysend_lowat 在默认配置中是没有体现的

    tcp_nodelay  on;

    语法:tcp_nodelay on | off;

    默认值:tcp_nodelay on;

    开启、禁用TCP_NODELAY选项。该选项仅在连接处于keep-alive状态下使用。

    参考连接:http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nodelay

     

    send_lowat       12000;

    说明:

    语法: send_lowat size;

    默认值: send_lowat 0;

    如果size设置为非0值,nginx将通过使用kqueueNOTE_LOWA标识或SO_SNDLOWAT选项尽量减少send操作次数

    LinuxSolarisWindows上,该指令被忽略

    参考连接:http://nginx.org/en/docs/http/ngx_http_core_module.html#send_lowat

     

        #keepalive_timeout  0;

    keepalive_timeout  65;

     

    说明:

    语法:keepalive_timeout timeout [header_timeout];

    默认值:keepalive_timeout 75s;

    第一个参数:设置keep-alive客户端连接在服务器端保持open状态时间,超过这个时间服务器将关闭连接。如果设置为0,那么禁用keep-alive客户端连接。

    第二个参数:可选参数,在响应头头域设置一个响应头Keep-Alive: timeout=time,其中time等于header_timeoutIE浏览器会在大致60s后自动关闭keep-alive连接。

    这里如果设置时间太长,连接长时间不关闭,而导致连接太多,占用过多资源,所以值得根据具体情况设置。

    参考连接:    http://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout

     

    # 可以考虑补充以下指令

    reset_timeout_connection on;

     

    语法:reset_timedout_connection on | off;

    默认值:reset_timedout_connection off;

    帮助避免已经关闭的socket长时间处于FIN_WAIT1状态并占用缓冲区(This helps avoid keeping an already closed socket with filled buffers in a FIN_WAIT1 state for a long time)。也就说开启后,可以避免不必要的资源占用。

    参考连接:http://nginx.org/en/docs/http/ngx_http_core_module.html#reset_timedout_connection

     

    语法: send_timeout time;

    默认值:send_timeout 60s;

    设置传输响应体到客户端的超时时间。这里的超时时间是指两次成功写操作之间的时间间隔。在这个时间范围内,客户端没接收到任何数据,那么连接被关闭。

    参考连接:http://nginx.org/en/docs/http/ngx_http_core_module.html#send_timeout

     

    #gzip  on;

    说明:

    语法:gzip on | off;

    开启、禁用压缩请求响应体(response),开启后可大大减少传输数据的大小,建议开启。可以配合其它gzip_xxxx设置使用。

    gzip on;

        gzip_min_length  1100;

        gzip_buffers     4 8k;

    gzip_types       text/plain;

     

    语法: gzip_min_length length;

    默认:gzip_min_length 20;

    设置仅响应头Content-Length的值超过gzip_min_length长度的响应体(response)才进行压缩。建议:如果请求小于1000字节,最好不要压缩它

    参考连接:http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_min_length

       

    语法:gzip_types mime-type ...;

    默认值:gzip_types text/html;

    指定除text/html之外的,需要压缩的响应体的MIME类型。如指定值 * 则匹配任意MIME类型(0.8.29)。携带text/html的响应体类型总是被压缩。

    参考连接:http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_types

     

    语法:gzip_buffers number size;

    默认值:gzip_buffers 32 4k|16 8k;

    设置用于压缩响应体的缓冲区数量和大小。默认的,buffer大小等于一个内存页大小,4K8K, 取决于操作系统。

    0.7.28为止,默认的使用44k8K

    参考连接:http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_buffers

     

    更多参数研究,查看官方文档:http://nginx.org/en/docs/

     

  • 相关阅读:
    Vue学习Day05-Vue中组件间的通讯(父到子,子到父,兄弟间)
    mysql数据库阻塞事务分析(thread_running突然升高)
    新建npm的nexus本地仓库,npm install报错npm ERR! code E401
    sqlserver数据库可以ping通但是1433端口无法telnet
    ansible使用synchronize模块报Broken pipe
    centos7上vagrant的安装及使用
    centos无法安装后无法识别r6818网卡问题
    手动上传snapshot和第三方jar包到nexus3
    搭建docker私有仓库nexus
    python3的pip安装报错pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
  • 原文地址:https://www.cnblogs.com/shouke/p/10157622.html
Copyright © 2011-2022 走看看