Nginx配置详解

Nginx初识

  • master:配置文件分析和加载、管理worker、平滑升级;

  • worker:处理用户请求;

  • cache loader, cache manager:缓存加载和缓存管理

  • nginx的其它的二次发行版:tengine、OpenResty

  • Nginx模块:

    • 核心模块:core module
    • 标准模块:
      • HTTP modules:
        • Standard HTTP modules
        • Optional HTTP modules
      • Mail modules
      • Stream modules:传输层代理
  • 负载均衡比较:

    1. 传输层:lvs、nginx、haproxy
    2. 应用层:nginx(http, https, smtp, pop, imap), haproxy(http), httpd(http/https), ats, perlbal, pound, …
  • Nginx作用:

    1. 静态的web资源服务器;(图片服务器,或js/css/html/txt等静态资源服务器)
    2. 结合FastCGI/uwSGI/SCGI等协议反代动态资源请求;
    3. http/https协议的反向代理;
    4. IMAP4/pop3协议的反向代理;
    5. tcp/udp协议的请求转发;
  • Nginx配置文件:

    # 用户
    user  www www;
    # 启动进程数
    worker_processes  auto;
    # 错误日志
    error_log  logs/error.log;
    # pid文件
    pid        logs/nginx.pid;
    # 进程绑定CPU
    worker_cpu_affinity auto;
    # 设置优先级
    worker_priority -5;
    # 允许打开的最大文件数
    worker_rlimit_nofile 65535;
    
    # 最大链接数
    events {
        worker_connections  1024;
    }
    
    
    http {
    	# 各个server共享的配置
    	# mime信息,包含哪些mime协议
        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"';
        # 配置文件目录
        include	/etc/nginx/conf.d/*.conf;
        # 日志路径
        access_log	/var/log/nginx/logs/access.log;
        error_log	/var/log/nginx/logs/error.log;
        # 控制网络连接参数:
        # 发送文件
        sendfile        on;
        #tcp_nopush     on;
        # 保持连接超时65s
        keepalive_timeout  65;
    
        #gzip  on;
    
        upstream helpdesk {
            server unix:///data/helpdesk-master/desk_demo.sock;
        }
    
        # 虚拟主机
        server {
            listen       80;
            # 默认虚拟主机,当没有匹配项是使用
            # listen       80 default_server;
            # 主机名
            server_name  10.126.3.193;
            # 默认网页跟路径
            root         /usr/share/nginx/html;
    
            #charset koi8-r;
            charset utf-8;
    
            # max upload size
            client_max_body_size 75M;
            
            # Django media
            location /media  {
                alias /data/helpdesk-master/media;  # 指向django的media目录
            }
            
            location /static {
                alias /data/helpdesk-master/static; # 指向django的static目录
            }
            
            # Finally, send all non-media requests to the Django server.
            location / {
                uwsgi_pass  helpdesk;
                include     uwsgi_params; # the uwsgi_params file you installed
            }
    
            # 自定义错误页
            error_page  404              /404.html;
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    
    }
    
    
    • 详细配置说明:
      • 每项配置以分号;结尾

      • main配置段常见的配置指令:
        分类:
        1. 正常运行必备的配置
        1) user user [group];
        2) pid /PATH/TO/PID_FILE; # 指定存储nginx主进程号码的文件路径
        3) include file | mask; # 指明包含进来的其他配置文件片段
        4) load_module file; # 指明要装载的动态模块
        2. 优化性能相关的配置
        1) worker_processes number | auto;
        ###### number:worker进程的数量,通常应该 <= 当前主机的CPU的物理核心数;auto:当前主机物理CPU核心数;
        2) worker_cpu_affinity auto | cpumask;
        ###### nginx进程与CPU绑定,如果不绑定进程使用的CPU会随着访问进行跳转。auto:自动;cpumask:CPU位掩码(00000001表示0号CPU,00000010表示1号CPU)
        ###### 查看nginx运行在第几个CPU:ps axo comm,pid,psr | grep nginx
        3) worker_prilrity number; # 指定worker进程的nice值,设定worker进程优先级;[-20,20]
        4) worker_rlimit_nofile number; # worker进程所能够打开的文件数量上限
        3. 用于调试及定位问题相关的配置
        1.) daemon on|off; # 是否以守护进程方式运行nginx
        2.) master_process on|off; # 是否以master/worker模型运行nginx;默认为on
        3.) error_log file [level]; # 错误日志位置
        4. 事件驱动相关的配置
        events { ... }
        1.) worker_connections number; # 每个worker进程所能够打开的最大并发连接数(worker_process * worker_connections)
        2.) use method; # method: select/epoll 指明并发连接请求的处理方法。
        3.) accept_mutex on|off; # 处理新的连接请求的方法;on意味着由各worker轮流处理新请求,off意味着每个新请求到达都会通知所有worker进程,谁先抢到谁执行。

      • http协议的相关配置:

        	http {
        		...
        		server {
        			...
        			server_name
        			root
        			location [OPERATOR] /URL/ {
        				...
        			}
        		}
        		server {
        			...
        		}
        	}
        
        • 与套接字相关的配置:

          1. server {…} # 配置一个虚拟主机
          server {
          	listen address[:PORT]|PORT;
          	server_name SERVER_NAME;
          	#root /PATH/TO/DOCUMENT_ROOT;      # 本地文件路径
          	#proxy_pass http://192.168.33.6;   # 反代
          }
          
          1. listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE
            listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
            基于端口
            default_server: 设定为默认虚拟主机;
            ssl: 限制仅能够通过ssl连接提供服务;
            backlog=number: 后援队列长度;
            rcvbuf=size: 接收缓冲区大小;
            sndbuf=size: 发送缓冲区大小;
          2. server_name name;
            指明虚拟主机的主机名;可使用由空白字符分割的字符串;支持*统配任意长度的任意字符;(eg: server_name .fone.com www.fone.)支持~其实的字符做正则表达式模式匹配;(eg: server_name ~^www\d+.fone.com$)
            • 匹配机制:
              1. 首先是字符串精确匹配;
              2. 左侧*通配符;
              3. 右侧*通配符;
              4. 正则表达式;
          3. tcp_nodelay on|off; # 在keepalived模式下的连接是佛启用TCP_NODELAY选项;启用时将小报文合并发送(延迟到达);
          4. keepalive_timeout time; # 保持连接的超时时间,默认75s
          5. keepalive_requests number; # 保持连接时最多请求多少资源,默认100
          6. types_hash_max_size 2048; # 设定保存类型哈希表的最大值
          7. sendfile on|off; # 是否启用sendfile功能;
          8. tcp_nopush on|off; # 在sendfile模式下,是否启用TCP_CORK选项;是否等待应用层合并起来一并发送
        • 与路径相关的配置:

          1. root path; # 设置web资源路径映射;用于指明用户请求的URL锁对应的本地文件系统上的文档所在目录路径;可用位置:http、server、location、if in location
          2. location [=|~|~*|^~] url {...}
            在一个server中location配置段可存在多个,用于实现从URL到文件系统的路径映射;nginx会根据用户请求的uri来检查定义的所有location,并找出一个最佳匹配应用其配置
            = : 对URI做精确匹配;
            location = / {...}
            
            ~:对uri做正则表达式模式匹配,区分字符大小写;
            ~*:对uri做正则表达式模式匹配,不区分字符大小写;
            ^~:对uri左半部分做匹配,不区分字符大小写;
            不带符号:匹配起于次URL的所有url;
            匹配优先级:=、^ / ~*、不带符号。匹配长度越长优先级越高
          3. alias path;
            定义路径别名,文档映射的另一种机制;仅用于location上下文;
            注意:location中使用root指令和alias指令的意义不同;
            (a) root: 给定的路径对应于location中的/uri/左侧的/
            (b) alias: 指定的路径对应于location中的/uri/右侧的/
          4. index file …; # 默认资源:http、server、location
          5. error_page code [=[response]] url; # 配置错误页
            error_page 404 =200 /notfound.html;
            location = /notfound.html {
            	root /data/nginx/error_pages;
            }
            
          6. try_files file … url;
        • 定义客户端请求的相关配置

          1. keepalive_timeout time [header_timeout]; # 设置保持连接的超时时长,0表示禁止长连接,默认为75s
          2. keepalive_requests number; # 再一次长连接上所允许请求的资源的最大数量,默认为100
          3. keepalive_disable none | browser …; # 对特定浏览器禁用长连接
          4. send_timeout time; # 向客户端发送响应报文的超时时长,指两次写操作之间的间隔时长
          5. client_body_buffer_size size; # 用于接收客户端请求报文的body部分的缓冲区大小,默认为16k;超出部分将被暂存到磁盘上client_body_temp_path指定路径处
          6. client_body_temp_path path [level1 [level2 [level3]]]; # 设定用于存储客户端请求报文的body部分的临时存储路径及子目录结构和数量
          	client_body_temp_path /var/tmp/client_body 2 1 1
          	2:表示用2位16进制数字表示三级子目录  00-ff
          	1:表示用1位16进制数字表示二级子目录  0-ff
          	1:表示用1位16进制数字表示一级子目录  0-ff
          
        • 对客户端进行限制的配置

          1. limit_rate rate; # 限制响应给客户端的传输速率,单位是bytes/second, 0表示无限制
          2. limit_except method {…} # 限制对指定请求方法之外的其他方法的使用客户端
          	limit_except GET {
          		allow 192.168.33.0/24;
          		deny all;
          	}
          
        • 文件操作优化配置

          1. aio on | off | threads[=pool]; # 是否启用文件系统的aio功能(threads线程池)
          2. directio size | off; # 在Linux主机启用O_DIRECT标记,此处意味着文件大于等于给定的大小时使用,例如directio 4m;
          3. open_file_cache off;
            open_file_cache max=N [inactive=time];
            nginx可以换成以下三种信息:
            1. 文件描述符、文件大小和最近一次的修改时间;
            2. 打开的目录结构;
            3. 没有找到的或者没有权限访问的文件相关信息;
            max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现缓存管理
            inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中或命中的次数少于open_file_cache_min_users指定的次数即为非活动项
          4. open_file_cache_valid time; # 缓存项有效性的检查频率;默认为60s
          5. open_file_cache_min_users number; # 在open_file_cache指令的inactive参数指定的时长内,至少应该被命中多少次才能被归类为活动项
          6. open_file_cache_errors on|off; # 是否缓存查找时发生错误文件的信息
        • 常用模块

          1. ngx_http_access_module模块:实现基于ip的访问控制

            • allow address | CIDR | unix: | all;
            • deny address | CIDR | unix: | all;
          2. ngx_http_auth_basic_module模块:实现基于用户认证访问控制

            • auth_basic string | off;
            • auth_basic_user_file file;
            	location ~* ^/admin {
                    auth_basic "login page";
                    auth_basic_user_file /etc/nginx/.ngxpasswd;
                }
            

            注意:密码文件.ngxpasswd需要先安装httpd-tools,再使用htpasswd -c -m conf/.ngxpasswd username创建(创建完成以后添加用户要去除-c选项)

          3. ngx_http_stub_status_module模块:用于输出nginx的基本状态信息

            • stub_status;
            	输出信息:
            	Active connections: 1 
            	server accepts handled requests
            	 418 418 1999 
            	Reading: 0 Writing: 1 Waiting: 0 
            	说明:
            	Active connections:活动状态的连接数
            	accepts:已经接受的客户端请求的总数
            	handled:已经处理完成得客户端请求总数
            	requests:客户端发来的请求总数
            	Reading:处于读取客户端请求报文首部的连接数
            	Writing:处于项客户端发送响应报文过程中的连接数
            	Waiting:处于等待客户端发来请求的额空闲连接数
            
          4. ngx_http_log_module模块:日志相关

            • log_format name string …; # string可以使用nginx核心模块及其它模块内嵌的变量
            • access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
              access_log off; # 访问日志文件路径,格式及相关的缓冲配置
            • open_log_file_cache max=N [inactive=time] [min_users=N] [valid=time];
              open_log_file_cache off; # 缓存各日志文件相关的元数据信息
              max:缓存的最大文件描述符
              min_users:在inactive指定的时长内访问大于等于此值才能被当做活动项
              inactive:非活动时长
              valid:验证缓存中各缓存项是否为活动项的时间间隔
          5. ngx_http_gzip_module模块:压缩传输

            • gzip on|off;
            • gzip_comp_level level; # 压缩级别[1,9]
            • gzip_disable regex …;
            • gzip_min_length length; # 启用压缩功能的响应报文大小阈值
            • gzip_buffers number size; # 支持实现压缩功能时为其配置的缓冲区数量及每个缓冲区的大小
            • gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | noetag | auth | any …;
              nginx作为代理服务器接收到从被代理服务器发送的响应报文后,在何种条件下启用压缩功能。(off: 对代理的请求不启用;no-cache, no-store, private:表示从被代理服务器收到的响应报文首部的Cache-Control的值为此三者中的任何一个,则启用压缩功能)
            • gzip_types mime-type ...; # 压缩类型,仅对此处设定的MIME类型的内容启用压缩功能,默认为text/html
          6. ngx_http_ssl_module模块:配置SSL主机

            • ssl on|off;
            • ssl_certificate file; # 当前虚拟主机使用PEM格式的证书文件
            • ssl_certificate_key file; # 当前虚拟主机上与其证书匹配的私钥文件
            • ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2]; # 支持ssl协议版本,默认为后三个
            • ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
              [builtin[:size]]:使用OpenSSL内建的缓存,此缓存为每个worker进程私有
              [shared:name:size]:在各worker之间使用一个共享的缓存
            • ssl_session_timeout time; # 客户端一侧的拦截可以复用ssl_session_cache中缓存的ssl参数的有效时间
            	server { 
            		listen 443 ssl;
            		server_name ngx_ssl;
            		root ...;
            		access_log ...;
            
            		ssl on;
            		ssl_certificate ...;
            		ssl_certificate_key ...;
            		ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
            		ssl_session_cache shared:SSL:10m;
            	}
            
          7. ngx_http_rewrite_module模块:实现URL重定向

            • rewrite regex replacement [flag]; # 将用户请求的uri基于regex匹配值进行检查,匹配到时将其替换为replacement指定的新的uri
              注意:如果在同一级配置块中存在多个rewrite规则,那么会自上而下逐个检查;被某个条件规则替换完成后,会重新一轮的替换检查,因此隐含有循环机制;flag标志位用于控制此循环机制。如果replacement是以http://或https://开头,则替换结果会直接以重定向返回给客户端
              flag标志位:
                - last:重写完成后停止对当前uri在当前location中后续的其他重写操作,而后对新的uri启动新一轮的重写检查;提前重启新一轮循环。相当于continue
                - break:重写完成后停止对当前uri在当前location中后续的其他重写操作,而后直接跳转至重写规则配置块之后的其他配置;结束循环;
                - redirect:重写完成后以临时重定向方式直接放回重写后生成的新uri给客户端,有客户端重新发起请求;不能以http://或https://开头;
              
            • permanent:重写完成后以永久重定向方式直接返回重写后生成的新uri给客户端,由客户端重新发起请求
            • return code [text] [URL];
            • rewrite_log on | off; # 是否开启重写日志
            • if (condition) {…} # 引入一个新的配置上下文;条件满足时,执行配置块中的配置指令
            condition中的比较操作符:
            ==、!=、(模式匹配,区分字符大小写)、(模式匹配,不区分大小写)、!(模式不匹配,区分大小写)、!(模式不匹配,不区分大小写)
            文件及目录存在性判断:
            -e, !-e、-f, !-f、-d, !-d、-x, !-x
            • set $variable value; # 用户自定义变量
          8. ngx_http_referer_module模块:防盗链

            • valid_referers none | blocked | server_names | string …; # 定义referer首部的合法可用值
            none:请求报文首部没有referer首部;
            blocked:请求报文的referer首部没有值;
            server_names:参数,其可以有值作为主机名或主机名模式;
            arbitrary_string:直接字符串,但可使用*作通配符;
            regular expression:被指定的正则表达式模式匹配到的字符串,要以开头,如.*.fone.com
            	valid_referers none block server_names *.fone.com fone.* ~.*\.fone\.;
            	if($invalid_referer) { 
            		return 403;
            	}
            
          9. (*) ngx_http_proxy_module模块:代理模块(缓存相关)

            • proxy_pass URL;
              注意:proxy_pass后面的路径不带uri时,其会将location的uri传递给后端主机;proxy_pass后面的路径是一个uri时,其会将location的uri替换为proxy_pass的uri
              server {
              	...
              	server_name HOSTNAME;
              	location /uri/ {
              		proxy_pass http://hos[:port];
              	}
              	...
              }
              # http://HOSTNAME/uri --> http://host/uri 
              
              server {
              	...
              	server_name HOSTNAME;
              	location /uri/ {
              		proxy http://host/new_uri/;
              	}
              	...
              }
              # http://HOSTNAME/uri/ --> http://host/new_uri/
              
              如果location定义其uri时使用了正则表达式的模式,或在if语句或limt_execept中使用proxy_pass指令,则proxy_pass之后必须不能使用uri; 用户请求时传递的uri将直接附加代理到的服务的之后;
              server {
              	...
              	server_name HOSTNAME;
              	location ~|~* /uri/ {
              		proxy http://host;
              	}
              	...
              }
              # http://HOSTNAME/uri/ --> http://host/uri/
              
            • proxy_set_header field value; # 设定发往后端主机的请求报文的请求首部的值
              proxy_set_header X-Real-IP  $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              
            • proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; # 定义可用于proxy功能的缓存路径
            • proxy_cache zone | off; # 是否开启缓存
            • proxy_cache_key string; # 缓存中用于“键”的内容
            • proxy_cache_valid [code] time; # 定义对特定响应码的响应内容的缓存时长
              # 在http中
              proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;
              
              # 在server中
              proxy_cache pxycache;
              proxy_cache_key $request_uri;
              proxy_cache_valid 200 302 301 1h;
              proxy_cache_valid any 1m;
              
            • proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off; # 定义缓存使用的场景
            • proxy_cache_methods GET | HEAD | POST; # 限定使用缓存的请求类型
            • proxy_hide_header field; # 隐藏请求头信息
            • proxy_connect_timeout time; # 缓存连接超时时间,默认65s,最长75s
            • proxy_read_timeout time; # 缓存获取超时时间
            • proxy_send_timeout time; # 缓存发送超时时间
          10. (*) ngx_http_headers_module模块:向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值

            • add_header name value [always]; # 添加自定义首部
            • expires [modified] time; # 用于定义Expire或Cache-Control首部的值,或添加其它自定义首部
          11. ngx_http_fastcgi_module模块:FastCGI server

            • fastcgi_pass IP; # 服务器地址
            • fastcgi_index name; # 默认主页资源
            • fastcgi_param parameter value [if_not_empty]; # 参数
              location ~* \.php$ {
              	root           /usr/share/nginx/html;
              	fastcgi_pass   127.0.0.1:9000;
              	fastcgi_index  index.php;
              	fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
              	include        fastcgi_params;
              }
              
              # 通过/pm_status和/ping来获取fpm server状态信息
              location ~* ^/(pm_status|ping)$ {
              	include        fastcgi_params;
              	fastcgi_pass 127.0.0.1:9000;
              	fastcgi_param  SCRIPT_FILENAME  $fastcgi_script_name;
              }
              
            • fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; # 定义fastcgi的缓存;缓存位置为磁盘上的文件系统,由path所指定路径来定义;
              levels=levels:缓存目录的层级数量,以及每一级的目录数量;如leves=1:2:2
              keys_zone=name:size:k/v映射的内存空间的名称及大小
              inactive=time:非活动时长
              max_size=size:磁盘上用于缓存数据的缓存空间上限
            • fastcgi_cache zone | off; # 调用指定的缓存空间来缓存数据
            • fastcgi_cache_key string; # 定义用作缓存项的key的字符串
            • fastcgi_cache_methods GET | HEAD | POST …; # 为哪些请求方法使用缓存
            • fastcgi_cache_min_uses number; # 缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项
            • fastcgi_cache_valid [code …] time; # 不同的响应码各自的缓存时长
              http {
              	...
              	fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20m inactive=120s;
              	...
              	server {
              		...
              		location ~* \.php$ {
              			...
              			fastcgi_cache fcgi;
              			fastcgi_cache_key $request_uri;
              			fastcgi_cache_valid 200 302 10m;
              			fastcgi_cache_valid 301 1h;
              			fastcgi_cache_valid any 1m;	
              			...
              		}
              		...
              	}
              	...
              }
              
            • fastcgi_keep_conn on | off; # 是否保持连接
          12. (*) ngx_http_upstream_module模块:定义集群

            • upstream name { … } # 定义后端服务器组,默认调度方法wrr
              upstream httpdsrvs {
              	server ...
              	server...
              	...
              }
              
            • server address [parameters]; # 在upstream中server成员,以及相关的参数
              address的表示格式:
              	unix:/PATH/TO/SOME_SOCK_FILE
              	IP[:PORT]
              	HOSTNAME[:PORT]
              	
              parameters:
              	weight=number
              		权重,默认为1;
              	max_fails=number
              		失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用;
              	fail_timeout=time
              		设置将服务器标记为不可用状态的超时时长;
              	max_conns
              		当前的服务器的最大并发连接数;
              	backup
              		将服务器标记为“备用”,即所有服务器均不可用时此服务器才启用;
              	down
              		标记为“不可用”;
              
            • least_conn; # 最少连接调度算法,当server拥有不同的权重时为wlc;当所有后端主机的连接数相同时,则使用wrr进行调度;
            • least_time header | last_byte; # 最短平均响应时长和最少连接
            • ip_hash; # 源地址hash调度方法,能够将来自同一个源IP地址的请求始终发往同一个upstream server
            • hash key [consistent]; # 基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者的组合。用于将请求分类,同一类请求将发往同一个upstream server
            • keepalive connections; # 为每个worker进程保留的空闲的长连接数量
            • health_check [parameters];
              定义对后端主机的健康状态检测机制;只能用于location上下文;
              可用参数:
              interval=time:检测频率,默认为每隔5秒钟;
              fails=number:判断服务器状态转为失败需要检测的次数;
              passes=number:判断服务器状态转为成功需要检测的次数;
              uri=uri:判断其健康与否时使用的uri;
              match=name:基于指定的match来衡量检测结果的成败;
              port=number:使用独立的端口进行检测;
              
            • match name { … } # 定义衡量某检测结果是否为成功的衡量机制
              status:期望的响应码;
              	status CODE
              	status ! CODE
              	...
              header:基于响应报文的首部进行判断
              	header HEADER=VALUE
              	header HEADER ~ VALUE 
              	...
              body:基于响应报文的内容进行判断
              	body ~ "PATTERN"
              	body !~ "PATTERN"
              
          13. (*) ngx_stream_core_module模块:模拟反代基于tcp或udp的服务连接,即工作于传输层的反代或调度器

            • stream { … } # 定义stream相关的服务
              stream {
              	upstream sshsrvs {
              		server 192.168.22.2:22; 
              		server 192.168.22.3:22; 
              		least_conn;
              	}
              
              	server {
              		listen 10.1.0.6:22022;
              		proxy_pass sshsrvs;
              	}
              }
              
            • listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
            • proxy_pass IP;
            • proxy_timeout time;
            • proxy_connect_timeout time; # 设置nginx与被代理的服务器尝试建立连接的超时时长;默认为60s
              stream {
              	upstream sshsrvs {
              		server 192.168.10.130:22;
              		server 192.168.10.131:22;
              		hash $remote_addr consistent;
              	}
              
              	server {
              		listen 172.16.100.6:22202;
              		proxy_pass sshsrvs; 
              		proxy_timeout 60s;
              		proxy_connect_timeout 10s;
              	}
              }
              
  • 安装:

    1. yum安装:http://nginx.org/en/linux_packages.html#RHEL-CentOS
    2. 编译安装:
      • 前提:开发环境,包括nginx编译要启用的功能依赖到的开发库;

        # yum groupinstall "Development Tools" "Server Platform Development"
        # yum -y pcre-devel openssl-devel
        
      • 编译过程:

        # ./configure --prefix=/usr/local/nginx --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 --user=nginx --group=nginx --with-http_ssl_module  --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-threads --with-file-aio
        # make && make install
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值