Nginx配置

events {
     # 语法  use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
     use epoll;   # 使用epoll(linux2.6的高性能方式)
     worker_connections 51200; #每个进程最大连接数(最大连接=连接数×进程数)
     # 并发总数是 worker_processes 和 worker_connections 的乘积
     # 即 max_clients = worker_processes * worker_connections
     # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4
     # 并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
     # 查看系统可以打开的最大文件数
     # cat /proc/sys/fs/file-max
}


gzip    on;    # 开启gzip,默认关闭  
gzip_comp_level    5;    # 压缩级别,1-9,级别越高压缩率越高,但是相应的耗cpu  
gzip_min_length    1025;    # 当响应内容大小大于多少bytes后使用gzip  
gzip_types    text/plain application/x-javascript application/json text/javascript text/css    # 对于什么类型的内容使用gzip


worker_processes 16;


events {
    accept_mutex off;
}


http {
    include mime.types;
    default_type application/octet-stream;


    access_log off;
    sendfile on; #启用sendfile并设置sendfile_max_chunk,有利于减少阻塞调用sendfile时带来的总时间。
    sendfile_max_chunk 512k;
    tcp_nopush     on;


    server {
        listen 8000;


        location / {
            root /storage;
thread_pool default threads=32 max_queue=65536;
#启用aio时会自动启用directio,小于directio定义的大小的文件则采用sendfile进行发送,超过或等于directio定义的大小的文件,将采用aio线程池进行发送,
#也就是说aio和directio适合大文件下载.因为大文件不适合进入操作系统的buffers/cache,这样会浪费内存,而且Linux AIO(异步磁盘IO)也要求使用directio的形式.
#sendfile_max_chunk可以减少阻塞调用sendfile()所花费的最长时间.因为Nginx不会尝试一次将整个文件发送出去,而是每次发送大小为256KB的块数据.
#注意,Nginx从1.7.11开始为AIO引入了线程池支持,能够使用多线程读取和发送文件,以免工人进程被阻塞.要启用多线程支持,configure时需要显式加入--with-threads选项.
aio threads=default;
directio 512k;
output_buffers 1 128k; 
        }
    }
}




http {
    thread_pool one threads=128 max_queue=0;
    thread_pool two threads=32; #在未定义max_queue时默认为65536,当设置成0时,服务能力等同线程数量。


    server {
        location /one {
            aio threads=one;
        }


        location /two {
            aio threads=two;
        }
    }

}


#假如你的缓存代理服务器有3块磁盘,内存不能放下预期需要缓存的文件,所以我们首先需要让磁盘工作最大化。
# We assume that each of the hard drives is mounted on one of the directories:
# /mnt/disk1, /mnt/disk2, or /mnt/disk3 accordingly
proxy_cache_path /mnt/disk1 levels=1:2 keys_zone=cache_1:256m max_size=1024G 
                 use_temp_path=off;
proxy_cache_path /mnt/disk2 levels=1:2 keys_zone=cache_2:256m max_size=1024G 
                 use_temp_path=off;
proxy_cache_path /mnt/disk3 levels=1:2 keys_zone=cache_3:256m max_size=1024G 
                 use_temp_path=off;


thread_pool pool_1 threads=16;
thread_pool pool_2 threads=16;
thread_pool pool_3 threads=16;


split_clients $request_uri $disk {
    33.3%     1;
    33.3%     2;
    *         3;
}


location / {
    proxy_pass http://backend;
    proxy_cache_key $request_uri;
    proxy_cache cache_$disk;
    aio threads=pool_$disk;
    sendfile on;
}
#使用了3个独立的缓存,每个缓存指定到一块磁盘,然后有3个独立的线程池。
#split_clients模块用于缓存间的负载均衡。
#use_temp_path=off参数让Nginx将缓存文件保存至文件同级目录,可以避免缓存更新时磁盘间的文件数据交换。






