系统学习Nginx之基础篇

什么是Nginx:

Nginx是一个开源且高性能、可靠的HTTP中间件、代理服务。

为什么要选择Nginx:

IO多路复用epoll
轻量级(功能模块少、代码模块化)
CPU亲和(affinity)(是一种把CPU核心和Nginx工作进程绑定方式,把每个worker进程固定在一个cpu上执行,减少切换cpu的cache miss,获得更好的性能)
sendfile

使用yum进行安装:
打开nginx.org,进入到Ngixn官网,点击download-页面下方的 Linux packages for stable version,根据官网给出的操作,vim /etc/yum.repos.d/nginx.repo文件,增加如下代码:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/ #OS是系统,这里写centos,OSRELEASE是版本号,这里写6或7,根据实际情况
gpgcheck=0
enabled=1

然后可以使用yum list |grep nginx 查看是否有nginx,最后执行yum -y nginx进行安装,这样我们就下载了nginx的最新稳定版本

如果是使用的yum安装,可以通过 rpm -pl nginx 命令查看nginx的目录。

看一下nginx的目录结构:
/etc/logrotate.d/nginx 配置文件 Ngixn日志轮转,用于logrotate服务的日志切割

/etc/nginx
/etc/nginx/nginx.conf
/etc/nginx/conf.d
/etc/nginx/default.conf  这4个为Ngixn主配置文件

/etc/nginx/fastcgi_params
/etc/nginx/uwsgi_params
/etc/nginx/scgi_params  类型为cgi配置相关,fastcgi配置

/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/win-utf   类型为配置文件    编码转换映射转换文件

/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug  类型为配置文件,用于配置出系统守护进程管理器管理方式。

/usr/lib64/nginx/modules  
/etc/nginx/modules  类型为目录,Nginx模块目录

/usr/sbin/mginx
/usr/sbin/nginx-debug  类型为命令,Nginx服务的启动管理的终端命令

/usr/share/doc/nginx-1.12.0
/usr/share/doc/nginx-1.12.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz  类型为文件、目录   Nginx的手册和帮助文件

/var/cache/nginx 类型为目录 Nginx的缓存目录

/var/log/nginx  目录  Nginx的日志目录

Nginx默认配置语法:

user  nginx;  #设置Nginx服务的系统使用用户,一般不更改
worker_processes  1; #Nginx的工作进程数,一般和CPU核心数量一致即可

error_log  /var/log/nginx/error.log warn; #Nginx的错误日志
pid        /var/run/nginx.pid; #Nginx服务启动时候的pid


events {
    worker_connections  1024;  #每个进程允许最大连接数,一般调节到10000左右可满足大部分要求,实际情况选择
    #use     #Nginx使用哪种内核模型
}

http{
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request(请求行)" '
                      '$status(返回状态) $body_bytes_sent(返回的body信息大小) "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main; #与上面的main相互对应,表示一致

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;  #客户端与服务端超时实际,单位s

    #gzip  on;
    server {
        listen       80;  #监听端口
        server_name  localhost; #服务名称,域名地址

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;

        location / { #默认路径
            root   /usr/share/nginx/html; #路径地址
            index  index.html index.htm; #默认访问哪个页面,如果index.html没找到,就去访问index.htm
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        # 状态码为500 520 503 504时重定向到指定页面,更友好
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
server {
    ... ...
    }
}

Nginx日志类型:

包括:error.log(处理HTTP请求错误的状态,以及服务本身错误的状态。按照级别记录) access_log(记录每次HTTP请求的访问状态)

Nginx变量:

HTTP请求变量:arg_PARAMETER、http_HEADER、send_http_HEADER等
Nginx内置变量:Nginx内置的
自定义变量-自己定义

模块学习:

http_stub_status_module模块:Nginx的客户端状态

配置:
stub_status;

Active connections: 4  #连接数
server accepts handled requests
 7 7 5     #总值  Nginx握手总次数  处理连接数  总的连接数,一般来说,前2个要想等,说明没有丢失数据
Reading: 0 Writing: 1 Waiting: 3  #读  写 等待数

http_random_index_module模块:目录中选择一个随机主页,很少使用

语法:random_index on|off
默认:random_index off;
层级:location

此配置不会打开.开头的隐藏文件

http_sub_module模块:

HTTP内容替换,一般应用较少

语法:sub_filter string(替换对象) replacement(替换后对象);
默认:--
层级:http,server,location


语法:sub_filter_last_modified on|off #是一个时间,判断是否过期
默认:sub_filter_last_modified off
层级:http,server,location


语法:sub_filter_once on|off;   #是否只替换一次
默认:sub_filter_once on
层级:http,server,location

Nginx请求限制模块:

连接请求限制 -limit_conn_module #

语法:limit_conn_zone key zone=name:size; #key以什么来限制,如ip,zone空间名字,szie空间的大小
默认:--
层级:http

语法:limit_conn zone number; #zone上面的申请的名字  number并发的限制
默认:--
层级:http,server,location

请求频率限制 -limit_req_module #:

语法:limit_req_zone key zone=name:size rate=rate; #上同,rate速率如1r/s,一秒一次1
默认:--
层级:http

语法:limit_req zone=name [burst=number] [nodelay]; #burst客户端在超过这个number后,number个请求下一秒执行,nodelay如设置,其他直接返回403
默认:--
层级:http,server,location

一般来说,连接限制比频率限制要有效,因为根据TCP/IP协议,一次连接可以有多次请求。

访问控制:

基于IP的访问控制 -http_access_module;

语法:allow address | CIDR | unix: | all; #IP地址|网段|Socket限制,使用不多|允许全部
默认:--
层级:http,server,location,limit_except

语法:deny address | CIDR | unix: | all; #IP地址|网段|Socket限制,使用不多|允许全部
默认:--
层级:http,server,location,limit_except

假设客户端使用代理等中间件访问,可以通过这层限制。此时可以使用 http_x_forwarded_for=Client IP,Proxy(1)IP,Proxy(2)IP,…

解决该局限性的方法:
1:采用别的HTTP头信息控制访问,如:HTTP_X_FORWARD_FOR,不过不是所以客户端支持,而且以该头信息可被修改
2:结合geo模块
3:通过HTTP自定义变量传递

基于用户的信任登陆 -http_auth_basic_module

语法:auth_basic string |off; #即开始同时显示string
默认:auth_basic off;
层级:http,server,location,limit_except

语法:auth_basic_user_file file; #用来认证存储用户名和密码信息的文件,格式官网可看
默认:--;
层级:http,server,location,limit_except

局限性:
1.用户信息依赖文件方式,
2.操作管理机械,效率低下

解决:结合LUA实现高效验证、Nginx和LDAP打通,利用Nginx-auth-ldap模块