• Nginx


    1 Nginx

    engin X: nginx

    官方站点:http://nginx.org/

    1.1 类似的产品

    二次开发版:淘宝正在使用的tengine, openresty

    1.2 Nginx的特性:

    • 模块化设计,较好的扩展性;
    • 高可靠性
      • master/worker
    • 支持热部署
      • 不停机更新配置文件、更换日志文件、更新服务器程序版本;
    • 低内存消耗
      • 10000个keep-alive连接模式下的非活动连接仅消耗2.5M内存;
    • 支持事件驱动event-driven, 异步aio, 内存映射mmap

    1.3 正向代理与反向代理

    • 正向代理
      正向代理指的是位于客户端前面的一个替客户端去请求做某事的服务器,只面向于客户端
    • 反向代理
      反向代理指的是位于服务器前面的一个完成客户端请求响应的服务器,只面向于服务器,因此客户端不会真正的知道真实服务器是哪一个

    1.4 nginx基本功能:

    • 静态资源的web服务器;
    • http协议反向代理服务器;
    • pop3/imap4协议反射代理服务器;
    • FastCGI(lnmp), uWSGI等协议;
    • 模块化(非DSO),著名有zip, SSL, ...;

    1.5 web服务器相关的功能:

    • 虚拟主机
    • keepalive
    • 访问日志
    • url rewrite
    • 路径别名
    • 基于ip及用户的访问控制
    • 支付速率限制及并发数限制
    • ...;

    1.6 Nginx的程序架构:

    master/worker
    一个master进程,可生成一个或多个worker进程;

    • master:加载配置文件、管理worker进程、平滑升级、...
    • worker:http服务、http代理、fastcgi代理、...

    1.7 模块类型:

    • 核心模块: core module
    • 标准模块:
      • Standard HTTP modules
      • Optional HTTP modules
      • Mail modules
    • 3rd party modules:

    1.8 nginx用来做什么?

    • 静态资源的web服务器
    • http协议反向代理

    1.9 nginx的安装:

    # yum -y install pcre-devel openssl-devel zlib-devel
    # ./configure 
    	--prefix=/usr/local
    	--sbin-path=/usr/sbin/nginx
    	--conf-path=/etc/nginx/nginx.conf
    	--error-log-path=/var/log/nginx/error.log
    	--http-log-path=/var/log/nginx/access.log
    	--pid-path=/var/run/nginx.pid
    	--lock-path=/var/run/nginx.lock
    	--http-client-body-temp-path=/var/cache/nginx/client_temp
    	--http-proxy-temp-path=/var/cache/nginx/proxy_temp
    	--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
    	--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
    	--http-scgi-temp-path=/var/cache/nginx/scgi_temp
    	--user=nginx
    	--group=nginx
    	--with-http_ssl_module
    	--with-http_realip_module
    	--with-http_addition_module
    	--with-http_sub_module
    	--with-http_dav_module
    	--with-http_flv_module
    	--with-http_mp4_module
    	--with-http_gunzip_module
    	--with-http_gzip_static_module
    	--with-http_random_index_module
    	--with-http_secure_link_module
    	--with-http_stub_status_module
    	--with-http_auth_request_module
    	--with-threads
    	--with-stream
    	--with-stream_ssl_module
    	--with-http_slice_module
    	--with-file-aio
    	--with-http_v2_module
    # make && make install
    

    2 nginx相关的配置

    配置文件的位置:/etc/nginx/nginx.conf,以及在nginx.conf中includ所定义的配置文件的位置,如include conf.d/*.conf,有些还包含mime.types

    在这些指令中包含了facscgi,scgi,uwscgi的相关配置

    2.1 配置指令

    注意nginx配置指令必须要以分号结尾

    指令的格式为:directive value1 [value2...];

    	支持使用变量:
    		内置变量:由模块引入,可直接引用;
    		自定义变量:set  variable_name  value;
    			
    			引用变量:$variable_name
    			
    配置文件结构:
    	main block:全局配置;
    	event {
    		...
    	}:事件驱动的相关配置;
    	http {
    		...
    	}:http协议的相关配置
    	mail {
    		...
    	}:mail相关的配置;
    
    	http相关的配置:
    		http {
    			...
    			...
    			server {
    				...
    				server_name
    				root
    				alias 
    				location /uri/ {
    					...
    				}
    				...
    			}
    			server {
    				...
    				...
    			}
    		}
    		
    main block:
    	配置指令的类别:
    		正常运行必备的配置;
    		优化性能的配置;
    		用于调试、定位问题的配置;
    		
    	正常运行必备的配置:
    		1、user  USERNAME  [GROUPNAME];
    			指定用于运行worker进程的用户和组;
    			
    			user  nginx  nginx;
    			
    		2、pid  /PATH/TO/PID_FILE;
    			指定nginx进程的pid文件路径;
    			
    			pid  /var/run/nginx.pid;
    			
    		3、worker_rlimit_nofile number;
    			单个worker进程所能够打开的最大文件数;
    			
    	性能优化相关的配置:
    		1、	worker_processes number | auto;
    			worker的进程数;通常应该为CPU的核心数减1;
    			
    		2、worker_cpu_affinity cpumask ...;
    		      worker_cpu_affinity auto [cpumask]; 
    		      
    		      CPUMASK:
    				0000 0001
    				0000 0010
    				0000 0100
    				0000 1000
    				0001 0000
    				...
    		3、worker_priority nice;
    			[-20,19]
    				100-139
    				
    	调试、定位问题:
    		1、daemon on | off;
    			是否以守护进程方式启动nginx进程;
    			
    		2、master_process on | off;
    			是否以master/worker模型启动nignx进程;
    			
    		3、error_log file | stderr | syslog:server=address[,parameter=value] | memory:size [debug | info | notice | warn | error | crit | alert | emerg];
    			错误日志文件的记录方式,及其日志级别:
    				方式:
    					file  /PATH/TO/SOME_LOG_FILE;
    					stderr:发送到错误输出;
    					 syslog:server=address[,parameter=value]:发送给syslog服务器;
    					 memory:size
    					 
    				日志级别:
    					debug依赖于configure时的--with-debug选项;
    

    回顾:IO模型、nginx

    IO模型:
    阻塞式
    非阻塞式
    IO复用(select, poll)
    信号驱动的IO(epoll, kqueue, /dev/poll)
    AIO

    阶段:等待数据准备完成,复制数据(从内核缓冲区到进程的地址空间)
    

    nginx:master/worker
    master
    worker()
    cache loader
    cache manager

    模块类别:核心模块、标准模块(http标准模块,http可选模块,mail模块)、3rd模块
    

    nginx.conf
    main block
    event {
    ...
    }
    http {
    ...
    server {
    ...
    server_name
    root
    location /uri/ {
    ...
    }
    }
    server {
    ...
    }
    }

    Nginx(2)

    nginx.conf:
    main block
    events {
    ...
    }

    	1、worker_connections number;
    		每个worker进程所能够并发打开的最大连接数;
    		
    		worker_processes * worker_connections
    		
    	2、use method;
    		指明并发连接请求处理时使用的方法;
    		
    		use  epoll;
    		
    	3、accept_mutex on | off;
    		启用时,表示用于让多个worker轮流地、序列化地响应新请求;
    		
    http {
    	...
    }
    
    定义套接字相关功能
    
    	1、server { ... }
    		配置一个虚拟主机;
    		
    		server {
    			listen PORT;
    			server_name  HOSTNAME; 
    			root /PATH/TO/DOCUMENTROOT;
    			...
    		}
    		
    		注意:
    			(1) 基于port的虚拟主机:
    				listen指令要使用不同的端口;
    			(2) 基于Hostname的虚拟主机;
    				server_name指令指向不同的主机名;
    			(3) 基于ip的虚拟主机:
    				listen IP:PORT;
    		
    	2、listen address[:port] [default_server] [ssl] [backlog=number] [rcvbuf=size] [sndbuf=size];
    	      listen port [default_server] [ssl];
    	      listen unix:path [default_server] [ssl] ;
    	      
    		default_server:默认虚拟主机;
    		ssl:限制只能通过ssl连接提供服务;
    		backlog:后缓队列的长度;
    		rcvbuf:接收缓冲大小;
    		sndbuf:发送缓冲区大小;
    		
    	3、server_name name ...;
    		指明当前server的主机名;后可跟一个或空白字符分隔的多个主机;
    		支持使用*任意长度的任意字符;
    		支持~起始的正则表达式模式字符串;
    		
    		应用策略:
    			(1) 首先精确匹配;
    			(2) 左则*通配符匹配;
    			(3) 右侧*通配符匹配;
    			(4) 正则表达式模式匹配;
    														
    			server_name  www.magedu.com;
    			
    			server_name *.magedu.com;
    			
    			server_name  www.magedu.*;
    			
    			server_name ~^.*.magedu..*$;
    			
    			mail.magedu.com, www.magedu.com
    			
    	4、tcp_nodelay  on|off;
    		对keepalived模式下的连接是否启用TCP_NODELAY选项;
    		
    	5、sendfile on | off;
    		是否启用sendfile功能;
    		
    
    定义路径相关配置
    	6、root path;
    		设置web资源路径映射;用于指明用户请求的url所对应的本地文件系统上的文档所在目录路径;
    		可用上下文:http, server, location, if
    		
    	7、location [ = | ~ | ~* | ^~ ] uri { ... }
    	      location @name { ... }
    	      
    	      根据用户请求的URI来匹配定义的location,匹配到时,此请求将被相应的location块中的指令所处理;
    	      
    		server {
    			...
    			location {
    				
    			}
    			location {
    				...
    			}
    		}
    		
    		=:URI精确匹配;
    		~:做正则表达式模式匹配,区分字符大小写;
    		~*:做正则表达式模式匹配,不区分字符大小写;
    		^~:对URI的左半部分做匹配检查,不区分字符大小写;
    		
    		匹配优先级:=、^~、~/~*、不带符号;
    		
    	8、alias path;
    		定义路径别名,文档映射的一种机制;仅能用于location上下文;
    		
    		alias  /bbs/  /web/forum/
    
    		http://www.magedu.com/bbs/a.jpg		
    			
    			location  /bbs/  {
    				alias  /web/forum/;
    			}
    			
    			/web/forum/a.jpg
    		
    			location  /bbs/  {
    				root  /web/forum/;
    			}
    			
    			/web/forum/bbs/a.jpg
    		
    		
    		注意:
    			root指令:给定的路径对应于location中的/uri/左侧的/;
    			alias指令:给定的路径对应于location中的/uri/右侧的/;
    		
    	9、index file ...;
    		可用位置:http, server, location
    		
    		默认主面;
    		
    	10、error_page code ... [=[response]] uri;
    		根据用户请求的资源的http响应的状态码实现错误页重定向;
    		
    		http://www.magedu.com/hello.html --> 因为资源不存在而被改为对
    			http://www.magedu.com/404.html
    			
    	11、
    	
    	
    
    定义客户端请求的相关配置
    
    	12、keepalive_timeout timeout [header_timeout];
    		设定保持连接的超时时长,0表示禁止长连接 ;默认为75s;
    		
    	13、keepalive_requests number;
    		在一次长连接上所允许请求的资源的最大数量,默认为100;
    		
    	14、keepalive_disable none | browser ...;
    		对哪种浏览器禁用长连接;
    		
    	15、send_timeout time;
    		向客户端发送响应报文的超时时长; 特别地,是指两次写操作之间的间隔时长; 
    	
    	16、client_body_buffer_size size;
    		用于接收客户端请求报文的body部分的缓冲区大小;默认为16k;超时此大小时,其将被暂存到磁盘上;
    		
    	17、client_body_temp_path path [level1 [level2 [level3]]];
    		设定用于存储客户端请求报文的body部分的临时存储路径及子目录结构和数量;
    		
    		/var/tmp/body  2 1 2
    			00-ff
    	
    
    对客户的请求进行限制的相关配置:
    	18、limit_rate rate;
    		限制响应给客户端的传输速率,单位是bytes/second,0表示无限制;
    		
    	19、limit_except method ... { ... };
    		限制对指定的请求方法之外的其它方法的使用客户端;
    		
    		limit_except GET POST {
    			allow  172.18.0.0/16;
    			deny all;
    		}
    		
    		表示除了GET和POST之外的其它方法仅允许172.18.0.0/16中的主机使用;
    
    文件操作优化的配置:
    	20、aio on | off | threads[=pool];
    		是否启用aio功能;
    		
    	21、directio size | off;
    		
    	22、open_file_cache off;
    		open_file_cache max=N [inactive=time];
    			nginx可以缓存以下三种信息:
    				(1) 文件的描述符、文件大小和最近一次的修改时间;
    				(2) 打开的目录的结构;
    				(3) 没有找到的或者没有权限访问的文件的相关信息;
    				
    			max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现缓存管理;
    			
    			inactive=time:缓存项的超时时长,在此处指定的时长内未被命中的缓存项即为非活动项;
    			
    	23、open_file_cache_errors on | off;
    		是否缓存查找时发生错误的文件一类的信息;
    		
    	24、open_file_cache_min_uses number;
    		在open_file_cache指令的inactive参数指定的时长内,至少命中此处指定的次数方可不被归类到非活动项;
    		
    	25、open_file_cache_valid time;
    		缓存项有效性的检查频率;默认是60s;
    		
    ngx_http_access_module模块:
    	实现基于ip的访问控制功能;
    	
    	26、allow address | CIDR | unix: | all;
    	27、deny address | CIDR | unix: | all;
    	
    	可用上下文:http, server, location, limit_except
    
    ngx_http_auth_basic_module模块:
    	28、auth_basic string | off;
    		使用basic机制进行用户认证;
    		
    	29、auth_basic_user_file file;
    		认证用的账号密码文件;
    		
    		文件格式:
    			name:password:commet
    			
    		密码格式:
    			htpasswd命令;
    			
    			location /admin/ {
    			auth_basic "Admin Area";
    			auth_basic_user_file /etc/nginx/.ngxpasswd;
    			}
    		
    ngx_http_stub_status_module模块:
    	用于输出nginx的基本状态信息;
    	
    	Active connections: 1 
    	server accepts handled requests
    	155 155 298 
    	Reading: 0 Writing: 1 Waiting: 0 		
    	
    	Active connections:  处于活动状态的客户端连接的数量;
    	accepts:已经接受的客户端请求的总数;
    	handled:已经处理完成的客户端请求的总数;
    	requests:客户端发来的总的请求数;
    	Reading:处于读取客户端请求报文首部的连接数;
    	Writing:处于向客户端发送响应报文过程中的连接数;
    	Waiting:处于等待客户端发出请求的空闲连接数;
    	
    ngx_http_referer_module模块:
    	The ngx_http_referer_module module is used to block access to a site for requests with invalid values in the “Referer” header field.
    	
    	30、valid_referers none | blocked | server_names | string ...;
    		定义合法的referer数据;
    		
    		none:请求报文首部没有referer首部;
    		blocked:请求报文的referer首部没有值;
    		server_names:其值是主机名;
    		arbitrary string:直接字符串,可以使用*作为通配符;
    		regular expression:被指定的正则表达式模式匹配到的字符串;要使用~起始;
    		
    		valid_referers none blocked server_names *.magedu.com magedu.* ~.magedu.;
    		
    		if ($invalid_referer) {
    			return 403;
    		}
    

    回顾:
    master/worker

    nginx.conf:
    main block
    events {
    ...
    }
    http {
    ...
    server {
    ...
    listen
    server_name
    location [=|^||~*] /uri/ {
    root
    ...
    }
    ...
    }

    Nginx(3)

    ngx_http_ssl_module

    ssl on | off;
    	是否启用当前虚拟主机的ssl功能;
    	
    ssl_certificate file;
    	当前虚拟主机使用的PEM格式的证书文件;
    	
    ssl_certificate_key file;
    	当前虚拟机使用的证书文件中的公钥配对儿的私钥文件路径,PEM格式;
    	
    ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
    	SSL协议的版本;
    	
    ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
    	指明ssl会话的缓存机制;
    		builtin:使用openssl内建的缓存机制,此为各worker独有;
    		shared:由各worker共享的缓存;
    			name:缓存空间的名称;
    			size:字节为单位的缓存空间的大小;每1MB内存空间可缓存4000个会话;
    			
    ssl_session_timeout time;
    	ssl会话超时时长,指ssl session cache中缓存条目有效时长; 
    

    ngx_http_log_module

    The ngx_http_log_module module writes request logs in the specified format.
    
    access_log path [format [buffer=size [flush=time]] [if=condition]];
    access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition];
    access_log syslog:server=address[,parameter=value] [format [if=condition]];
    access_log off;
    
    log_format name string ...;
    	
    open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
    open_log_file_cache off;
    
    	max:最大缓存条目;
    	inactive=time:非活动时长; 
    	min_uses:最少使用次数;
    	valid:验正缓存条目有效性的频率;
    

    ngx_http_rewrite_module

    rewrite regex replacement [flag];
    	把用户请求的URI基于regex做检查,匹配到时将替换为replacement指定的字符串;			
    	在同一个location中存在的多个rewrite规则会自上而下逐个被检查(循环);可以使用flag控制此循环功能;
    	如果replacement是以http://或https://开头,则替换结果会直接以重定向方式返回给客户端;
    	
    	[flag]:
    		last:重写完成后停止对当前uri在当前location中的后续其它重写操作,改为对新uri的新一轮处理;
    		break:重写完成后停止对当前uri在当前location中的后续其它重写操作; 
    		redirect:重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,由客户对新URL进行请求;(302)
    		permanent:重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户对新URL进行请求;(301)
    		
    rewrite_log on | off;
    	是否启用重写日志;启用时,日志信息被发往错误日志;
    	
    if (condition) { ... }:
    	条件判断机制,在条件满足时,执行配置块中的配置;
    	引入了一个新的配置上下文;
    	
    	condition:
    		比较表达式:
    			==, !=
    			~:模式匹配,区分字母大小写;
    			~*:模式匹配,不区分字符大小写;
    			!~:模式不匹配,区分字母大小写;
    			!~*:模式不匹配,不区分字母大小写;
    		文件及目录存在性判断:
    			-f, !-f:文件
    			-d, !-d:目录
    			-e, !-e:存在
    			-x, !-x:执行
    			
    return:
    	return code [text];
    	return code URL;
    	return URL;
    	
    set $variable value;
    	用户自定义变量;
    

    ngx_http_gzip_module

    过滤器,对指定类型的资源压缩传输以节约带宽;
    
    gzip on | off;
    	启用或禁用gzip压缩响应报文;
    gzip_comp_level level;
    	指定压缩比,1-9,默认为1;
    gzip_disable regex ...;
    	regex是匹配客户端浏览器类型的模式,表示对所有匹配到的浏览器不执行压缩响应;
    gzip_min_length length;
    	触发启用压缩功能的响应报文的最小长度;
    gzip_http_version 1.0 | 1.1;
    	设定启用压缩响应功能时,协议的最小版本;
    gzip_types mime-type ...;
    	指定仅执行压缩的资源内容类型;默认为text/html;
    gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
    	对代理的请求基于何种属性判断其是否应该启用压缩功能;
    	
    示例:
    	gzip on;
    	gzip_http_version 1.0;
    	gzip_comp_level 6;
    	gzip_disable msie6;
    	gzip_min_length 2;
    	gzip_types text/plain text/css text/xml application/x-javascript  application/xml  application/json application/java-script;
    

    ngx_http_fastcgi_module

    LAMP:
    	httpd+php:
    		modules
    		cgi
    		fastcgi
    			proxy_fastcgi_module
    	
    LNMP:
    	nginx+php:
    		fastcgi
    		
    	php:编译时,支持fpm;
    		./configure   ...  --enable-fpm ...
    		
    		php-fpm工作方式(类似于httpd的prefork):
    			listen = 127.0.0.1:9000
    			listen.allowed_clients = 127.0.0.1
    			pm = dynamic | static 
    				pm.start_servers:启动fpm进程时启动的工作进程数量;
    				pm.min_spare_servers:最少空闲进程数;
    				pm.max_spare_servers:最大空闲进程数;
    				pm.max_children:最大工作进程数;
    			user = USERNAME
    			group = GROUPNAME
    			
    	fastcgi模块指令:
    		
    			fastcgi_pass address;
    				address是fpm服务器监听的地址和端口;
    				
    				示例: 	fastcgi   127.0.0.1:9000; 
    			
    			fastcgi_index name;
    				fastcgi应用的主页名称;
    				
    			fastcgi_param parameter value [if_not_empty];
    				传递给fpm服务器的参数及其值; 
    				
    			fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
    				path:文件系统路径,用于存储缓存的文件数据;
    				max_size=size:定义此路径下的多大空间用于存储缓存数据;
    				levels=#[:#[:#]]:缓存目录层级定义;
    					levels=1:2
    				keys_zone=name:size
    					内存中用于缓存k/v映射关系的空间名称及大小;
    				inactive=time
    				
    				注意:只能定义在http上下文 ;
    				
    			fastcgi_cache zone | off;
    				是否启用cache,如果启用,数据缓存于哪个cache中;
    				
    			fastcgi_cache_key string;
    				定义要使用的缓存键;
    				
    				例如: fastcgi_cache_key  $request_uri;
    				
    			fastcgi_cache_methods GET | HEAD | POST ...;
    				缓存哪些类型的请求的相关数据;
    				
    			fastcgi_cache_min_uses number;
    				
    			fastcgi_cache_valid [code ...] time;
    				对不同响应码设定其可缓存时长; 
    				
    			注意:调用缓存时,至少应该 指定三个参数
    				fastcgi_cache
    				fastcgi_cache_key 
    				fastcgi_cache_valid  
    

    Nginx(5)

    ngx_http_proxy_module

    (1) proxy_pass URL;
    	location, if in location, limit_except
    	
    	proxy_pass后面的路径不带uri时,其会将location的uri传递给后端主机;
    	
    		location  /uri/  {
    			proxy_pass  http://HOST; 
    		}
    		
    	proxy_pass后面路径是一个uri时,其会将location的uri替换为proxy_pass后端主机的uri;
    	
    		location  /uri/  {
    			proxy_pass  http://HOST/new_uri/;
    		}
    		
    	如果location定义其uri时使用了正则表达模式匹配机制,则proxy_pass后的路径必须不能使用uri;
    		location  ~|~*  PATTERN {
    			proxy_pass  http://HOST;
    		}
    	
    		
    		http://www.magedu.com/bbs/  -->  http://172.16.100.7/bbs/
    		http://www.magedu.com/bbs/  -->  http://172.16.100.7/
    				
    (2) 	proxy_set_header
    		proxy_set_header field value;
    		设定向后端主机发送的请求报文的首部及其值; 
    		
    	示例:
    		proxy_set_header  X-Real-IP  $remote_addr;
    		proxy_set_header  X-Forwarded-For $proxy_add_x_forwared_for;
    		
    				
    缓存相关的选项(缓存要先定义,后调用)
    (3) proxy_cache_path path [levels=levels]  keys_zone=name:size [inactive=time] [max_size=size] ;
    	用于http上下文 ;
    	
    (4) proxy_cache zone | off;
    	调用缓存,默认为off;
    	
    (5) proxy_cache_key string;
    	定义缓存键;
    		proxy_cache_key  $request_uri
    		proxy_cache_key  $scheme$proxy_host$request_uri
    		
    (6) proxy_cache_valid [code ...] time;
    	为不同的响应码设定其缓存的时长; 
    	
    	示例:
    		proxy_cache_valid  200 302  10m;
    		proxy_cache_valid  404 1m;
    		
    (7) proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...;
    	Determines in which cases a stale cached response can be used when an error occurs during communication with the proxied server. 
    

    回顾:
    nginx:proxy
    正向代理
    反向代理

    ngx_http_proxy_module:
    proxy_pass
    proxy_set_header
    proxy_cache_path (http)
    proxy_cache
    proxy_cache_key
    proxy_cache_valid
    proxy_cache_use_stale

    Nginx(6)

    ngx_http_proxy_module模块

    跟连接相关的指令
    	(8) proxy_connect_timeout time;
    		与后端服务器建立连接的超时时长,默认为60s,最长为75s;
    		
    	(9) proxy_read_timeout time;
    		等待后端主机发送响应报文的超时时长,默认为60s;
    		
    	(10) proxy_send_timeout time;
    		向后端服务发送请求报文的超时时长,默认为60s;
    

    ngx_http_headers_module
    用于在响应给客户端的报文中添加首部;

    (1) add_header name value [always];
    	向响应报文添加自定义首部,并赋值;			
    	http, server, location, if in location
    	
    	add_header  X-Via  $server_addr;
    	
    (2) expires [modified] time;
    	expires epoch | max | off;
    	
    	用于添加Expire及Cache-Control首部或修改首部的值;
    

    ngx_http_upstream_module
    将多个后端主机定义为服务器组,而后可由proxy_pass, fastcgi_pass, memcached_pass等进行引用;

    (1) upstream name { ... }
    	定义后端服务器组;引入新的上下文;只能用于http上下文;
    	name:名称,直接字符串;
    	
    (2) server
    	server address [parameters];
    	
    	定义服务器的地址和相关的参数;
    		地址格式:
    			IP[:port]
    			HOSTNAME[:port]
    			unix:/PATH/TO/SOME_SOCK_FILE
    			
    		参数:
    			weight=number:服务器权重;
    			max_fails=number:最大失败尝试次数;
    			fail_timeout=time:设置服务器不可用超时时长; 
    			backup:备用主机;
    			down:手动标记其不再处理任何用户请求; 
    			
    (3) ip_hash
    	源地址哈希调度算法;
    	
    (4) least_conn
    	最少连接调度算法;
    	
    (5) health_check [parameters];
    	定义后端主机的健康状态检测机制;只能用于location上下文;
    	
    	可用参数:
    		interval=#:检测的频度,默认为5秒;
    		fails=number:判定为失败的检测次数;
    		passes=number:判定为成功的检测次数;
    		uri=uri:执行健康状态检测时请求的uri;
    		match=name:基于哪个match做检测结果为“成功”或“失败”的判定;
    		port=number:向服务器的哪个端口发起健康状态检测请求;
    		
    (6) match name { ... }
    	仅能用于http上下文 ;对后端主机做健康状态检测时,定义其结果判断标准;
    	
    	专用指令:
    		status:期望的响应码;
    			status  CODE
    			status  ! CODE
    			status CODE-CODE 
    		header:基于响应首部进行判断
    			header HEADER=VALUE
    			header HEADER!=VALUE
    			header [!] HEADER
    			header HEADER ~ VALUE
    		body:期望的响应报文的主体部分应该有的内容;
    			body ~ "CONTENT"
    			body !~ "CONTENT"
    			
    (7) hash key [consistent];
    	定义调度方法,可自定义基于何种信息(key)进行绑定;
    	
    	hash $remote_addr
    	hash $request_uri
    	hash $cookie_username
    	
    补充:内置变量的调用,向客户端展示缓存命令与否:
    	add_header  X-Cache  $upstream_cache_status;
  • 相关阅读:
    P2788 数学1(math1)- 加减算式
    数据库第三章-学习笔记
    字典序
    P1739 表达式括号匹配
    P3742 umi的函数
    P1765 手机
    P2192 HXY玩卡片
    全排函数c++ next_permutation()
    11.css定义下拉菜单
    10.php引用(&)详解及注意事项
  • 原文地址:https://www.cnblogs.com/xuelong3/p/7775249.html
Copyright © 2020-2023  润新知