1.Ngjinx 简介

Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。其特点是占有内存少,并发能力强,中国大陆使用nginx网站用户有:新浪、网易、腾讯等。够支持高达 50,000 个并发连接数的响应

2.编译安装Nginx

#添加Nginx运行用户及组
[root@lab1 ~]# groupadd nginx -r
[root@lab1 ~]# useradd -g nginx -r -s /sbin/nologin nginx
#解决依赖关系
[root@lab1 ~]# yum install pcre-devel zlib-devel openssl-devel
#解压并安装
[root@lab1 nginx-1.6.1]# ./configure \
  --prefix=/usr/local/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --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
[root@lab1 nginx-1.6.1]# make && make install 
 
#创建Nginx临时目录
[root@lab1 ~]# mkdir -v /var/tmp/nginx/
mkdir: created directory `/var/tmp/nginx/'
 
 
为Nginx提供启动脚本,脚本内容如下
[root@lab1 ~]# cat /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/local/nginx/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
#现在我们已经可以正常启动并访问Nginx服务器了
chmod +x /etc/init.d/nginx 
[root@lab1 nginx-1.6.1]# service nginx start
Starting nginx:                                            [  OK  ]


3.Nginx配置文件详解

#文档结构
... #运行,性能相关配置
 
event{ #事件相关配置
 
}
http{ #nginx的http web功能配置段
... #http server通用配置段
...
server { #虚拟主机配置段
 
}
server {
...
}
}
 
######################
#其中相关的配置项有:
######################
#运行相关
user USERNAME [GROUPNAME]; #指定 运行用户
pid /path/to/pid/file; #指定pid文件位置
worker_rlimit_nofile #; #指定每一个worker进程可以打开的最大文件数
worker_rlomit_sigpending #; #指定没用用户能够发往worker进程的信号数量
#性能相关配置
worker_processes #; #worker进程数,通常为cpu个数减1
worker_cpu_affinity cpumask ...; #指定每个进程工作在哪个cpu上
如:0001 0010 0100 1000
表示第一个CPU在第一个CPU上运行
表示第二个CPU在第二个CPU上运行
......
ssl_engine_device ; #在存在ssl硬件加速器的服务器上,指定加速设备
worker_priority nice;指定进程运行优先级
nice为-20-19之间的值
#事件相关配置
event{}
accept_mutex on|off #是否打开负载均衡锁
lock_file /path... #lock文件位置
multi_accept on|off #是否允许一此性响应多个用户请求
accept_mutex_delay #  #重试获取accept锁时间间隔
use epool|rtsig|select|poll #定时使用事件模型,建议nginx自动选择
 
#server相关配置
server{} #定义一个虚拟主机
listen #定义监听地址和端口
default_server #定义默认httpserver,未使用此选项,默认为第一个
rcvbuf=SIZE :接受缓冲区大小
sndbuf=SIZE:发送缓冲区大小
server_name ;定义主机名
#可以定义多个,也可以使用通配符和正则表达式匹配
 
#资源定义
location =|~|~*|^~ uri ... {} 
# =:精确匹配;
# ~:正则表达式模式匹配,匹配时区分字符大小写
# ~*:正则表达式模式匹配,匹配时忽略字符大小写
# ^~: URI前半部分匹配,不检查正则表达式
 
# 匹配优先级:
# =,  ^~ , ~ , ~* , 空
root path #在location中定义web资源路径
alias path #定义路径别名
index file ...; #定义默认主页
error_page code ... [=[response]] uri; #定义错误页面重定向
try_files path1 [path2].... uri #自左而右尝试path路径,均不成功时返回最后一各uri

 

4.配置使用nginx

#配置基于端口的虚拟主机及添加认证
  server {
        listen 8000;
        auth_basic "admin area";
        auth_basic_user_file /etc/nginx/.htpasswd;
        location / {
                root /web/test;
                index index.html;
        }
    }
 #使用httpd的htpasswd生成用户名和密码
 [root@lab1 html]#  htpasswd -c /etc/nginx/.htpasswd admin
 #创建网站根录及文件
 [root@lab1 html]#  mkdir /web/test -p
 [root@lab1 html]# echo '172.16.21.101:8000' > /web/test/index.html

Nginx 基本使用_nginx


未完待续.....