#  kencery 注释说明Nginx文件
#  时间:2016-1-19
#  学习内容,只是来自互联网,有版权问题请联系我删除。


########   Nginx的main(全局配置)文件
#指定nginx运行的用户及用户组,默认为nobody
#user  nobody;   


#开启的线程数,一般跟逻辑CPU核数一致
worker_processes  1;   


#定位全局错误日志文件,级别以notice显示,还有debug,info,warn,error,crit模式,debug输出最多,crir输出最少,根据实际环境而定
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


#指定进程id的存储文件位置
#pid        logs/nginx.pid;


#指定一个nginx进程打开的最多文件描述符数目,受系统进程的最大打开文件数量限制
#worker_rlimit_nofile 65535


events {
    #设置工作模式为epoll,除此之外还有select,poll,kqueue,rtsig和/dev/poll模式
    #use epoll;
    
    #定义每个进程的最大连接数,受系统进程的最大打开文件数量限制。
    worker_connections  1024;
}


#######Nginx的Http服务器配置,Gzip配置
http {
    #主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度,DNS主配置文件中的zonerfc1912,acl基本上都是用include语句。
    include       mime.types;
    
    #核心模块指令,智力默认设置为二进制流,也就是当文件类型未定义时使用这种方式
    default_type  application/octet-stream;


    #下面代码为日志格式的设定,main为日志格式的名称,可自行设置,后面引用
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';


    #引用日志main
    #access_log  logs/access.log  main;


    #设置允许客户端请求的最大的单个文件字节数
    #client_max_body_size 20M;
    #指定来自客户端请求头的headebuffer大小
    #client_header_buffer_size  32k;
    #指定连接请求试图写入缓存文件的目录路径
    #client_body_temp_path /dev/shm/client_body_temp;
    #指定客户端请求中较大的消息头的缓存最大数量和大小,目前设置为4个32KB
    #large client_header_buffers 4 32k;
    
    #开启高效文件传输模式
    sendfile        on;
    #开启防止网络阻塞
    #tcp_nopush     on;
    #开启防止网络阻塞
    #tcp_nodelay    on;
    
    #设置客户端连接保存活动的超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;


    #设置客户端请求读取超时时间
    #client_header_timeout 10;
    #设置客户端请求主体读取超时时间
    #client_body_timeout 10;
    #用于设置相应客户端的超时时间
    #send_timeout 
    
    ####HttpGZip模块配置
    #httpGzip modules
    #开启gzip压缩
    #gzip  on;
    #设置允许压缩的页面最小字节数
    #gzip_min_length 1k;
    #申请4个单位为16K的内存作为压缩结果流缓存
    #gzip_buffers 4 16k;
    #设置识别http协议的版本,默认为1.1
    #gzip_http_version 1.1;
    #指定gzip压缩比,1-9数字越小,压缩比越小,速度越快
    #gzip_comp_level 2;
    #指定压缩的类型
    #gzip_types text/plain application/x-javascript text/css application/xml;
    #让前端的缓存服务器进过gzip压缩的页面
    #gzip_vary on;  
    
    #########Nginx的server虚拟主机配置
    server {
        #监听端口为 80
        listen       80;
        
        #设置主机域名
        server_name  localhost;
        
        #设置访问的语言编码
        #charset koi8-r;


        #设置虚拟主机访问日志的存放路径及日志的格式为main
        #access_log  logs/host.access.log  main;


        #设置虚拟主机的基本信息
        location / {
            #设置虚拟主机的网站根目录
            root   html;
            
            #设置虚拟主机默认访问的网页
            index  index.html index.htm;
        }


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


#开启监听
location /nginx_status{
stub_status on;
access_log off;
#allow 127.0.0.1;
#deny all;
}




-- 检测文件是否存在  
local file_exists = function(name)  
    local f=io.open(name,"r")  
    if f~=nil then io.close(f) return true else return false end  
end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值