nginx的安装

详见查看nginx.org

一、nginx安装

nginx安装方法有两种:

  • yum安装

测试安装系统 centos6.5 

yum源:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/6/$basearch/
gpgcheck=0
enabled=1

安装版本为最新版

  • 源码安装
 1 [root@mytestpc1 ~]# yum groupinstall -y "Development tools" "Server Platform Development"   ##安装开发组件
 2 [root@mytestpc1 ~]# yum install -y pcre-devel openssl-devel zlib-devel ##安装一些模块用到的包 安装pcre是为了支持rewrite,zlib是为了支持gzip压缩,openssl是为了支持https;
 3 [root@mytest4 sbin]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_gzip_static_module
 4 [root@mytest4 sbin]# make && make install
 5 [root@mytest4 sbin]# cd /usr/local/nginx/sbin
 6 [root@mytest4 sbin]# ll
 7 total 5844
 8 -rwxr-xr-x 1 root root 5982777 Sep 22 20:08 nginx
 9 [root@mytest4 sbin]# ./nginx -t          ##可以通过nginx -t 查nginx是否安装正常
10 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
11 nginx: configuration file /etc/nginx/nginx.conf test is successful

 二、nginx相关配置

 可以查看nginx模板的默认配置:

 1 [root@mytest4 ~]# cat /etc/nginx/nginx.conf.default | grep -v -e "^[[:space:]]*#.*" -e "^[[:space:]]*$"
 2 worker_processes  1;                                    ##worker线程数
 3 events {
 4     worker_connections  1024;                ##每个worker的最大并发数
 5 }
 6 http {                              ##nginx是按照模块进行配置,http模块相关配置
 7     include       mime.types;
 8     default_type  application/octet-stream;
 9     sendfile        on;
10     keepalive_timeout  65;                  
11     server {                          server相关
12         listen       80;
13         server_name  localhost;
14         location / {                     
15             root   html;
16             index  index.html index.htm;
17         }
18         error_page   500 502 503 504  /50x.html;
19         location = /50x.html {
20             root   html;
21         }
22     }
23 }

