一、) 安装Nginx
1.) 安装
Nginx发音为[engine x],是由俄罗斯人Igor Sysoev建立的项目,基于BSD许可。据说他当初是F5的成员之一,英文主页:http://nginx.net。俄罗斯的一些大网站已经使用它超过两年多了,一直表现不凡。
安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等.
安装pcre
下载地址:
下载适合自己的版本,然后进行安装:
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7
make
make install
安装zlib
下载地址:
下载适合自己的版本,然后进行安装:
tar zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
make
make install
下载地址:
等待pcre和zlib安装完毕,开始安装nginx
下载适合自己的版本,然后编译安装:
Nginx的编译参数如下:
[root@oracle132 /]# tar zxvf nginx-0.6.31
[root@oracle132 nginx-0.6.31]# cd
特别说明: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
[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a
[root@oracle132 nginx-0.6.31]# cp /oracle/pcre-7.7/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
[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a
[root@oracle132 nginx-0.6.31]# cp /oracle/pcre-7.7/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
worker_processes
#全局错误日志及PID文件
#error_log
error_log
#error_log
pid
#工作模式及连接数上限
events {
use epoll;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型
include
#设定日志格式
#
#access_log logs/access.log main;
#设定请求缓冲
client_header_buffer_size
large_client_header_buffers 4 4k;
#设定access log
#启用网页压缩
#设定负载均衡的服务器列表
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
3)虚拟主机配置文件详解
由于虚拟主机分别有一个文件来指定,下面举例某个虚拟主机的配置如下:
[root@oracle132 vhosts]#
vi /usr/local/nginx/conf/vhosts/www.test.com.conf;
server {
#所有jpg格式的图片都有nginx来处理
#所有gif格式的图片都有nginx来处理
# upload和html下所有文件都有nginx来处理
#除去上面的文件,剩下的所有都代理给http://127.0.0.1:8009来访问
#设定查看Nginx状态的地址
auth_basic_user_file
}
[root@oracle132 vhosts]# /usr/local/bin/htpasswd
New password:
Re-type new password:
Adding password for user
上面 /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