Chinaunix首页 | 论坛 | 博客
  • 博客访问: 92915
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-04 22:47
文章分类

全部博文(34)

文章存档

2017年(34)

我的朋友

分类: 服务器与存储

2017-04-09 23:06:14

nginx的模块类别:
      核心模块
      标准http模块
      可选的http模块
      邮件模块
      第三方扩展模块


安装方法:
      编译安装
      rpm包安装
          epel源

Nginx的配置文件:
        main配置段
        http {

        }

配置参数需要以分号结尾,语法格式:
       参数名   值1 [值2 ...];

       还支持使用变量;
              模块内置变量
              用户自定义变量
                     set var_name

      Nginx基本配置的类别:
          用于调试、定位问题
          正常运行的必备配置
          优化性能的配置
          事件类的配置
    
    worker进程应该以普通用户身份运行: nginx用户、nginx组:

        HTTP的方法:GET,HEAD,  POST,PUT,DELETE,OPTIONS,TRACE  

        nginx的配置:
             正常运行的必备配置:
             1、user username
                     指定运行worker进程的用户和组
   
             2、pid    /path/to/pidfile_name
                     指定nginx的pid文件
     
             3、worker_rlimit_nofile #;
                    指定一个worker进程所能够打开文件个数

             4、woker_rlimit_sigpending #;
                    设定每个用户能够发往woker进程的信号的数量;

          优化性能相关的配置:
              1、worker_processes #;
                     worker进程的个数:通常其数值应该为CPU的物理核心数减1;

              2、worker_cpu_affinity cpumask ...;
                    
              3、ssl_engine device;
                     在存在ssl硬件加速器的服务器上,指定所使用的ssl硬件加速设备:
  
              4、timer_resolution t;
                     每次内核事件调用返回时,都会使用gettimeofday()来更新nginx缓存时钟:timer_resolution用于定义每隔多久才会由gettimeiofday()更新一次缓存时钟:x86-64系统上,gettimeofday()代价已经很小,可以忽略此配置:
 
              5、worker_priority nice;
                   -20,20之间的值:

          事件相关的配置
               1、accept_mutex [on|off]
                       是否打开nginx的负载均衡锁:此锁能够让多个 worker进程轮流地、序列化地与新的客户端建立连接:而通常一个worker进程负载达到上限的7/8时,master就尽可能不再将请求调度至此worker:

               2、lock_file  /path/to/lock_file;
                     lock文件

               3、accept_mutex_delay #ms;
                     accept锁模式中,一个worker进程为取得accept锁的等待时长:如果某worker进程在某次试图取得锁时失败了,至少要等待#ms才能再依次请求锁:

               4、multi_accept on|off;
                      是否允许一次性地响应多个用户请求:默认为off;

               5、use [epoll|rtsig|select|poll]
                      定义使用的事件模型,建议让nginx自动选择:

               6、worker_connections #;
                       每个worker能够并发响应的最大请求数;

        用于调试、定位问题:只调试nginx时使用
               
                1、  daemon [on|off]
                        是否让nginx运行于后台:默认为on,调试时可以设置为off,使得所有信息直接输出至控制台:

                2、master_process on|off
                        是否以master/worker模式运行nginx:默认为on:调试时可以设置为off以方便追踪:

                3、error_log /path/to/error_log  level;
                        错误日志文件及其级别;调试是可以使用debug级别,默认为error级别,但要求在编译时必须使用--with-debug启用debug功能;
 
         nginx的 http功能:
               必须使用虚拟主机来配置站点:每个虚拟主机使用一个server {}段配置:
                  server{

                    }
              非虚拟主机的配置或公共配置,需要定义在server之外,http之内:
                http {
                       directive  value;
                         ...

                       server  {
                       
                        }
                           ...
                       }
                       
                               
             1、server {}
                     定义一个虚拟主机:nginx仅支持使用基于主机名称或IP的虚拟主机;

            2、Listen 
                     listen address[:port];
                     listen port
                    
                     default_server:定义此server为http中默认的server:如果所有的server中没有任何一个listen使用此参数,那么第一个server即为默认server
                     rcvbuf=SIZE:接收缓冲大小;
                     sndbuf=SIZE : 发送缓冲区大下;
                     ssl:https server;

            3、server_name [...];
                     server_name 可以跟多个主机名;当nginx收到一个请求时,会取出其首部的server的值,而后跟众server_name进行比较:比较方式

                     (1)先做精确匹配:
                     (2)左侧通配符匹配:
                     (3)右侧通配符匹配:
                     (4)正则表达式匹配:
             
            4、server_name_hash_bucket_size  32|64|128;
                     为了实现快速主机查找,nginx使用hash来保存主机名:

            5、location URI
                 location  @name {...}

               功能:允许根据用户请求的URI来匹配指定的各location以进行访问配置;匹配到时,将被location块中的配置锁处理:
  
                =: 精确匹配;
                ~:正则表达式模式匹配,匹配时区分字符大小写
                ~*:正则表达式模式匹配,匹配时忽略字符大小写
                ^~:URI前半部分匹配,忽略正则表达式
               
               
                    
                                          
              

              

             
                    
             
                 


