
#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #脚本位置
    lua_package_path "/usr/local/openresty/nginx/lua/?.lua;/usr/local/openresty/lua-resty-http-master/lib/?.lua;/usr/local/openresty/lua-resty-jwt-master/lib/resty?.lua;;";

    #Nginx代理缓存配置
    proxy_cache_path /usr/local/openresty/nginx/cache levels=1:2 keys_zone=openresty_cache:10m max_size=10g inactive=60m use_temp_path=off;


   #定义Nginx缓存
   lua_shared_dict act_cache 128m;


    #限流设置  每秒钟只处理2个并发请求
    limit_req_zone $binary_remote_addr zone=contentRateLimit:10m rate=2r/s;


    #根据IP地址来限制，存储内存大小10M
    limit_conn_zone $binary_remote_addr zone=addr:1m;

    #IP限流
    limit_conn_zone $binary_remote_addr zone=perip:10m;
    #根据server的名字限流
    limit_conn_zone $server_name zone=perserver:10m;
 
    server {
        listen       80;
        server_name  localhost;

	#跨域配置
	add_header Access-Control-Allow-Origin *;
    	add_header Access-Control-Allow-Methods 'GET,POST';
    	add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'; 


	#Nginx队列术，锁定用户请求
	location /mlog/top {
                 #启用缓存openresty_cache
                proxy_cache openresty_cache;
                #针对指定请求缓存
                #proxy_cache_methods GET;
                 #设置指定请求会缓存
                proxy_cache_valid 200 304 10s;
                 #最少请求1次才会缓存
                proxy_cache_min_uses 1;
                #如果并发请求，只有第1个请求会去服务器获取数据
                proxy_cache_lock on;
                #唯一的key
                proxy_cache_key $host$uri$is_args$args;
		
	
		proxy_pass http://192.168.0.101:18082;
	}


	#user限流配置
	location /userinfo {
		#一个用户限流
		#limit_conn perip 3;

		#配置整个server限流
		#limit_conn perserver 5;		

		#使用指定的缓存配置
		limit_req zone=contentRateLimit burst=4 nodelay;

		#并发量限流 每秒钟只允许有2个并发请求
		#limit_conn addr 2;
		proxy_pass http://192.168.0.101:18082;
	}


        #Lua实现Http请求
        location /api {
                content_by_lua_file /usr/local/openresty/nginx/lua/httpsend.lua;
        }



	#抢红包排第
        location /red {
                content_by_lua_file /usr/local/openresty/nginx/lua/mq.lua;
        }


	#根据ID实现活动查询
	location /act {
		content_by_lua_file /usr/local/openresty/nginx/lua/activity.lua;
	}


	#清理缓存
	location ~ /purge(/.*) {
    	    #清理缓存
	    proxy_cache_purge openresty_cache $host$1$is_args$args;
	}


	#配置Nginx缓存
        location /user111 {
		 #启用缓存openresty_cache
	    	proxy_cache openresty_cache;
	    	#针对指定请求缓存
	    	#proxy_cache_methods GET;
	   	 #设置指定请求会缓存
	    	proxy_cache_valid 200 304 10s;
	   	 #最少请求1次才会缓存
	    	proxy_cache_min_uses 3;
		#如果并发请求，只有第1个请求会去服务器获取数据
	    	#proxy_cache_lock on;
	    	#唯一的key
 	    	proxy_cache_key $host$uri$is_args$args;
		proxy_pass http://192.168.211.1:18082;
	}


	#  http://192.168.211.141/1.html  /usr/local/server/html/目录下找1.html文件
	location / {
		root /usr/local/server/html/;
		#过期时间
		expires 10h;
	}

	
    }

}
