nginx.conf配置文件详解

#运行用户
    user www www;
    #启动进程,通常设置成和cpu的数量相等
    worker_processes  4;
	worker_cpu_affinity 0001 0010 0100 1000;
	
	worker_processes  8;
	worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

    #全局错误日志及PID文件 日志级别:debug|info|notice|warn|error|crit
    error_log  /var/log/nginx/error.log error;
    pid        /var/run/nginx.pid;
	
	worker_rlimit_nofile 51200; 更改worker进程的最大打开文件数限制。如果没设置的话,这个值为操作系统的限制

    #工作模式及连接数配置
    events {
        use epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
        worker_connections  1024;#单个后台worker process进程的最大并发链接数
        multi_accept off;       #多个worker按串行方式来处理连接,也就是一个连接只有一个worker被唤醒,其他的处于休眠状态。设置为off后,多个worker按并行方式来处理连接,也就是一个连接会唤醒所有的worker,知道连接分配完毕,没有取得连接的继续休眠。当你的服务器连接数不多时,开启这个参数会让负载有一定程度的降低。但是当服务器的吞吐量很大时,为了效率,请关闭这个参数

    }

    #设定http服务器,利用它的反向代理功能提供负载均衡支持
    http {
        include       mime.types;#设定mime类型,类型由mime.type文件定义
        default_type  text/html;
		client_max_body_size 8m; #客户端上传的body的最大值
		
		lua_max_pending_timers 10240;

        sendfile on; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
        tcp_nopush on; #数据包会累积一下再一起传输,可以提高一些传输效率
        server_tokens off;#关闭在错误页面中的nginx版本数字
        
		if_modified_since exact  #判断页面是不是最新的,浏览器本地最新的浏览器从本地获取。
		
        tcp_nodelay  on; #小的数据包不等待直接传输
		access_log off; 
		error_log /var/log/nginx/error.log crit;#告诉nginx只能记录严重的错误
		
		limit_conn addr 100;#为给定的key设置最大连接数。这里key是addr,我们设置的值是100,也就是说我们允许每一个IP地址最多同时打开有100个连接
		
		#连接超时时间
        keepalive_timeout  10; #keepalive_disable none; 
		client_header_timeout 10;#设置请求头和请求体(各自)的超时时间。我们也可以把这个设置低些
		client_body_timeout 10;
		reset_timeout_connection on;#关闭不响应的客户端连接
		send_timeout 10;#客户端没有读取任何数据,nginx就会关闭连接
       
	    client_header_buffer_size 128k; #设置header的缓冲大小
	    large_client _header_buffer 4 128k;#客户请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,如果header过大,它会使用large_client_header_buffers来读取
		
		#开启gzip压缩
        gzip  on;
        gzip_disable "MSIE [1-6]\.(?!.*SV1)"; #禁用某些浏览器的gizp   Microsoft Internet Explorer,简称MSIE
		gzip_proxied any;   #Nginx作为反向代理的时候启用,根据某些请求和应答来决定是否在对代理请求的应答启用gzip压缩
							#expired - 启用压缩,如果header头中包含 "Expires" 头信息
							#no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息
							#no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息
							#private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息
							#no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息
							#no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息
							#auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息
							#any - 无条件启用压缩
		gzip_min_length 1000; #对数据启用压缩的最少字节数。如果一个请求小于1000字节,我们最好不要压缩它
		gzip_comp_level 4; #设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置
		gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
		
		#php fastcgi的缓存
		fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
		fastcgi_cache_key "$scheme$request_method$host$request_uri";
		fastcgi_connect_timeout 300;
		fastcgi_send_timeout 300;
		fastcgi_read_timeout 300;
		fastcgi_buffer_size 16k;
		fastcgi_buffers 16 16k;
		fastcgi_busy_buffers_size 16k;
		fastcgi_temp_file_write_size 16k;
		fastcgi_cache TEST;
		fastcgi_cache_valid 200 302 1h;
		fastcgi_cache_valid 301 1d;
		fastcgi_cache_valid any 1m;
		fastcgi_cache_min_uses 1;
		fastcgi_cache_use_stale error timeout invalid_header http_500;
		
	   
        

        open_file_cache max=204800 inactive=20s;#nginx打开文件 缓存的同时也指定了缓存最大数目,以及缓存的时间,缓存溢出,最长使用过的文件(LRU)将被移除
        open_file_cache_valid 30s;#在open_file_cache中指定检测正确信息的间隔时间
		open_file_cache_min_uses 2;#定义了open_file_cache中指令参数不活动时间期间里最小的文件数
		open_file_cache_errors on;
		
		#proxy代理缓存
		proxy_cache_path /cache/proxy_cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=30g; #100m和30G,按照服务要求,适当增大
		proxy_temp_path /cache/proxy_temp;

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

        #设定负载均衡的服务器列表,实现nginx的内部跳转
        upstream mysvr {
			#weigth参数表示权值,权值越高被分配到的几率越大
			#本机上的Squid开启3128端口
			server 192.168.8.1:80  weight=5;
			server 192.168.8.2:80  weight=1;
			server 192.168.8.3:80  weight=6;
        }
		
		
		server {
        listen 2001;
        server_name  gmslog.jd.com;
			location / {
				autoindex on;
				root   "/export/Logs/gms-log";
				index  index.html index.log;
			}
		}


       server {
        #侦听80端口
        listen 80;
        #定义使用www.xx.com访问
        server_name  www.xx.com;
		index index.html index.htm
        #设定本虚拟主机的访问日志格式
		log_format tick "$msec|||$u_t|||$http_x_forwarded_for|||$u_domain|||$u_url|||$u_title|||$u_referrer|||$u_sh|||$u_sw|||$u_cd|||$u_lang|||$http_user_agent|||$u_utrace|||$u_account|||$u_time";
        access_log  logs/www.xx.com.access.log  tick buffer=32k;
		open_log_file_cache max=1000 inactive=10s min_size=2 valid=1m#记录日志缓存
		
		location / { 
			proxy_cache cache_one;
			proxy_cache_min_uses 3;
			proxy_cache_revalidate on;
			proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
			proxy_cache_key "$host$request_method$uri$is_args$args";
			proxy_cache_valid  200 304 12h;
			proxy_cache_valid  301 302 1m;
			proxy_cache_valid  any 1m;
			proxy_pass http://mysvr;
			proxy_redirect off;
			proxy_next_upstream http_503 http_500 http_502 error timeout invalid_header;
			proxy_set_header Host  $host;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			add_header  N-Cache "$upstream_cache_status From $host";
			log_not_found off;
			expires 1d;
		}
		
		location = /access {
			access_by_lua_block {
     
				--ngx.say(ngx.var.header_Accept);
				ngx.exec("/test");
				
			}
		 }
		 
		 
		location = /test {
			default_type 'text/plain';
			resolver 192.168.177.255;
			
			set $cache_key "1234567890";
			
			
			
			content_by_lua_file conf/test.lua;
		}
		 
		##获取post过来的参数,get过来的参数,获取header
		location /form {  
			set_form_input $name;
			content_by_lua '  
				local say = ngx.say;
				local header_Content_type = ngx.req.get_headers()["Cache-Control"]
				if not header_Content_type then
					say("error");
				end
				say(header_Content_type);
				local name = ngx.var.name;
				say(name);
				local age = ngx.var.arg_age;
				say(age);
			';  
		}
		
		
		# rewrite
		location /face {
			#rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
			rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg break;
			proxy_pass http://192.168.149.90/api/rec/list?name=123;

		}
		

        # 定义错误提示页面
        error_page   500 502 503 504 /50x.html; 
            location = /50x.html {
            root   /root;
        }

        #静态文件,nginx自己处理
        location ^~ /(images|static)/ {
            root /var/www/virtual/htdocs;
            #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
            expires 30d;
        }
		
		location ~* \.(gif|jpg|jpeg)$ {
			root /var/www/virtual/htdocs;
            #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
            expires 30d;
		}
		
        #设定查看Nginx状态的地址
        location /NginxStatus {
            stub_status            on;
            access_log              on;
            auth_basic              "NginxStatus";
            auth_basic_user_file  conf/htpasswd;
        }
		
		location / {
			deny  192.168.1.1;
			allow 192.168.1.0/24;
			allow 10.1.1.0/16;
			allow 2001:0db8::/32;
			deny  all;
		}
		
		location /face {
			rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;

		}
		
        #禁止访问 .htxxx 文件
        location ~ /\.ht {
            deny all;
        }
        
        }
    }

猜你喜欢

转载自liyonghui160com.iteye.com/blog/2354645