Nginx基础

Nginx基础

目录

  • Nginx概述

  • Nginx的优点

  • Nginx相比Apache

  • 编译安装Nginx

  • Nginx配置文件讲解

  • ngx_http_access_module模块

  • ngx_http_auth_basic_module模块

  • ngx_http_log_module模块

  • ngx_http_stub_status_module模块

Nginx概述

Engine X是一个高性能、高并发的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

Nginx的优点

高并发:Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性。在连接高并发的情况下,Nginx是Apache服务器不错的替代品,能够支持高达 50,000 个并发连接数的响应。 
负载均衡器·:Nginx作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务器对外进行服务。 
代理服务器:Nginx本身就是一个反向代理服务器,可支持邮件服务器代理以及http代理

Nginx相比Apache

1.轻量级,同样起web 服务,比apache 占用更少的内存及资源 
2.抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能 
3.高度模块化的设计,编写模块相对简单 
4.配置简洁易懂,正则配置让很多事情变得简单

编译安装Nginx

[root@centos7 ~]# yum -y groupinstall "Development Tools" "Server Platform Development"    # 安装开发包组
[root@centos7 ~]# yum -y install pcre-devel openssl-devel zlib-devel   # 安装依赖包
[root@centos7 ~]# useradd -r nginx  # 创建nginx系统用户
[root@centos7 ~]# ./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   # 配置nginx
[root@centos7 ~]# make   # 编译
[root@centos7 ~]# make install  # 安装

Nginx配置文件讲解