main配置段:

  配置指令的类别:

    正常运行必备的配置:

    优化性能的配置:

    用于调试定位问题的配置:

    nginx配置可以使用变量:

  正常运行必备的配置:

    1、user USERNAME [GROUPNAME];
    指定运行worker进程的用户 和组,例如:
    user nginx nginx;s

    2、pid /path/to/pid_file;
    指定nginx的pid文件;

    3、worker_rlimit_nofile #;
    指定一个worker进程所能够打开的最大文件句柄数;

    4、worker_rlimit_sigpending #;
    指定每个用户能够发往worker的信号的数量;

  优化性能相关的配置:
    1、worker_processes #:   可以使用cpu隔离,系统使用一颗核心,其他的worker个使用一个核心,一个worker放在一个cpu上可以使缓存命中数更高。
    worker线程的个数;通常应该为物理CPU核心个数减1;可以是auto,auto的数量是cpu的核心数

    2、worker_cpu_affinity cpumask ...;    cpu的亲缘性绑定
    绑定worker进程至指定的CPU上;
      CPUMASK
      0001  第一颗cpu
      0010  第二
      0100  第三
      1000  第四
      例如:
      worker_cpu_affinity 0000 0001、0000 0010、0000 0100;  & worker_cpu_affinity auto     


    3、timer_resolution t;
    gettimeofday();

    4、worker_priority nice;  ## nice值越低优先级越高,nice值对应的是-20~19 对应的优先级是100~139
    -20, 19

  用于调试、定位问题:
    1、daemon [off|on]
    是否以守护进程方式启动nginx;默认都是

    2、master_process on|off;
    是否以master/worker模型来运行nginx; 正常是on,调试时off

    3、error_log /path/to/error_log level;
    错误日志文件及其级别;出于调试的目的,可以使用debug级别,但此级别只有在编译nginx时使用了--with-debug选项才有效;

  EVENT段:

    1、worker_connections #

      每个进程能打开的最大连接数,受限制于worker_rlimit_nofile,每台机器能打开最大连接数等于 

       worker_processes  *   worker_connections

    2、use method;

      指定并发连接处理时使用的方法:epoll/poll/select/ 等等

    3、accept_mutex ON|OFF ;

      是否打开负载均衡锁,互斥锁。启用时,worker轮流的响应请求,不开启的话,哪个worker抢的快,用哪个。默认是打开的。

  HTTP段:

    主要作用:
    义套接字相关功能
    定义路径相关配置
    定义客户端请求的相关配置
    对客户的请求进行限制的相关配置
    文件操作优化的配置

    1、server {...} 定义虚拟主机的;

      server {

          listen PORT

          server name HOSTNAME

          root /PATH/TO/SOMEWHERE

        }

    2、listen 

    监听的端口
    完整格式 :listen address[:port] [default_server] [ssl] [spdy] [proxy_protocol] [setfib=number]
    [fastopen=number] [backlog=number(后援队列)] [rcvbuf=size] [sndbuf=size] [accept_filter=filter]
    [deferred] [bind] [ipv6only=on|off] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];

    3、server_name name [...];

    后可跟多个主机名;名称还可以使用通配符和正则表达式(~);
    (1) 先做精确匹配;www.magedu.com:
    (2) 左侧通配符匹配,例如:*.magedu.com;
    (3) 右侧通配符匹配,例如:www.*;
    (4) 正则表达式匹配,例如: ~^.*.magedu.com$
    (5) default_server

    4、tcp_nodelay on|off

      对keepalived模式下的链接是否启用tcp_delay选项。默认情况下tcp链接对很小的数据包为了节约带宽,会把小数据积累到一定数量再一并发送。

    5、tcp_nopush on|off  在sendfile模式下才有效,默认是关闭的。

    6、sendfile on|off:在内核中直接封装响应报文响应用户,默认是off的

  定义路径相关配置

    7、root path

      设置web资源的路径映射机制;用于指明用户请求的url多对应的本地文件系统上的文档所在目录路径;

      可用上下文:http,server,location,if

    8、location [=|~|~*|^~] /uri {...}

         location @name

      使用上下文:server、location

     功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location块中的配置所处理;

        =: 精确匹配检查;
        ~: 正则表达式模式匹配,区分字符大小写;
        ~*:正则表达式模式 匹配,不区分字符大小写;
        ^~:URI的前半部分匹配,不检查正则表达式;

          匹配优先级:精确匹配(=)、^~、~和~*、由不带符号的URL进行左侧匹配;

    9、alias path 定义路径别名,文件映射的一种机制。

       上下文:location

      location /images/ {
        alias /www/pictures/;
      }
      注意:root表示指明路径为对应location的URI的根  
      URL;alias表示路径映射,即location中的URI是相等于alias所指明的URI,可以理解为软连接;

    10、index file

      设置默认专业  

      可用上下文:httpd、server、location

    11、error_page 自定义错误页面

      根据用户请求的资源的http响应的状态码,实现错误页重定向。

    12、error_page code [...] [=code] URI | @name
      根据http状态码重定向错误页面
      error_page  404  /404.html
      =[code]: 以指定的响应码进行响应;省略code表示以新资源的响应码为响应码;

    13、try_files     ????
      try_files path1[,path2,...] URI

  定义客户端请求相关

    14、keepalive_timeout time [header_timeout];
      保持连接的超时时长,0表示禁止长链接,默认为75s;

    15、keepalive_requests #;
      在一次保持连接上允许承载最大资源请求数,默认100个;

    16、keepalive_disable [msie6|safari|none]  none表示哪个都不禁用
      为指定类型的浏览器禁用长连接; 

    17、send_timeout time;
      发送两次响应报文的超时时长;特别的指两次写操作之间的建个时长

    18、client_body_buffer_size SIZE;

      用于接受客户端请求报文的body部分的缓冲区大小;默认为16k,超出此大小时,其将被暂存到磁盘上。

    19、client_body_temp_path DIR [level1 [level2 [level3 [level4]]]]

      设定用于存储客户端请求报文的body部分的临时存储路径及目录结构和数量

      eg:client_body_temp_path /var/tmp/body 2 1 2    (2表示2个16进制数字,案例有256个一级子目录,16个二级子目录,256个三集子目录)在/var/tmp/body目录下建256个一级子目录00-ff

  对客户的请求进行限制的先关配置

    20、limit_rate rate;

    限制响应给客户端的传输速率,单位是byte/second,0表示无限制;

    21、limit_except method
    限制使用其他方法除了。。。
      eg:location /download/ {
          limit_except GET POST {
            allow 172.18.100.67/24;
            deny all;
          }
        }

        表示出了get、post的其他方法仅允许 172.18.100.67/24 这个网段的使用

  文件操作优化的方法

    20、aio on|off | 

      是否启用AIO功能:默认是关闭的

      存放的上下文 http、server、location

    21、directio size|off;

      是否启用直接IO:

    22、open_file_cache max=N[inactive=time] | off; N文件数 如果达到N,通过LRU算法实现缓存管理,inactive指定缓存项大超时时长,此处指定的时长内未被命中的缓存在超时时长结束后会被清除。
      nginx可以缓存以下三种信息提升文件访问性能:
        (1) 文件句柄、文件大小和最近一次修改时间;
        (2) 打开目录的目录结构;
        (3) 没有找到的或者没有权限操作的文件的相关信息;
      max=N表示可缓存的最大条目上限;一旦到达上限,则会使用LRU从缓存中删除最近最少使用的条目;
      inactive=time: 在inactive指定的时长内没有被访问过的缓存条目就会淘汰;

    23、open_file_cache_errors on|off;
      是否缓存在文件缓存中缓存打开文件时出现找不到路径,没有权限等的错误信息 (22的第三项)

    24、open_file_cache_min_uses num
      在inactive时间内定义的最小命中数,方可不被归类为非活动项 

    

    

      

  

  

原文地址:https://www.cnblogs.com/Nvax/p/7577125.html