安装前准备工作

    1.下载nginx-1.6.2.tar.gz源码包

            wget http://nginx.org/download/nginx-1.6.2.tar.gz                    编译安装nginx_工具包

    2安装开发工具包组"Development tools" "Server Platform Development"

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

    3.安装nginx所依赖的pcre-devel openssl-devel

yum install openssl-devel pcre-devel -y

 

安装过程:

 

    1.创建系统用户和组,解压源码包

           groupadd -r nginx

           useradd -r -g nginx nginx

           tar xf nginx-1.6.2.tar.gz

          编译安装nginx_开发_02

 

2.对软件进行配置,检查当前的环境是否满足要安装软件的依赖关系

./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_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_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/fastcgi编译安装nginx_开发_03    

--prefix=/usr/local/nginx

指定程序安装位置

--conf-path=/etc/nginx/nginx.conf

指定配置文件路径

--user=nginx

指定以nginx用户身份运行

--group=nginx

指定以nginx组运行

--error-log-path=/var/log/nginx/error.log

指定错误日志文件路径

--http-log-path=/var/log/nginx/access.log

指定访问日志文件路径

--pid-path=/var/run/nginx/nginx.pid

指定pid文件路径

--lock-path=/var/lock/nginx.lock

指定锁文件路径

--with-http_ssl_module

支持ssl模块

--with-http_stub_status_module

启用NginxStatus功能,以监控Nginx当前状态

--with-http_gzip_static_module

启用gzip压缩模块

--with-http_flv_module

启用模块支持flv流媒体

--with-http_mp4_module

启用模块支持mp4流媒体

--http-client-body-temp-path=/var/tmp/nginx/client

指定客户端请求body部分临时文件路径

--http-proxy-temp-path=/var/tmp/nginx/proxy

指定proxy临时文件保存路径

--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi

指定fastcgi临时文件保存路径

 

 

    3.安装 make && make install

编译安装nginx_开发_04

    4.进入安装目录,通过二进制文件启动nginx

             编译安装nginx_用户_05

        

    5.通过ps aux查看进程,nginx已经启动

编译安装nginx_工具包_06

    6.测试访问

编译安装nginx_用户_07

编写服务脚本:

 

    1.创建文件/etc/rc.d/init.d/nginx

            vim /etc/rc.d/init.d/nginx

    2.保存服务脚本如下:

#!/bin/sh

#

# 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

# config:      /etc/sysconfig/nginx

# pidfile:     /var/run/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/local/nginx/sbin/nginx"

prog=$(basename $nginx)

 

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

 

lockfile=/var/lock/nginx.lock

 

make_dirs() {

   # make required directories

   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

   options=`$nginx -V 2>&1 | grep 'configure arguments:'`

   for opt in $options; do

       if [ `echo $opt | grep '.*-temp-path'` ]; then

           value=`echo $opt | cut -d "=" -f 2`

           if [ ! -d "$value" ]; then

               # echo "creating" $value

               mkdir -p $value && chown -R $user $value

           fi

       fi

   done

}

 

start() {

    [ -x $nginx ] || exit 5

    [ -f $NGINX_CONF_FILE ] || exit 6

    make_dirs

    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

 

  3.添加执行权限并使用服务脚本启动服务

        chmod +x /etc/rc.d/init.d/nginx

        chkconfig --add nginx

        service nginx start

        编译安装nginx_开发_08

编译安装nginx_工具包_09

 

 

 

配置文件参数:

 

    1.main配置

 #user  nobody;            指定运行worker进程的用户和组

worker_processes  1;       worker线程的个数;通常为物理CPU核心数减1

 

#error_log  logs/error.log;    指定错误日志文件路径

 

#pid        logs/nginx.pid;    指定pid文件路径

 

 

events {

   worker_connections  1024; 每个worker进程所能够响应的最大并发请求数

}

    2.虚拟主机配置

http {

    include       mime.types;

    default_type  application/octet-stream   设置mime类型

 

    #access_log  logs/access.log  main;  定义访问日志路径

 

    sendfile        on;  指定 nginx 是否调用 sendfile 函数来输出文件

    #tcp_nopush     on;              防止网络阻塞

    keepalive_timeout  65;    长连接超时时长

 

    #gzip  on;    是否启用gzip压缩

 

    server {                                          定义一个虚拟主机

        listen       80;              指定监听的端口

        server_name  localhost;        指定主机名

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;   定义虚拟主机访问日志

 

        location / {        定义为web资源路径

            root   /web/html;    设置web资源路径映射

            index  index.html index.htm;  设置默认页面

                            rewrite ^/bbs/(.*) /forum/$1;   bbs目录下内容重写到forum

                            valid_referers none;  定义合法引用

                            if ($invalid_referer) {                  防盗链定义

         return 403;

}

add_header X-header testheader;  自定义响应首部

                            allow 172.16.0.0/16;  基于ip的访问控制,允许172.16段访问

                            deny all基于ip的访问控制,不允许其他地址访问

        }

 

                   location /p_w_picpaths/{           

                                     root "/web/imgs";    定义一个路径映射

                   用户访问http://ip/p_w_picpaths/  实际访问路径映射为/web/imgs/p_w_picpaths

                   }

 

                   location /pic/ {

                            alias /www/pictures/;    定义一个路径别名

                   用户访问http://ip/pic   实际访问路径映射为/www/pictures/

                   }

}

 

   3.网络连接相关配置:

 

Keepalive_timeout time;

                            保持连接的超时时长,默认为75s

                  Keepalive_requests #:

                            在一次保持连接上允许承载的最大资源请求数

                  Keepalive_disable [msie6|safari|none]

                            对指定类型的浏览器禁用长连接

                  tcp_nodelay on|off

                           对长连接是否使用tcpnodelay选项 

                  client_header_timeout time;

                            读取http请求报文首部的超时时长;

                  client_body_timeout time ;

                           读取http请求报文body部分的超时时长

                  send_timeout time;

                            发送响应报文的超时时长

 

         4. 对客户端的请求进行限制:

                   limit_except METHOD{...}

                            指定对范围之外的其他方法进行限制

                                     limit_except GET{

                                               allow 172.16.0.0/16;

                                               deny all;

                                     }

                                              

                   client_body_max_size SIZE;

限制请求报文中body部分的上限;通过检测请求报文首部中的"Content_Length"来判定;

                   limit_rate speed:

                            限制客户端每秒传输的字节数,默认为0,表示无限制;

                                    

5.对内存或磁盘资源进行分配

                   client_body_in_file_only on|clean|off;

                            请求报文的body是否存储到磁盘中

                            on:允许且请求结束也不会删除暂存内容

                            clean:允许但请求结束后删除暂存内容

                            off:关闭此功能

                   client_body_in_single_buffer on|off

                            请求报文的body部分是否暂存在内存的buffer

                   client_body_buffer_size size

                   client_body_tmp_path dir [level1[level2]]

                            例如:client_body_tmp_path /dir 1 2

                   clent_header_buffer_size:

请求报文header部分分配的内存空间大小