nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

#nginx用户及组,window下不指定。user用户 组 ;
user nginx;
#nginx的工作进程数,数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

#指定进程可以打开的最大描述符,我这里设置int最大值,可不设置
#worker_rlimit_nofile 65535;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
# 每个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。
# 每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。worker_processes*worker_connections
    worker_connections 1024;
# 使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
# 不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的
    use epoll;
# 告诉nginx收到一个新连接通知后接受尽可能多的连接。
    multi_accept on;
#keepalive超时时间。
    keepalive_timeout 60;

#客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
#分页大小可以用命令getconf PAGESIZE 取得。client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。
    client_header_buffer_size 4k;


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

# 多长时间检查一次缓存的有效信息。
    open_file_cache_valid 80s;
# open_file_cache指令中的inactive参数时间内文件的最少使用次数,
# 如果超过这个数字,文件描述符一直是在缓存中打开的,
# 如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除
    open_file_cache_min_uses 1;
}

# 设定http服务器,利用它的反向代理功能提供负载均衡支持
http {

#设置日志记录格式,
# 远程访问地址-访问主机名[访问时间] url和http协议
# 请求状态  requestBody内容大小 那个页面链接访问过来
# 客户浏览器的相关信息 http forward头部ip地址
    log_format json '{ "@timestamp": "$time_local", '
                         '"@fields": { '
                         '"remote_addr": "$remote_addr", '
                         '"http_host": "$http_host",'
                         '"remote_user": "$remote_user", '
                         '"upstream_addr": "$upstream_addr",'
                         '"body_bytes_sent": "$body_bytes_sent", '
                         '"request_time": "$request_time", '
                         '"status": "$status", '
                         '"request_uri": "$request_uri", '
                         '"request_method": "$request_method", '
                         '"http_referrer": "$http_referer", '
                         '"body_bytes_sent":"$body_bytes_sent", '
                         '"http_x_forwarded_for": "$http_x_forwarded_for", '
                         '"http_user_agent": "$http_user_agent" } }';

    access_log  /var/log/nginx/access.log  json;
# 保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的
# 如果需要增大hash max size 或 hash bucket size,我们需要增大server_names_hash_max_size的值
    server_names_hash_bucket_size 128;

# 客户端请求头部的缓冲区大小
    client_header_buffer_size 4k;
# 客户端请求头的缓冲区的最大数量和大小
    large_client_header_buffers 8 128k;

    open_file_cache max=102400 inactive=20s;
#设定通过nginx上传文件的大小
    client_max_body_size 300m;

#指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件。普通应用,必须设为on
#用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime
    sendfile            on;
#此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

#代理服务器连接的超时时间_发起握手等候响应超时时间
    proxy_connect_timeout 90;
#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
    proxy_read_timeout 180;
#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
    proxy_send_timeout 180;

# 设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头。
# 默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小
    proxy_buffer_size 256k;

# 设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小
    proxy_buffers 4 256k;

#设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长
    proxy_busy_buffers_size 256k;
    proxy_temp_file_write_size 256k;
    proxy_temp_path /data0/proxy_temp_dir;

# proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
# 设置内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
    proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
# 如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常
# 默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,再提交256k图片则会报500异常
    client_body_buffer_size 512k;

# 默认为off , 表示使nginx阻止HTTP应答代码为400或者更高的应答。指定的error_page错误页面才能生效
    proxy_intercept_errors on

# 设定mime类型,类型由mime.type文件定义
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

# gzip默认开启,默认作用于text/html格式的文件(后缀html htm shtml默认压缩),可以自定义gzip_types设置想要压缩的文件格式
    gzip on;
# 设置压缩响应所需的最小http协议版本1.1或1.0,默认值是1.1
    gzip_http_version 1.1;
# 指定允许压缩的文件类型,文本内容css,xml,json,javascript可以进行压缩
    gzip_types     text/plain text/css application/xml application/json text/javascript application/javascript application/x-javascript application/xml+rss;
# nginx作为反向代理时候设置哪个条件启动gzip
    gzip_proxied   expired no-cache no-store private auth;
# gzip_disable的设置是禁用IE6的gzip压缩
    gzip_disable   "MSIE [1-6].";
# 指定压缩的文件最小尺寸,单位 bytes 字节,低于该值的不压缩,超过该值的将被压缩
    gzip_min_length  1k;
# 以16k为单位,按照原始数据大小以16k为单位的4倍申请内存,默认值是申请跟原始数据相同大小的内存空间去存储gzip压缩结果。
    gzip_buffers     4 16k;
# 指定压缩等级,其值从1到9,数字越大,压缩率越高,越消耗CPU,负载也越高
# 一般使用1-4等级
    gzip_comp_level 2;
# 增加响应头Vary:Accept-Encoding,可以根据头信息返回压缩和非压缩副本
    gzip_vary on;

    # 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.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
#这是一个无效的域名,默认访问http://localhost时,如果找不到匹配的域名,则会访问第一个server节点,可以取任意匹配不到的字符串
#比如可以设置server_name s; ,,这个也可以使用localhost正确访问到nginx欢迎页面
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }


}

原文地址:https://www.cnblogs.com/syavingcs/p/12527652.html