一、) 安装Nginx
1.) 安装
Nginx发音为[engine x],是由俄罗斯人Igor Sysoev建立的项目,基于BSD许可。据说他当初是F5的成员之一,英文主页:http://nginx.net。俄罗斯的一些大网站已经使用它超过两年多了,一直表现不凡。
安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等.
安装pcre
下载地址: http://www.pcre.org/
下载适合自己的版本,然后进行安装:
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7
make
make install
安装zlib
下载地址: http://www.zlib.net/
下载适合自己的版本,然后进行安装:
tar zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
make
make install
下载地址: http://www.nginx.net/
等待pcre和zlib安装完毕,开始安装nginx
下载适合自己的版本,然后编译安装:
Nginx的编译参数如下:
[root@oracle132 /]# tar zxvf nginx-0.6.31
[root@oracle132 nginx-0.6.31]# cd nginx-0.6.31
特别说明:Nginx需要PCRE模块的支持,但在RHEL下,即便已经安装PCRE模块,Nginx编译时还是不能正确找到相关库文件,因此需要做以下变通。
[root@oracle132 nginx-0.6.31]# mkdir /usr/include/pcre
[root@oracle132 nginx-0.6.31]#cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.a
[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.la
[root@oracle132 nginx-0.6.31]# cp /oracle/pcre-7.7/pcre.h /usr/include/pcre/pcre.h
[root@oracle132 nginx-0.6.31]# mkdir /usr/include/pcre/.libs
[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a
[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la
[root@oracle132 nginx-0.6.31]# cp /oracle/pcre-7.7/pcre.h /usr/include/pcre/.libs/pcre.h
上面变通操作完毕,接下来开始编译安装.
[root@oracle132 nginx-0.6.31]# ./configure --with-pcre=/usr/include/pcre --with-http_stub_status_module
[root@oracle132 nginx-0.6.31]# vi ./objs/Makefile(注:删除此文件1006行“./configure --disable-shared”)
[root@oracle132 nginx-0.6.31]#make
[root@oracle132 nginx-0.6.31]#make install
[root@oracle132 nginx-0.6.31]#
[root@oracle132 nginx-0.6.31]#
安装完毕,默认nginx安装到了/usr/local/下,进入nginx文件夹,打开配置文件!
[root@oracle132 conf]# pwd
/usr/local/nginx/conf
2)nginx的配置文件详解
[root@oracle132 conf]# vi nginx.conf
user nobody nobody; #运行用户
worker_processes 1; #启动进程
#全局错误日志及PID文件
#error_log logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
#工作模式及连接数上限
events {
use epoll;
worker_connections 1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型
include 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 logs/access.log main;
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#设定access log
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#启用网页压缩
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1100;
gzip_buffers 4 8k;
#设定负载均衡的服务器列表
upstream ixdba{
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的Squid开启3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
#下面是配置虚拟主机
include /usr/local/nginx/conf/proxy.conf;
include /usr/local/nginx/conf/vhosts/www.test.com.conf;
3)虚拟主机配置文件详解
由于虚拟主机分别有一个文件来指定,下面举例某个虚拟主机的配置如下:
[root@oracle132 vhosts]#
vi /usr/local/nginx/conf/vhosts/www.test.com.conf;
server {
listen 80; #虚拟主机使用端口
server_name www.test.com; #虚拟主机访问域名
charset UTF-8; #设定nginx默认字符编码
#access_log logs/host.access.log main;
#所有jpg格式的图片都有nginx来处理
location ~ \.jpg$ {
root /cicro/cws3/vhosts/www.test.com/ROOT;
expires 30d;
}
#所有gif格式的图片都有nginx来处理
location ~ \.gif$ {
root /cicro/cws3/vhosts/www.test.com/ROOT;
expires 30d;
}
# upload和html下所有文件都有nginx来处理
location ~ ^/(upload|html)/ {
root /cicro/cws3/vhosts/www.test.com/ROOT;
expires 30d;
}
#除去上面的文件,剩下的所有都代理给http://127.0.0.1:8009来访问
location / {
root /cicro/cws3/vhosts/www.test.com/ROOT;
index index.html;
proxy_pass http://127.0.0.1:8009;
}
#设定查看Nginx状态的地址
location /NginxStatus {
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file ../htpasswd;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
在上面有设置查看Nginx状态的地址,需要apache的htpasswd 来生成一个登录验证文件,这样生成一个htpasswd 文件:
[root@oracle132 vhosts]# /usr/local/bin/htpasswd -c htpasswd gaojf
New password: (此处输入您的密码)
Re-type new password: (再次输入您的密码)
Adding password for user gaojf
上面 /usr/local/bin/htpasswd 是htpasswd 文件的执行路径,如果没有这个文件,可以从apache的bin目录拷贝一个过来即可!
-c是创建一个文件
-c后面的httpasswd是创建验证文件的名字.
gaojf是创建的用户
#查看nginxstatus:
http://www.test.com/nginxstatus/,输入验证帐号密码,即可看到类似如下内容:
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324
第一行表示现在活跃的连接数
第三行的第三个数字表示Nginx运行到当前时间接受到的总请求数,假如快达到了上限,就需要加大上限值了。
CODE:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# 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
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
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
################################################
#!/bin/bash
# v.0.0.1
# create by jackbillow at 2007.10.15
# nginx - This shell script takes care of starting and stopping nginx.
#
# chkconfig: - 60 50
# description: nginx [engine x] is light http web/proxy server
# that answers incoming ftp service requests.
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
nginx_path="/usr/local/nginx"
nginx_pid="/var/run/nginx/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
[ -x $nginx_path/sbin/nginx ] || exit 0
RETVAL=0
prog="nginx"
start() {
# Start daemons.
if [ -e $nginx_pid -a ! -z $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
if [ -e $nginx_path/conf/nginx.conf ];then
echo -n $"Starting $prog: "
$nginx_path/sbin/nginx -c $nginx_path/conf/nginx.conf &
RETVAL=$?
[ $RETVAL -eq 0 ] && {
touch /var/lock/subsys/$prog
success $"$prog"
}
echo
else
RETVAL=1
fi
return $RETVAL
}
# Stop daemons.
stop() {
echo -n $"Stopping $prog: "
killproc -d 10 $nigx_path/sbin/nginx
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $nginx_pid /var/lock/subsys/$prog
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reconfigure)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|reconfigure|status}"
exit 1
esac
exit $RETVAL