<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

实验环境: web Server IP:10.0.0.252 

[root@Bhanv ~]# ls nginx-<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />0.8.20.tar.gz

nginx-0.8.20.tar.gz

[root@Bhanv ~]# tar zxf nginx-0.8.20.tar.gz  -C /tmp/

[root@Bhanv ~]# cd /tmp/nginx-0.8.20/

[root@Bhanv nginx-0.8.20]#. /configure

若提示: /configure: error: the HTTP rewrite module requires the PCRE library 则:

[root@Bhanv nginx-0.8.20]# yum install pcre  pcre-devel –y

[root@Bhanv nginx-0.8.20]# make ;make install

[root@Bhanv nginx-0.8.20]# cd /usr/local/nginx/

[root@Bhanv nginx]# ls

conf  html  logs  sbin

其中 conf 为配置文件目录    html 为网页文件存放目录 logs 顾名思义   sbin 为启动脚本目录

[root@Bhanv nginx]# /usr/local/nginx/sbin/nginx

[root@Bhanv nginx]# netstat -antlp |grep 80

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      8136/nginx

测试可得到主页

[root@Bhanv nginx]# links http://localhost

 

                      Welcome to nginx

[root@Bhanv html]# echo "Bhanv study nginx test page" > index.html

[root@Bhanv nginx]# links http://localhost

              Bhanv study nginx test page

虚拟目录: nginx 貌似没有虚拟目录的说法,因为它本来就是完完全全根据目录来设计并工作的。
如果非要给 nginx 安上一个虚拟目录的说法,那就只有 alias 标签比较 ,干脆来说说 alias 标签和 root 标签的区别吧。
最基本的区别: alias 指定的目录是准确的,如:

   [root@Bhanv html]# vi ../conf/nginx.conf   

  location /wen/{

        alias /usr/local/nginx/html/wen/;

}

[root@Bhanv html]# echo "virtual directory test" >/usr/local/nginx/html/wen/index.html

为了服务管理方便 , /etc/init.d/ 创建一个管理脚本

#!/bin/bash
#Use:Startup script for the nginx HTTP Server

#Definition Global environment variable
IFS=$' \t\n'
unset -f unalias
\unalias -a
unset -f command
SYSPATH="$(command -p getconf PATH >/dev/null 2>&1)"
if [[ -z "$SYSPATH" ]];then
SYSPATH="/bin:/usr/bin"
fi
export PATH="$SYSPATH:$PATH"

#Definition nginx directory & service name
NGINX_DIR="/usr/local/nginx"
PROG="nginx"

#Definition check listen conflict function
check_listen(){
echo -n "Checek $PROG Listen port status: "
NGINX_LISTEN=(`grep "^listen" ${NGINX_DIR}/conf/$PROG.conf | awk '{print $NF}' | cut -d";" -f1 | uniq`)
SYSTEM_LISTEN=(`netstat -tnl |awk '{print $4}' | grep [0-9] | awk -F":" '{print $NF}' | uniq `)
for FIR in ${NGINX_LISTEN[*]}; do
for SEC in ${SYSTEM_LISTEN[*]};do
if [ $FIR == $SEC ]; then
echo "Has the listen port conflict, please check $PROG config file ${NGINX_DIR}/conf/$PROG.conf"
break
fi
done
done
echo "listen prot status OK!"

}

#definition check uid function
check_uid(){
if [ `id -u ` != "0" ];then
echo "Error,The Script must be root to run"
exit 1
fi
}

#Definition start function
start() {
check_uid
${NGINX_DIR}/sbin/$PROG -t >/dev/null 2>&1
if [ $? == "0" ]; then
if [ `ps aux | grep $PROG | grep -c "master"` == "1" ]; then
echo -n $"Starting $PROG: "
echo "$PROG is already running"
exit 1
else
check_listen
${NGINX_DIR}/sbin/$PROG
echo -n $"Starting $PROG: "
echo "$PROG START OK"
fi
else
echo "$PROG syntax error,please check $PROG config file ${NGINX_DIR}/conf/$PROG.conf"
exit 2
fi
}