一、安装nginx

1、安装依赖包

yum -y groupinstall "Development Tools" "Server Platform Development"

yum install pcre-devel

2、创建nginx用户和nginx组

[root@localhost ~]# groupadd -r nginx

[root@localhost ~]# useradd -r -g nginx nginx

3、解压配置和编译nginx源码包

tar -zxvf nginx-1.10.3.tar.gz

cd nginx-1.10.3

[root@localhost nginx-1.10.3]# ./configure \
> --prefix=/usr \
> --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/nginx.pid \
> --lock-path=/var/lock/nginx.lock \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_flv_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --http-client-body-temp-path=/var/tmp/nginx/client/ \
> --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
> --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
> --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
> --http-scgi-temp-path=/var/tmp/nginx/scgi \
> --with-pcre

make && make install

4、检测nginx
[root@localhost usr]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

提示缺少/var/tmp/nginx的目录
创建该目录
[root@localhost usr]# mkdir /var/tmp/nginx

再重新检测,通过
[root@localhost usr]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

5、编辑nginx启动脚本
vim /etc/init.d/nginx
#!/bin/bash
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#                          proxy and IMAP/POP3 proxy server
#
# processname: nginx
# config:         /etc/nginx/nginx.conf
# pidfile:       /var/run/nginx/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/nginx.lock
start() {
  [ -x $nginx ] || exit 5
  [ -f $NGINX_CONF_FILE ] || exit 6
  echo -n $"Starting $prog: "
  daemon $nginx -c $NGINX_CONF_FILE
  retval=$?
  echo
  [ $retval -eq 0 ] && touch $lockfile
  return $retval
}
stop() {
  echo -n $"Stopping $prog: "
  killproc $prog -QUIT
  retval=$?
  echo
  [ $retval -eq 0 ] && rm -f $lockfile
  return $retval
}
restart() {
  configtest || return $?
  stop
  sleep 1
  start
}
reload() {
  configtest || return $?
  echo -n $"Reloading $prog: "
  killproc $nginx -HUP
  RETVAL=$?
  echo
}
force_reload() {
  restart
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
  status $prog
}
rh_status_q() {
  rh_status >/dev/null 2>&1
}
case "$1" in
  start)
    rh_status_q && exit 0
    $1
    ;;
  stop)
    rh_status_q || exit 0
    $1
    ;;
  restart|configtest)
    $1
    ;;
  reload)
    rh_status_q || exit 7
    $1
    ;;
  force-reload)
    force_reload
    ;;
  status)
    rh_status
    ;;
  condrestart|try-restart)
    rh_status_q || exit 0
      ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
    exit 2
esac

6、给脚本加执行权限,并启动脚本

[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# service nginx start
正在启动 nginx:                                           [确定]


















阅读(1294) | 评论(0) | 转发(0) |
0

上一篇:DNS详解

下一篇:ansible安装及使用

给主人留下些什么吧!~~