nginx限流

load_module modules/ngx_stream_module.so;   #动态加载模块,必须写道开头
user  nginx;   #使用useradd nginx    添加一个nginx用户
worker_processes  4;   #cpu核心数 * 2
worker_rlimit_nofile   102400;  #配置nginx打开最大文件数  (每个工作进程绑定一个cpu,worker_cpu_affinity配置)
worker_cpu_affinity 0001 0010 0100 1000;  #工作进程使用哪个cpu的核心 (以四核为例)  0001是4核的第一个核心 0010是4核的第二个核心 

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  10240;
}


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"';

    #access_log  logs/access.log  main;   #在server虚拟目录里面配置日志,这里是全局日志

    sendfile        on;   
    #tcp_nopush     on;

    

    server_tokens off;  #错误的时候关闭输出版本号


    #keepalive_timeout  0;
    keepalive_timeout  30;

    gzip  on;   #压缩会占用cpu
    gzip_buffers 4 16k;
    gzip_comp_level 3;  #压缩等级
    gzip_disable "MSIE[1-6]";   #ie浏览器1-6禁用gzip
    gzip_min_length 1k;
    gzip_http_version 1.0;
    gzip_types text/plaion application/html application/css application/js;  #可以压缩的文件类型
    gzip_vary on;  #根据http头判断是否支持压缩

    client_max_body_size 8m;   #默认允许客户端最大上传文件大小
	
	#限流
	#limit_conn_zone $binary_remote_addr zone=addr:10m;         	 #并发限制(同时启用一个)
	limit_req_zone $binary_remote_addr zone=qps:10m rate=1r/s;  	 #请求限制   每秒钟处理一个请求
	limit_conn_log_level error;
	limit_conn_status 503;   #超出限制时,返回状态码
	
	
	server{
		#limit_conn addr 1;  #并发限制设置为1,是为了测试 addr是zone空间在53行(同时启用一个)
		
		#limit_req zone=qps;  #请求限制 
		limit_req zone=qps burset=1 nodelay;  #请求限制()
		
	}
	
	
	
	
	
	
	
	#限流end
	
}

猜你喜欢

转载自blog.csdn.net/qq_28710983/article/details/80923097