#Definition stop function
stop() {
check_uid
echo -n $"Stopping $PROG: "
if [ `ps aux | grep $PROG | grep -c "master"` == "1" ]; then
pkill $PROG
echo "$PROG stop OK"
else
echo "$PROG process does not exist.so $PROG stop failed"
fi
}

#Definition reload function
reload() {
check_uid
echo -n $"Reloading $PROG: "
${NGINX_DIR}/sbin/$PROG -t >/dev/null 2>&1
if [ $? == "0" ]; then
if [ `ps aux | grep $PROG | grep -c "master"` == "1" ]; then
kill -HUP `cat ${NGINX_DIR}/logs/$PROG.pid`
echo "$PROG reload OK"
exit 0
else
echo "$PROG process does not exist.so $PROG reload failed"
exit 1
fi
else
echo "$PROG syntax error,please check $PROG config file ${NGINX_DIR}/conf/$PROG.conf"
exit 2
fi
}

#Definition status function
status() {
if [ `ps aux | grep $PROG | grep -c "master"` == "1" ]; then
echo "$PROG status is running"
else
echo "$PROG status is stop"
fi
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
reload
;;
configtest)
check_uid
${NGINX_DIR}/sbin/$PROG -t
;;
status)
status
;;
listentest)
check_uid
check_listen
;;
*)
echo $"Usage: $0{start|stop|restart|reload|configtest|status|listentest}"
exit 1
esac

#exit status code
exit $?

添加个执行权限

[root@Bhanv rc3.d]# chmod o+x  /etc/init.d/nginx

启动服务测试 :

[ root@Bhanv rc3.d]# /etc/init.d/nginx restart

Stopping nginx: nginx process does not exist.so nginx stop failed

Checek nginx Listen port status: listen prot status OK!

Starting nginx: nginx

测试 :

[root@Bhanv html]# links http://localhost/wen/

 

virtual directory test

root 是指定目录的上级目录,并且该上级目录要含有 location 指定名称的同名目录

如果使用 root 则是 :

        location /wen/{

        root /usr/local/nginx/html/;

}

测试结果是一样的 ;

虚拟主机 :

双域名 :

[root@Bhanv html]# nslookup

> www.example.com

Server:         10.0.0.252

Address:        10.0.0.252#53

Name:   www.example.com

Address: 10.0.0.252

> www.yema.com

Server:         10.0.0.252

Address:        10.0.0.252#53

Name:   www.yema.com

Address: 10.0.0.252

 

编辑主配置文件 : 添家内容如下 :

  server {

        listen       80;

        listen       www.example.com:8080;

        server_name  www.example.com;

       location / {

            root   /usr/local/nginx/html/example.com;

            index  index.html index.htm;

        }

    }

    server {

        listen       80;

        listen       www.yema.com:8080;

        server_name  www.yema.com;

        location / {

            root   /usr/local/nginx/html/yema.com;

            index  index.html index.htm;

        }

}

 [ root@Bhanv nginx]# mkdir /usr/local/nginx/html/yema.com

[root@Bhanv nginx]# echo "yema virtual host server test" >/usr/local/nginx/html/yema.com/index.html

[root@Bhanv nginx]# mkdir /usr/local/nginx/html/example.com

[root@Bhanv nginx]# echo "example virtual host server test" >/usr/local/nginx/html/example.com/index.html

[root@Bhanv nginx]# /etc/init.d/nginx stop

Stopping nginx: 已终止

[root@Bhanv nginx]# /etc/init.d/nginx start

Checek nginx Listen port status: listen prot status OK!

Starting nginx: nginx START OK

测试 :

[root@Bhanv nginx]# links http://www.yema.com

 

yema virtual host server test

[root@Bhanv nginx]# links http://www.example.com

 

example virtual host server test