下面介绍的是Nginx有关知识点。


一、Nginx基础

1、Nginx功能(三个)

  静态的web服务器 ---------------http/https

  反向代理服务器 -----------http/smtp/pop3/imap4(主要作用)

  负载均衡集群


  Nginx的反代:

  基于FastCGI、uwsgi、SCGI等协议反代动态资源请求

  基于http/https协议的反代

  基于smtp/pop3/imap4协议的反代

  基于tcp/udp协议的反代


2、Nginx的I/O请求的两个阶段

(1)、数据从磁盘加载到内核空间

(2)、数据从内核空间复制到用户空间


3、Nginx的I/O模型(5种)

(1)、阻塞型 -----blocking(闲等待)

(2)、非阻塞型 -------------忙等待

(3)、复用型 --------------线程工作模式,如果多个用户请求相同内容,从硬盘只须载入内核一次(类似worker模型)

(4)、信号驱动型 -----------epoll模型,类似event模型

(5)、异步型 ------------asyhchronize,消息通知机制,POLL()函数回调,apache不支持,Nginx支持


4、Nginx特性 ---------------http://nginx.org/en/

(1)、基本http服务的特性

  支持基于缓存的反代,负载均衡、故障倒换

  支持基于FastCGI、uwsgi、SCGI的反代,负载均衡、故障倒换

  支持模块化结构、过滤器机制

  支持SSL、TLS

  支持http/2.0


  Nginx的模块化类型:核心模块、标准功能模块第三方模块


(2)、其他的http服务特性

  支持虚拟主机、长连接、URL重写、流媒体、响应限速


5、Nginx的程序结构:主进程/子进程。

  master进程:加载配置文件,管理worker进程,平滑升级。

  worker进程:处理并响应客户端请求。

  Nginx对于主进程(master),只有一个;但可以有多个子进程(worker)。worker可以单独存在,不一定要有master。



二、Nginx配置安装 ----------编译、rpm包

1、编译安装

(1)、编译环境

(2)、openssl-devel

       pcre-devel

       libenvent-devel

(3)、编译

  # ./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 --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-threads --with-file-aio --with-http_stub_status_module

(4)安装

  # make -j 4 

  # make install


  启动服务前,解决用户和组问题:

  # groupadd -r -g 808 nginx

  # useradd -r -g 808 -u 808 -r /sbin/usr nginx


2、rpm包安装

  http://nginx.org/packages/centos/      



三、Nginx配置文件 

  http://nginx.org/en/docs -------------Nginx帮助


1、主配置文件 -----------------/etc/nginx/nginx.conf

  主配置文件,主要有三段:全局配置段、http相关配置段、mail相关配置段

(1)、全局配置段 -----------main block

  例:

  user  nginx; -----------指定worker子进程的用户和组

  worker_processes 3; -------------worker进程的启动数量

  pid /var/run/nginx.pid; ------------指定存放【master进程编号】的文件路径

  events {

    worker_connections 1024; ----------每个worker并发连接的最大连接数

  }




