Chinaunix首页 | 论坛 | 博客
  • 博客访问: 383227
  • 博文数量: 146
  • 博客积分: 7142
  • 博客等级: 少将
  • 技术积分: 975
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-03 09:43
文章分类

全部博文(146)

文章存档

2012年(1)

2011年(5)

2010年(24)

2009年(116)

我的朋友

分类: LINUX

2009-12-12 01:51:22

Nginx+JSP环境:Centos5 Nginx-0.7.14.tar.gz Tomcat5.5.20

1 安装pcre
为了确保能在 Nginx 中使用正则表达式进行更灵活的配置,安装之前需要确定系统是否安装有 PCRE(Perl Compatible Regular Expressions)包,rpm包和tar.gz都可以
Rpm包如下:
pcre-6.6-1.1
pcre-devel-6.6-1.1
tar.gz包
#wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
#tar zxvf pcre-7.7.tar.gz
#cd pcre-7.7
# ./configure
# make
# make install
如果没有的话会报类似如下错误:
./configure: error: the HTTP rewrite module requires the PCRE library. 
You can either disable the module by using --without-http_rewrite_module 
option, or install the PCRE library into the system, or build the PCRE library 
statically from the source with nginx by using --with-pcre= option.

2 安装 nginx
#wget
# tar zxvf nginx-0.7.14.tar.gz 
#cd nginx-0.7.14
#./configure --prefix=/usr/local/nginx --with-http_stub_status_module
#make
#make install

说明:
参数 --with-http_stub_status_modul是为了启用nginx的 NginxStatus 功能,用来监控 Nginx 的当前状态
--prefix=/usr/local/nginx 指定安装目录更详细的参数参考./configure --help
安装成功后 /usr/local/nginx 目录下有四个子目录分别是:conf、html、logs、sbin 。其中 Nginx 的配置文件存放于 conf/nginx.conf,Nginx 只有一个程序文件位于 sbin 目录下的 nginx 文件。确保系统的 80 端口没被其他程序占用,运行 sbin/nginx 命令来启动 Nginx,打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
关于nginx的几个命令简单说明:
详情参考:
-c
为 Nginx 指定一个配置文件,来代替缺省的。 
-t 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。 
-v 显示 nginx 的版本。 
-V 显示 nginx 的版本,编译器版本和配置参数
3配置 nginx.conf
修改配置文件 /usr/local/nginx/conf/nginx.conf
下面是一个配置给出些说明:
user nobody nobody; #工作进程的属主

worker_processes 2; # 工作进程数,一般与 CPU 核数等同

error_log /usr/local/nginx/logs/nginx_error.log crit;

pid /usr/local/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
  use epoll;

  worker_connections 51200; # 每个工作进程允许最大的同时连接数
}

http
{
  include mime.types;
  default_type application/octet-stream;

  charset gbk;
   
  server_names_hash_bucket_size 128;
   
  sendfile on;
  tcp_nopush on;

  keepalive_timeout 60;

  tcp_nodelay on;


  gzip on;
# gzip_min_length 1k;
# gzip_buffers 4 8k;
# gzip_http_version 1.1;
# gzip_types text/plain application/x-javascript text/css text/html application/xml;

  server
  {
  listen 80; #nginx侦听端口
  server_name localhost;
  index index.jsp index.html index.htm ;
  root /usr/local/www;#web的工作目录

  if (-d $request_filename)
  {
  rewrite ^/(.*)([^/])$ permanent;
  }
  #加下面这段是因为我发行如果首页是index.jsp的话好像不能默认通过index.jsp来解析
  location / {
  root /usr/local/www;  
  index index.jsp;
  proxy_pass
  }
  #以扩展名方式匹配静态文件
  Location ~* \.(htm|html|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf|txt|wma)$ 
  { 
  root /usr/local/www; 
  expires 24h; 
  }
  #以目录方式匹配静态目录
Location ~ ^/(images|common|cooperation|download|huodong|inc|magic|manager|produces|pages|secert|SinaEditor|styles|javascript)/ 
  { 
  root /usr/local/www;
  expires 30d;
  }


#以扩展名方式匹配动态文件
location ~* \.(jsp|do)$ 
  {
  root /usr/local/www;
  index index.jsp;
  include /usr/local/nginx/conf/proxy.conf; # 加载proxy.conf 也就是测试中用来链接JSP
  proxy_pass
  proxy_set_header X-Real-IP $remote_addr;
  }
#让我的nginx也支持php
location ~ .*\.php?$
  {
  include fcgi.conf;
  #fastcgi_pass unix:/tmp/php-cgi.sock;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  }

#注意如果开启下面这几段的话就不能有上文中location / {的部分否则会报错,也就是只能有一个location / {
# location / {
# include /usr/local/nginx/conf/proxy.conf;
# proxy_pass
# proxy_set_header X-Real-IP $remote_addr;
# }
   

  log_format access '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" $http_x_forwarded_for';
  access_log /usr/local/nginx/logs/access.log access;
  }

 

  server
  {
  listen 84;#让nginx的NginxStatus功能运行在我的84端口,当然自己配置对应的server_name也可以
  server_name localhost;

  location / {
  stub_status on;
  access_log off;
  }
  }
}

关于include /usr/local/nginx/conf/proxy.conf; # 加载proxy.conf 也就是测试中用来链接JSP
Vi /usr/local/nginx/conf/proxy.conf 
内容如下:
# proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;

关于#让我的nginx也支持php
location ~ .*\.php?$
  {
  include fcgi.conf;
  #fastcgi_pass unix:/tmp/php-cgi.sock;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  }
这段不做详细说明了:

启动nginx
测试配置文件有无问题,例如:
#/usr/local/nginx/sbin/nginx –t

2008/09/09 12:16:13 [info] 17651#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2008/09/09 12:16:13 [info] 17651#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
上面说明配置文件没有问题
启动
#/usr/local/nginx/sbin/nginx
查看是否启动了
# ps fax|grep nginx
17966 pts/1 S+ 0:00 \_ grep nginx
17514 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
17515 ? S 0:00 \_ nginx: worker process  
17516 ? S 0:00 \_ nginx: worker process  


NginxStatus功能说明如下:

Active connections: 9 
server accepts handled requests
 14 14 150 
Reading: 0 Writing: 1 Waiting: 8 

active connections -- 对后端发起的活动连接数
server accepts handled requests -- nginx 总共处理了 14 个连接, 成功创建 14 次握手 (证明中间没有失败的), 总共处理了 150 个请求
reading -- nginx 读取到客户端的 Header 信息数。 
writing -- nginx 返回给客户端的 Header 信息数。 
waiting -- 开启 keep-alive 的情况下,这个值等于 active - (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接

关于平滑重启nginx还是参考:

取服务器信息

HTTP/1.1 200 OK
Server: nginx/0.7.14
Date: Tue, 09 Sep 2008 07:22:03 GMT
Content-Type: text/html;charset=GBK
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: JSESSIONID=049F77C0BE388C3C942A4A80EE1DA346; Path=/

阅读(1172) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~