您的位置:首页 > 运维架构 > Nginx

原创Linux下搭建Nginx环境的搭建(支持正则)

2010-08-19 14:59 351 查看
【转载时请以超链接形式标明文章出处和作者信息及本声明】
原文链接: 一、) 安装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运行到当前时间接受到的总请求数,假如快达到了上限,就需要加大上限值了。

本篇文章来源于锋芒网(www.ixdba.net) 原文链接:http://www.ixdba.net/a/web/nginx/2010/0422/41.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: