概述   

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名

Nginx的程序架构:
master/worker
    一个master进程:
        负载加载配置文件、管理worker进程、平滑升级
    一个或多个worker进程
        处理并响应用户请求
    缓存相关的进程:
        cache loader:载入缓存对象
        cache manager:管理缓存对象

nginx高度模块块:高度模块化,但其模块早期不支持DSO机制;近期版本支持动态装载和卸载;

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

特性:异步、事件驱动和非阻塞

          并发请求处理:通过kevent/epoll/select
          文件IO:高级IO sendfile,异步,mmap

sendfile :    用户空间无法调用系统硬件,只有通过内核调用,当硬件将信息返回给内核后,内核直接制作相应报文回应

nginx的功用:

    静态的web资源服务器;

    结合FastCGI/uwSGI/SCGI等协议反代动态资源请求;(反向代理服务器,且还能缓存)
    http/https协议的反向代理;(LNMP或者LANMP)
    imap4/pop3协议的反抽代理;
    tcp/udp协议的反代;

nginx的安装配置:

1)官方的预制包:

    

2)编译安装

实例:

实验环境:CentOS 7.2

源码包:nginx-1.10.0.tar.gz

#安装必要的开发包组以及相关开发包[root@dashui ~]# yum groupinstall "Development Tools"[root@dashui ~]# yum install pcre-devel openssl-devel zlib-devel  -y#添加nginx用户[root@dashui ~]# useradd -r nginx#解压nginx源码包[root@dashui ~]# tar xf nginx-1.10.0.tar.gz#切换至解压目录,然后编译安装[root@dashui ~]# ./configure --prefix=/usr/local/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 --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio#安装[root@dashui ~]# make && make install#添加nginx的bin目录至PATH环境变量[root@dashui ~]# echo "export PATH=/usr/local/nginx/bin:PATH" > /etc/profile.d/nginx.sh#启动Nginx[root@dashui ~]# nginx[root@dashui ~]# nginx[root@dashui ~]# ss -tnlState       Recv-Q Send-Q          Local Address:Port                         Peer Address:Port              LISTEN      0      128                         *:80                                      *:*                  LISTEN      0      128                         *:22                                      *:*                  LISTEN      0      128                 127.0.0.1:631                                     *:*                  LISTEN      0      100                 127.0.0.1:25                                      *:*                  LISTEN      0      128                        :::22                                     :::*                  LISTEN      0      128                       ::1:631                                    :::*                  LISTEN      0      100                       ::1:25                                     :::*

Nginx的配置文件:

配置文件的组成部分:

    主配置文件:nginx.conf
        include conf.d/*.conf
    fastcgi, uwsgi,scgi等协议相关的配置文件
    mime.types:支持的mime类型

主配置文件的配置指令:

    directive value [value2 ...];

注意:

    (1) 指令必须以分号结尾;
    (2) 支持使用配置变量;
        内建变量:由Nginx模块引入,可直接引用;
        自定义变量:由用户使用set命令定义;
            set variable_name value;
            引用变量:$variable_name

主配置文件结构:

    main block:主配置段,也即全局配置段;

#实例  1   2 #user  nobody;  3 worker_processes  1;  4   5 #error_log  logs/error.log;  6 #error_log  logs/error.log  notice;  7 #error_log  logs/error.log  info;  8   9 #pid        logs/nginx.pid; 10  11

    event {

        ...
    }:事件驱动相关的配置;
    

 12 events { 13     worker_connections  1024; 14 } 15

    http {

        ...
    }:http/https 协议相关的配置段;
  

http { 18     include       mime.types; 19     default_type  application/octet-stream; 20  21     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 22     #                  '$status $body_bytes_sent "$http_referer" ' 23     #                  '"$http_user_agent" "$http_x_forwarded_for"'; 24  25     #access_log  logs/access.log  main; 26  27     sendfile        on; 28     #tcp_nopush     on; 29  30     #keepalive_timeout  0; 31     keepalive_timeout  65; 32  33     #gzip  on; 34  35     server { 36         listen       80; 37         server_name  localhost; 38  39         #charset koi8-r; 40  41         #access_log  logs/host.access.log  main; 42  .....省略..... 81  82     # another virtual host using mix of IP-, name-, and port-based configuration 83     # 84     #server { 85     #    listen       8000; 86     #    listen       somename:8080; 87     #    server_name  somename  alias  another.alias; 88  89     #    location / { 90     #        root   html; 91     #        index  index.html index.htm; 92     #    } 93     #}

 mail {

         ...
    }
    stream {
         ...
    }