nginx配置优化

user deploy;
#为worker_processes增加参数auto。当设置成auto,tengine将自动启动与cpu数量相同的worker进程
worker_processes auto;
#为worker_cpu_affinity增加参数auto和off。当设置成auto时,tengine将根据worker的数量自动配置cpu绑定位图。
worker_cpu_affinity auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
    use epoll;
    #单个进程最大连接数(最大连接数=连接数*进程数)
    worker_connections 65535;
}

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;
    charset utf-8; #默认字符集
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    server_tokens off; # 隐藏版本号
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # 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.


    #gzip模块设置
    gzip on; #开启gzip压缩输出
    gzip_disable "msie6"; #为指定的客户端禁用 gzip 功能。我们设置成 IE6 或者更低版本以使我们的方案能够广泛兼容。
    gzip_min_length 3k; #设置对数据启用压缩的最少字节数。如果一个请求小于 1000 字节,我们最好不要压缩它,因 为压缩这些小的数据会降低处理此请求的所有进程的
    gzip_buffers 4 16k; #压缩缓冲区
    gzip_http_version 1.0; #压缩版本
    gzip_comp_level 4; #设置数据的压缩等级。这个等级可以是 1-9 之间的任意数值,9 是最慢但是压缩比最大的。我 们设置为 4,这是一个比较折中的设置。
    gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_vary on;

    set_real_ip_from 100.120.0.0/16;
    real_ip_header X-Forwarded-For;

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

  

原文地址:https://www.cnblogs.com/The-day-of-the-wind/p/12103000.html