Nginx基本概念和安装

nginx做为一个轻量级的WED服务,它有着比apache更好的对Fcgi的支持、处理连接方式上,nginx支持epoll,而apache却不支持;

nginx的优势分析:

  • 作为web服务器,Nginx处理静态文件,索引文件,自动索引的效率非常高

  • 作为代理服务器,nginx可以实现无缓存的反向代理加速,提高网站运行速度

  • 作为负载均衡服务器,nginx既可以在内存支持Rails和PHP,也可以支持HTTP代理服务器对外进行服务,同时还支持简单的容错和利用算法进行负载均衡

  • 在性能方面,nginx是专门为性能优化而开发的,时间上非常注重效率,它采用内存poll模型,可以支持更多的并发连接,最大可以支持对50000个并发连接的响应,而且只占用很低的内存资源

  • 在稳定性方面,nginx采用了分阶段资源分配技术,使得CPU与内存的占用非常低。

  • 在高可用性方面,nginx支持热部署,启动速度特别迅速,可以在不间断服务的情况下,对软件版本或者配置进行升级。

Nginx模块与工作原理:

  • nginx由内核和模块组成

  • 内核的工作:通过查找配置文件将客户端请求映射到一个location block中(location是配置文件中用来匹配URL的),在这个location中定义了很多指令,这些指令将会启动不同的模块去完成相应的工作。

  • nginx的模块分为:核心模块、标准模块、第三方模块

动静态编译:

  • nginx的模块直接被编译进nginx,因此属于静态编译方式。启动nginx会被自动加载。

  • apache,首先将模块编译成一个so文件,然后在配置文件中指定是否进行加载

nginx的安装:

1、解决依赖关系

# yum groupinstall "Development Tools""Server Platform Deveopment"
# yum install openssl-devel pcre-devel
2、添加用户
首先添加用户nginx,实现以之运行nginx服务进程:
# groupadd -r nginx
# useradd -r -g nginx nginx
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提供开机自动启动脚本

#vim /etc/rc.d/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; doif [ `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 $lockfilereturn $retval
}
 
stop() {
    echo -n $"Stopping $prog: "killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfilereturn $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"instart)
        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
5、为此脚本赋予执行权限
# chmod +x /etc/rc.d/init.d/nginx

6、添加至服务管理列表,并让其开机自动启动

# chkconfig --add nginx
# chkconfig nginx on

7、启动服务并测试

# service nginx start

DHB%GR1E$T){0PICLH7[HLN

你可能感兴趣的:(nginx,Web)