这两天一直在看Nginx相关视频资料,但是由于知识点是在太多,所以在看过视频之后总是记不住,做过的实验第二天也忘了怎么做,所以决定将学习笔记写到博客里面加深映像,学习本来就是一个举一反三的过程,温故知新嘛。


一、Nginx的安装

Nginx的安装很简单,有两种安装方式:

1、源码编译安装。

去官网下载源码包,地址:http://nginx.org/ ,生产环境最好是下载stabe版本。要稳定一些。


安装过程如下:

安装编译需要的环境:

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


安装依赖包 

yum -y install pcre-devel openssl-devel zlib-devel


创建nginx用户和组

useradd -r nginx


创建目录

mkdir /var/tmp/nginx



编译安装:

./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/locl/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


测试配置是否正确

/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


启动nginx

 /usr/sbin/nginx


nginx启动脚本,/etc/init.d/nginx


#!/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/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
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



2、RPM包安装

官网下载stabe版本的RPM包安装,地址:http://nginx.org/packages/centos/6/x86_64/RPMS/

或者创建一个repo文件,文件内容如下:然后用yum安装,过程如下:

#vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

注意baseurl中6可以用数字替换,centos也可以用rhel代替,根据自己系统的版本来设置URL地址。

下面是官网链接:http://nginx.org/en/linux_packages.html#stable


二、Nginx的配置文件

我是下载RPM包安装的。生成配置文件路径如下:

/etc/nginx/nginx.conf    

这个文件是nginx的全局配置文件,包含了全局配置,event段相关设定,http段相关设定。

注意,event段和http段有花括号引起来。文件结构为:

event {

...

}

http {

...

}

文件内容如下:

###全局配置
user  nginx;      ###指定nginx启动用户   
worker_processes  4;  ###启动nginx服务时,启动的worker process数 可以设置为auto,但一般设置为与CPU核心数相等,比如,CPU为4核,则设置为4,或者设置为CPU核心数减去1。
error_log  /var/log/nginx/error.log warn;   ###错误日志的路径
pid        /var/run/nginx.pid;  ###pid的路径
###event段设置
events {
    worker_connections  1024;   ###每个进程的最大连接数,与worker_processes数相乘为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 "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;  ###包含了/etc/nginx/conf.d目录下所有以".conf"结尾的文件,注意这条很重要,因为在默认配置中没有server段的配置,而一般都是将server的配置放在
/etc/nginx/conf.d/目录下。
}

正常情况下http段配置结构如下:

http {
      ...
    server {
          ...
        location [PATTERN]  {
               ...
        }
        location [PATTERN]  {
              ...
       }
    }
    
  server {
         ...
  }
}

每一个http段中可以包含多个server段,每一个server段可以包含多个location段。不同的server配置最好是用单独的文件存放,都放在/etc/nginx/conf.d/目录下,只要以".conf"结尾即可


三、Nginx相关命令和服务启动停止

运行nginx  -h查看nginx的相关命令参数

root@localhost:~# nginx -h
nginx version: nginx/1.8.1
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /etc/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

用的较多就是

nginx  -s  reload  :重新加载nginx的配置文件;

nginx  -t:检查配置文件语法是否有问题;


Nginx服务启动、停止、重新加载

#service nginx start  | stop | reload

#/etc/init.d/nginx  start | stop | reload