(2)、http相关配置段

  http{

    context{ ... }

    directive value;

  }


  例:

  include /etc/nginx/conf/*.conf;

  sendfile on;

  keepalive_timeout  65; -------------此时间内没有收到任何请求,则关闭此长连接

  keepalive_request 100; ------------用户请求达到该最大限制值,则关闭此长连接

  server {

    listen 80;

    server_name little;

    include /etc/nginx/conf/*.conf;

    root /myweb/vhost1; -------------web资源的路径映射

    location {

      root html; 

      alias /bbs/ /forum/; -----------定义路径映射的别名

    }

  }


  server {

    listen 8080;

    server_name ling;

    root /myweb/vhost2;

  }



(3)、mail相关配置段

  mail{

    ...

  }



2、主配置文件的配置指令,内容解释 ----------------/etc/nginx/nginx.conf

(1)、全局配置段,配置指令 ------------Context:  main

  全局配置的配置指令,分为四部分,分别是:正常服务、优化性能、调试和定位、event函数。

  正常服务指令:

  user user [group]; -------------指定worker子进程的用户和组

  pid file; -----------指定存放【master进程编号】的文件的路径

  worker_rlimit_nofile number; ------------worker进程打开的文件数量的最大值


  优化性能指令:

  worker_processes number | auto; ------------------worker进程的启动数量

  worker_cpu_affinity cpumask ...;

  worker_cpu_affinity auto [cpumask]; -----------设置每个CPU核心、worker进程的亲缘性

  worker_priority number; ------------设置worker进程启动nice值

  例:

  worker_cpu_affinity cpumask 0010 0100

  

  调试和定位指令:

  daemon on | off; -----------------是否以独立守护进程,启动nginx

  master_process on | off; ---------是否以master/worker模型,启动nginx进程

  error_log file [level]; ----------错误日志文件的位置、记录日志的级别

    Context: 【main、location】、http、mail、stream、server

  thread_pool name threads=number [max_queue=number]; -----------定义线程池的名称、线程数量、线程后援队列长度

       

  events()函数:

  worker_connections number; ----------worker进程的并发连接的最大连接数(默512)

  worker_connections * worker_processes; -----------能够【响应】的最大并发连接数

  use method; ----------并发连接请求,处理方法(epoll)# use epoll

  accept_mutex on | off; ----------是否多个worker进程,依次轮流响应新请求


(2)、http相关配置段指令

  http相关配置指令,主要分为两部分:虚拟主机相关、客户端相关。


  1、虚拟主机相关:

  server{……} -----------Context: server

  server_name name ...; ---------虚拟主机名称(多个主机名、"*"匹配的主机、~正则表达式主机)

  listen address[:port] [default_server] [ssl] [http2 | spdy]  [backlog=number] [rcvbuf=size] [sndbuf=size]

  listen port [default_server] [ssl] [http2 | spdy]

  listen unix:path [default_server] [ssl] [http2 | spdy]

    backlog=number -------------后援队列长度

    rcvbuf=size ---------------接收缓冲大小

    sndbuf=size -------------发送缓冲大小

    例:

    listen 127.0.0.1:8000;

    listen 127.0.0.1;

    listen 8000;

    listen *:8000;

    listen localhost:8000;

  root path; -------------------web资源,路径映射

    Context: http、【server、location】

  location [ = | ~ | ~* | ^~ ] uri { ... } ------------据URI匹配location

    Context: server、location

    = ------------精确匹配

    ~ ------------正则表达式匹配,【区分】大小写

    ~* -----------正则表达式匹配,【不区分】大小写

    ^~ -------------匹配URI左半部分,【不区分】大小写

  alias path; ------------路径映射的别名

    Context: location

  index file ...; ------------默认主页文件名

    Context: http、【server、location】


  

  2、客户端相关:--------------Context: http、【server、location】

  error_page code ... [=[response]] uri; ------------据响应状态码实现页面错误重定向

    例:error_page 404 =200 /404.html;

  keepalive_disable none | browser ...; ---------对部分浏览器,禁用长连接

  keepalive_requests number; ----------用户请求达到最大限制值,关闭长连接

  keepalive_timeout timeout [header_timeout]; ---------指定时间没有请求,关闭长连接

  

  send_timeout time; ----------响应报文的超时时长,该时间没有收到响应报文,关闭连接

  client_body_buffer_size size; -------------body部分缓冲区大小(默16K)

  client_body_temp_path path [level1 [level2 [level3]]]; -------------body的临时存储路径

    例:client_body_temp_path /var/tmp/client_body 2 1 2(几个16进制目录)

  limit_rate rate; -----------客户端传输速率(0表无限制)

  limit_except method ... { ... }; ---------指定的不用(allow、deny)

    Context:  location

  

  allow address | CIDR | unix: | all; ------------对客户端访问控制,顺序匹配

  deny address | CIDR | unix: | all;

    Context: 【limit_except】 ++

    CIDR --------无类域间路由

  aio on | off | threads[=pool]; --------是否启用异步I/O

  directio size | off; -------------是否启用directio

  open_file_cache off; -----------文件描述符缓存

  open_file_cache max=N [inactive=time]; -----------缓存数量上限,缓存超时时长

  open_file_cache_errors on | off; --------------错误信息,是否缓存

  open_file_cache_min_uses number; ----------指定时间,缓存元素使用次数

  open_file_cache_valid time; -----------缓存检查时间(默60s)

  stub_status; -----------输出基本信息(简要)

    Context: server, location

  valid_referers none | blocked | server_names | string ...; -----------合法的Referer数据(跳转)

    none ----------首部没有Referer

    blocked ------------首部有Referer,但无值

    server_name ----------------首部Referer是主机名

    arbitrary string ----------任意,可用"*"通配符

    regular expression -----------正则表达式,有"~"起始

    例:valid_referers none blocked server_name *.little.com ~\.little\.



  3、SSL相关(ngx_http_ssl_module):------------Context: http、server

  ssl on | off; -----------虚拟主机https协议

    同:listen 443 ssl;

  ssl_certificate file; -------------PEM格式证书文件路径

  ssl_certificate_key file; ----------私钥文件路径

  ssl_session_cache off | none | [builtin[:size]] [shared:name:size]; 

      -------------------ssl的会话缓存,哪种类型、多少存储空间

  ssl_session_timeout time; ------------ssl参数的有效时长(默5分钟)

  ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2] [TLSv1.3]; --------nginx兼容的协议版本



  4、日志相关(ngx_http_log_module) -------------Context: http

  access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

    ----------访问路径、格式、缓冲区大小、压缩等级、刷新时间

  access_log off; ----------是否记录访问日志

    Context: http、server、location、if in location、limit_except

  

  log_format name [escape=default|json] string ...; ----------存储访问日志【格式】

    例:

    log_format combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';

  

  open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];

    -----------描述符数量、非活动时间、缓存最少命中次数,缓存有效时长(60s)

  open_log_file_cache off; ---------缓存机制

    Context: http, server, location


  

  5、URI重写相关(ngx_http_rewrite_module) ------------Context: server、location、if

  rewrite regex replacement [flag]; -----------regex的URI替换为replacement

    flag:last、break、redirect、permanent

    例:

    location / {

      root   html;

      index  index.html index.htm;

      rewrite ^/epel http://172.16.72.101;

      rewrite ^/(.*\.(jpg|jpeg|gif|png))$ http://172.16.72.1/p_w_picpaths/$1;

      rewrite ^/(.*)\.htm$ /$1.html;

    }

  

  return code [text]; --------------返回一个状态码、URL

  return code URL;

  return URL;

         

  if (condition) { ... } -------------条件判断


  6、反代功能相关(ngx_http_proxy_module)

  proxy_pass URL; ----------设置被代理服务器的地址、协议

    Context:  location, if in location, limit_except

  

  proxy_set_header field value; ---------发往后端服务器的请求报文首部、值

    Context:  http, server, location

    例:

    proxy_set_header X-Forwarded-For $remote_addr;

  

  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]; --------------缓存路径

    Context:  http

    例:

    proxy_cache_path levels=1:2:2 keys_zone=proxycache:10m max_size=1g;

  

  proxy_cache zone | off; -----------调用缓存开启或关闭缓存机制

    Context:  http, server, location


  proxy_cache_key string; ----------定义缓存的键

    Context:  http, server, location


  proxy_cache_valid [code...] time; -----------响应码状态时长

      Context:  http, server, location


  proxy_connect_timeout time; ----------建立tcp连接的超时时间,默认60s

  proxy_send_timeout time; -----------------向后端发送请求的超时时间,默认60s

  proxy_read_timeout time; -----------从后端响应的超时时间,默认60s

    Context:  http, server, location


  proxy_buffer_size

    proxy_buffering

    proxy_buffers



  7、首部管理模块(ngx_http_headers_module)------------向客户端的响应数据报文

  add_header name value [always]; -----------增加的首部内容

    Context:  http, server, location, if in location


  expires [modified] time; ---------定义超时、缓存控制首部的值

  expires epoch | max | off;

    Context:  http, server, location, if in location


  8、服务组管理(ngx_http_upstream_module)

  upstream name { ... } ------------定义一个服务组

    Context:  http


  server address [parameters]; ------------后端服务器地址等参数

    Context:  upstream

    parameters:

    weight=number -----------权重,默认 1

    max_fails=number ------------尝试连接RS的最多失败此时,默认 1

    fail_timeout=time -----------RS的不可用超时时长,默认 10s

    backup --------------服务器备用

    down -----------服务器下线


  hash key [consistent]; ----------一致性hash算法

    Context:  upstream


  ip_hash; --------------源hash算法

    Context:  upstream


  least_conn; -----------最少连接数调度算法

    Context:  upstream


  least_time header | last_byte [inflight]; ----------最少平均响应时长、最少连接数算算法

    Context:  upstream



  9、组服务----------与http相对(ngx_stream_core_module)

  listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size]

    Context:  server


  server { ... } --------------定义一个集群服务

    Context:  stream


  stream { ... } ----------------定义一个负载均衡器

    Context:  main



3、片段配置文件 ---------------/etc/nginx/conf/*.conf(编译的没有,需指定)


4、模块配置文件 --------------与FCGI、uwsgi、SCGI相关的配置文件


5、mime.types -----------------所有的mime类型