nginx配置文件组成:主配置文件nginx.confconf.d/*.conf; 
fastcgiuwsgiscgi等协议相关的配置文件; 
mime.types:支持的mime类型

main段配置

正常运行必备的配置
   1、user  # 指定用于运行worker进程时的用户
       Syntax: user user [group];
       Default:    user nobody nobody;
       Context:    main

   2、pid /PATH/TO/PID_FILE;    # 指定存储nginx主进程进程号码的文件路径;
       Syntax: pid file;
       Default:    pid nginx.pid;
       Context:    main

   3、include file | mask;   # 指明包含进来的其它配置文件片断;
       Syntax: include file | mask;
       Default:    —
       Context:    any

   4、load_module file;    # 指明要装载的动态模块;
       Syntax: load_module file;
       Default:    —
       Context:    main                    

性能优化相关的配置:
   1、worker_processes number | auto; # worker进程的数量;通常应该为当前主机的cpu的物理核心数;
       Syntax: worker_processes number | auto;
       Default:    worker_processes 1;
       Context:    main                    

   2、worker_cpu_affinity cpumask ...;  # 定义worker进程和cpu的绑定
       worker_cpu_affinity auto [cpumask];
       Default:    —
       Context:    main                        
       CPU MASK:
           000000010号CPU
           000000101号CPU
           ... ...

   3、worker_priority number; # 指定worker进程的nice值,设定worker进程优先级;[-20,20]
       Syntax: worker_priority number;
       Default:    worker_priority 0;
       Context:    main

   4、worker_rlimit_nofile number;  # worker进程所能够打开的文件数量上限;
       Syntax: worker_rlimit_nofile number;
       Default:    —
       Context:    main

调试、定位问题:
   1、daemon on|off;     # 是否以守护进程方式运行Nignx;
       Syntax: daemon on | off;
       Default:    daemon on;
       Context:    main

   2、master_process on|off;  # 是否以master/worker模型运行nginx;默认为on
       Syntax: master_process on | off;
       Default:    master_process on;
       Context:    main

   3、error_log file [level];  # 定义错误日志文件路径与级别
       Syntax: error_log file [level];
       Default:    error_log logs/error.log error;
       Context:    main, http, mail, stream, server, location

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


   1、worker_connections number; # 每个worker进程所能够打开的最大并发连接数数量;
       Syntax: worker_connections number;
       Default:    worker_connections 512;
       Context:    events
       worker_processes * worker_connections得出最大并发连接数

   2、use method;  # 指明并发连接请求的处理方法;
       Syntax: use method;
       Default:    —
       Context:    events
           use epoll;

   3、accept_mutex on | off;  # 处理新的连接请求的方法;on意味着由各worker轮流处理新请求,Off意味着每个新请求的到达都会通知所有的worker进程;建议使用on
       Syntax: accept_mutex on | off;
       Default:    accept_mutex off;
       Context:    events

http段配置

与套接字相关的配置:
   1、server { ... }  #配置一个虚拟主机;
       Default:    —
       Context:    http

       server {   # 配置虚拟主机示例
           listen address[:PORT]|PORT;
           server_name SERVER_NAME;
           root /PATH/TO/DOCUMENT_ROOT;
       }

   2、listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE  #定义虚拟主机所监听的端口
       listen address[:port] [default_server] [ssl] [http2 | spdy]  [backlog=number] [rcvbuf=size] [sndbuf=size]
       Default:    listen *:80 | *:8000;
       Context:    server

   3、server_name name ...;  #指明虚拟主机的主机名称;后可跟多个由空白字符分隔的字符串;
       Default:    server_name "";
       Context:    server

           支持*通配任意长度的任意字符;server_name *.magedu.com
           支持~起始的字符做正则表达式模式匹配;server_name ~^www\d+\.magedu\.com$

       匹配机制:
           (1) 首先是字符串精确匹配;
           (2) 左侧*通配符;
           (3) 右侧*通配符;
           (4) 正则表达式;

   4、tcp_nodelay on | off;  #在keepalived模式下的连接是否启用TCP_NODELAY选项;将多个小包打包成一个报文发送给客户端
       Default:    tcp_nodelay on;
       Context:    http, server, location


   5、sendfile on | off;  #是否启用sendfile功能;      
       Default:    sendfile off;
       Context:    http, server, location, if in location

定义路径相关的配置:
   6
、root path;   #设置web资源路径映射;用于指明用户请求的url所对应的本地文件系统上的文档所在目录路径;
       Default:    root html;
       Context:    http, server, location, if in location

   7location [ = | ~ | ~* | ^~ ] uri { ... }  #在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置;
       location @name { ... }
       Default:    —
       Context:    server, location

       =:对URI做精确匹配;例如, http://www.magedu.com/, http://www.magedu.com/index.html
           location = / {
               ...
           }
       ~:对URI
做正则表达式模式匹配,区分字符大小写;
       ~*:对URI做正则表达式模式匹配,不区分字符大小写;
       ^~:对URI的左半部分做匹配检查,不区分字符大小写;
       不带符号:匹配起始于此uri的所有的url;

       匹配优先级:=, ^~, ~/~*,不带符号;

       root /vhosts/www/htdocs/
       http://www.magedu.com/index.html --> /vhosts/www/htdocs/index.html

       server {
           root  /vhosts/www/htdocs/      
           location /admin/ {
               root /webapps/app1/data/
           }
       }

   8、alias path;   #定义路径别名,文档映射的另一种机制;仅能用于location上下文;
       Syntax: alias path;
       Default:    —
       Context:    location

       注意:location中使用root指令和alias指令的意义不同;
           (a) root,给定的路径对应于location中的/uri/左侧的/;
           (b) alias,给定的路径对应于location中的/uri/右侧的/;

   9、index file ...;   #默认主页面定义
       Default:    index index.html;
       Context:    http, server, location

   10、error_page code ... [=[response]] uri;  #定义默认错误页面
       Default:    —
       Context:    http, server, location, if in location

   11、try_files file ... uri;

定义客户端请求的相关配置
   12、keepalive_timeout timeout [header_timeout];  #设定保持连接的超时时长,0表示禁止长连接;默认为75s;
       Default:    keepalive_timeout 75s;
       Context:    http, server, location

   13、keepalive_requests number;  #在一次长连接上所允许请求的资源的最大数量,默认为100;
       Default:    keepalive_requests 100;
       Context:    http, server, location

   14、keepalive_disable none | browser ...;  #对哪种浏览器禁用长连接;
       Default:    keepalive_disable msie6;
       Context:    http, server, location

   15、send_timeout time;  #向客户端发送响应报文的超时时长,此处,是指两次写操作之间的间隔时长;
       Default:    send_timeout 60s;
       Context:    http, server, location

   16、client_body_buffer_size size;  #用于接收客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大小时,其将被暂存到磁盘上的由client_body_temp_path指令所定义的位置;
       Default:    client_body_buffer_size 8k|16k;
       Context:    http, server, location

   17、client_body_temp_path path [level1 [level2 [level3]]];  #设定用于存储客户端请求报文的body部分的临时存储路径及子目录结构和数量;
       Default:    client_body_temp_path client_body_temp;
       Context:    http, server, location

           16进制的数字;

           client_body_temp_path path  /var/tmp/client_body  1 2 2
               /var/tmp/client_body目录下有16个一级子目录,每个一级子目录下面有256个二级子目录,每个二级子目录下面有256个三级子目录

对客户端进行限制的相关配置:
   18、limit_rate rate;  #限制响应给客户端的传输速率,单位是bytes/second,0表示无限制;
       Default:    limit_rate 0;
       Context:    http, server, location, if in location

   19、limit_except method ... { ... }  #限制对指定的请求方法之外的其它方法的使用客户端;
       Default:    —
       Context:    location

       limit_except GET {
           allow 192.168.1.0/32;
           deny  all;
       }      

文件操作优化的配置:
   20、aio on | off | threads[=pool];  #是否启用aio功能;
       Default:    aio off;
       Context:    http, server, location

   21、directio size | off;  #在Linux主机启用O_DIRECT标记,此处意味文件大于等于给定的大小时使用,例如directio 4m;
       Default:    directio off;
       Context:    http, server, location

   22、open_file_cache off;  # 是否开启缓存
       open_file_cache max=N [inactive=time];
       Default:    open_file_cache off;
       Context:    http, server, location
           nginx可以缓存以下三种信息:
               (1) 文件的描述符、文件大小和最近一次的修改时间;
               (2) 打开的目录结构;
               (3) 没有找到的或者没有权限访问的文件的相关信息;

           max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现缓存管理;

           inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_users指令所指定的次数的缓存项即为非活动项;

   23、open_file_cache_valid time;  #缓存项有效性的检查频率;默认为60s;
       Default:    open_file_cache_valid 60s;
       Context:    http, server, location

   24、open_file_cache_min_uses number;  #在open_file_cache指令的inactive参数指定的时长内,至少应该被命中多少次方可被归类为活动项;
       Default:    open_file_cache_min_uses 1;
       Context:    http, server, location

   25、open_file_cache_errors on | off;  #是否缓存查找时发生错误的文件一类的信息;
       Default:    open_file_cache_errors off;
       Context:    http, server, location

ngx_http_access_module模块

实现基于ip的访问控制功能
   26、allow address | CIDR | unix: | all;
       Default:    —
       Context:    http, server, location, limit_except

   27、deny address
| CIDR | unix: | all;
       Default:    —
       Context:    http, server, location, limit_except

ngx_http_auth_basic_module模块

实现基于用户的访问控制,使用basic机制进行用户认证;
   28、auth_basic string | off;
       Default:    auth_basic off;
       Context:    http, server, location, limit_except

   29、auth_basic_user_file file;
       Default:    —
       Context:    http, server, location, limit_except

       location /admin/
{
           alias /webapps/app1/data/;
           auth_basic "Admin Area";
           auth_basic_user_file /etc/nginx/.ngxpasswd;
       }


       注意:htpasswd命令由httpd-tools所提供;      

ngx_http_stub_status_module模块

用于输出nginx的基本状态信息;

   Active connections: 291
   server accepts handled requests
       16630948 16630948 31070465
   Reading: 6 Writing: 179 Waiting: 106    

   Active connections: 活动状态的连接数;
   accepts:已经接受的客户端请求的总数;
   handled:已经处理完成的客户端请求的总数;
   requests:客户端发来的总的请求数;
   Reading:处于读取客户端请求报文首部的连接的连接数;
   Writing:处于向客户端发送响应报文过程中的连接数;
   Waiting:处于等待客户端发出请求的空闲连接数;

   30、stub_status;
       Default:    —
       Context:    server, location

   配置示例:
       location  /basic_status {
           stub_status;
       }

ngx_http_log_module模块

he ngx_http_log_module module writes request logs in the specified format.

   31、log_format name string ...;   #日志格式
       Default:    log_format combined "...";
       Context:    http

       string可以使用nginx核心模块及其它模块内嵌的变量;

       $bytes_sent:发送到客户端的字节数
       $connection:连接序列号
       $connection_requests:目前一些通过连接发出的请求(1.1.18
       $msec:时间与一个毫秒分辨率秒日志写入的时间
       $pipe"p"如果请求被流水线
       $request_length:请求长度
       $request_time:请求处理时间在毫秒分辨率秒; 第一字节之间经过的时间是从在客户端和日志写入读出之后,最后的字节被发送到客户端
       $status:响应状态
       $time_iso8601:在ISO 8601标准格式的本地时间
       $time_local:在通用日志格式的本地时间

   32、access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];   #访问日志文件路径,格式及相关的缓冲的配置;
       access_log off;
       Default:    access_log logs/access.log combined;
       Context:    http, server, location, if in location, limit_except

           buffer=size
           flush=time

   33、open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];    #缓存各日志文件相关的元数据信息;
       open_log_file_cache off;
       Default:    open_log_file_cache off;
       Context:    http, server, location

           max:缓存的最大文件描述符数量;
           min_users:在inactive指定的时长内访问大于等于此值方可被当作活动项;
           inactive:非活动时长;
           valid:验正缓存中各缓存项是否为活动项的时间间隔;

原创文章,作者:zhai796898,如若转载,请注明出处:http://www.178linux.com/56509

(0)
zhai796898zhai796898
上一篇 2016-11-01 14:16
下一篇 2016-11-01 16:56

相关推荐

  • 震惊!!iptables还能这样用!

    基本语法:iptables [-t 表] [操作命令] [链][规则匹配器][-j 目标动作] 系统的INPUT和OUTPUT默认策略为DROP; # iptables -P INPUT DROP # iptables -P OUTPUT DROP 1、限制本地主机的web服务器在周一不允许访问;新请求的速率不能超过100个每秒;web服务器包含了admin…

    系统运维 2017-04-18
  • 我的第一篇博客

    2018.3.26 整理

    2018-03-26
  • 关于VIM编辑器

                                                      &nbsp…

    系统运维 2016-08-11
  • SSH端口转发

      SSH 会自动加密和解密所有SSH 客户端与服务端之间的网络数据。但是,SSH 还能够将其他TCP 端口的网络数据通过SSH 链接来转发,并且自动提供了相应的加密及解密服务。这一过程也被叫做“隧道”(tunneling),这是因为SSH 为其他TCP 链接提供了一个安全的通道来进行传输而得名。例如,Telnet,SMTP,LDAP 这些TCP 应用均能够…

    2017-09-10
  • mariadb基础应用

    mariadb基础应用 一、 前言 MariaDB is one of the most popular database servers in the world. It’s made by the original developers of MySQL and guaranteed to stay open source. Notable users …

    Linux干货 2016-12-16
  • Bash 三种排序算法的简单实现

    Bash 三种排序算法的简单实现

    2017-07-09