安泰信息

2008年09月26日

转载:使用 Nginx 提升网站访问速度

归档在: 未分类 — JACKEYJ @ 4:58 PM

2008 年 7 月 18 日
本文主要介绍如何在 Linux 系统上安装高性能的 HTTP 服务器 —— Nginx、并在不改变原有网站结构的条件下用 Nginx 来提升网站的访问速度。
Nginx 简介
Nginx (”engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。 Igor 将源代码以类 BSD 许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。
根据最新一期(08 年 6 月份)的 NetCraft 调查报告显示,已经有超过两百万的主机使用了 Nginx,这个数字超过了另外一个轻量级的 HTTP 服务器 lighttpd, 排名第四,并且发展迅速。下面是这份报告的前几名的报表:
产品 网站数
Apache 84,309,103
IIS 60,987,087
Google GFE 10,465,178
Unknown 4,903,174
nginx 2,125,160
Oversee 1,953,848
lighttpd 1,532,952

关于这期调查报告的更详细信息请看下面链接:
http://survey.netcraft.com/Reports/200806/
下图是最近几个月使用 Nginx 和 lighttpd 的网站数比较

图 1. 最近几个月使用 Nginx 和 lighttpd 的网站数比较

使用 Nginx 前必须了解的事项
目前官方 Nginx 并不支持 Windows,您只能在包括 Linux、UNIX、BSD 系统下安装和使用;
Nginx 本身只是一个 HTTP 和反向代理服务器,它无法像 Apache 一样通过安装各种模块来支持不同的页面脚本,例如 PHP、CGI 等;
Nginx 支持简单的负载均衡和容错;
支持作为基本 HTTP 服务器的功能,例如日志、压缩、Byte ranges、Chunked responses、SSL、虚拟主机等等,应有尽有。
在 Linux 下安装 Nginx
为了确保能在 Nginx 中使用正则表达式进行更灵活的配置,安装之前需要确定系统是否安装有 PCRE(Perl Compatible Regular Expressions)包。您可以到 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
# 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

接下来安装 Nginx,Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /opt/nginx 目录下的详细步骤:
# wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
# tar zxvf nginx-0.6.31.tar.gz
# cd nginx-0.6.31
# ./configure –with-http_stub_status_module –prefix=/opt/nginx
# make
# make install

其中参数 –with-http_stub_status_module 是为了启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当前状态。
安装成功后 /opt/nginx 目录下有四个子目录分别是:conf、html、logs、sbin 。其中 Nginx 的配置文件存放于 conf/nginx.conf,Nginx 只有一个程序文件位于 sbin 目录下的 nginx 文件。确保系统的 80 端口没被其他程序占用,运行 sbin/nginx 命令来启动 Nginx,打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
常用的 Nginx 参数和控制
程序运行参数
Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的。 Nginx 的参数包括有如下几个:
-c :使用指定的配置文件而不是 conf 目录下的 nginx.conf 。
-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。
-v:显示 nginx 版本号。
-V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。
例如我们要测试某个配置文件是否书写正确,我们可以使用以下命令
sbin/nginx – t – c conf/nginx2.conf

通过信号对 Nginx 进行控制
Nginx 支持下表中的信号:
信号名 作用描述
TERM, INT 快速关闭程序,中止当前正在处理的请求
QUIT 处理完当前请求后,关闭程序
HUP 重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求
USR1 重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件
USR2 平滑升级可执行程序
WINCH 从容关闭工作进程

有两种方式来通过这些信号去控制 Nginx,第一是通过 logs 目录下的 nginx.pid 查看当前运行的 Nginx 的进程 ID,通过 kill – XXX 来控制 Nginx,其中 XXX 就是上表中列出的信号名。如果您的系统中只有一个 Nginx 进程,那您也可以通过 killall 命令来完成,例如运行 killall – s HUP nginx 来让 Nginx 重新加载配置。
配置 Nginx
先来看一个实际的配置文件:
user nobody;# 工作进程的属主
worker_processes 4;# 工作进程数,一般与 CPU 核数等同

#error_log logs/zjant-error.log;
#error_log logs/zjant-error.log notice;
#error_log logs/zjant-error.log info;

#pid logs/nginx.pid;

events {
use epoll;#Linux 下性能最好的 event 模式
worker_connections 2048;# 每个工作进程允许最大的同时连接数
}

http {
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 off;
access_log logs/access.log;# 日志文件名

sendfile on;
#tcp_nopush on;
tcp_nodelay on;

keepalive_timeout 65;

include gzip.conf;

# 集群中的所有后台服务器的配置信息
upstream tomcats {
server 192.168.0.11:8080 weight=10;
server 192.168.0.11:8081 weight=10;
server 192.168.0.12:8080 weight=10;
server 192.168.0.12:8081 weight=10;
server 192.168.0.13:8080 weight=10;
server 192.168.0.13:8081 weight=10;
}

server {
listen 80;#HTTP 的端口
server_name www.zjant.com;

charset utf-8;

#access_log logs/host.access.log main;

location ~ ^/NginxStatus/ {
stub_status on; #Nginx 状态监控配置
access_log off;
}

location ~ ^/(WEB-INF)/ {
deny all;
}

location ~ \.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|
zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ {
root /opt/webapp;
expires 24h;
}

location / {
proxy_pass http://tomcats;# 反向代理
include proxy.conf;
}

error_page 404 /html/404.html;

# redirect server error pages to the static page /50x.html
#
error_page 502 503 /html/502.html;
error_page 500 504 /50x.html;
location = /50x.html {
root html;
}
}
}

Nginx 监控
上面是一个实际网站的配置实例,其中灰色文字为配置说明。上述配置中,首先我们定义了一个 location ~ ^/NginxStatus/,这样通过 http://www.zjant.com/NginxStatus/ 就可以监控到 Nginx 的运行信息,显示的内容如下:
Active connections: 70
server accepts handled requests
14553819 14553819 19239266
Reading: 0 Writing: 3 Waiting: 67

NginxStatus 显示的内容意思如下:
active connections – 当前 Nginx 正处理的活动连接数。
server accepts handled requests — 总共处理了 14553819 个连接 , 成功创建 14553819 次握手 ( 证明中间没有失败的 ), 总共处理了 19239266 个请求 ( 平均每次握手处理了 1.3 个数据请求 )。
reading — nginx 读取到客户端的 Header 信息数。
writing — nginx 返回给客户端的 Header 信息数。
waiting — 开启 keep-alive 的情况下,这个值等于 active - (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。
静态文件处理
通过正则表达式,我们可让 Nginx 识别出各种静态文件,例如 images 路径下的所有请求可以写为:
location ~ ^/images/ {
root /opt/webapp/images;
}

而下面的配置则定义了几种文件类型的请求处理方式。
location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
root /opt/webapp;
expires 24h;
}

对于例如图片、静态 HTML 文件、js 脚本文件和 css 样式文件等,我们希望 Nginx 直接处理并返回给浏览器,这样可以大大的加快网页浏览时的速度。因此对于这类文件我们需要通过 root 指令来指定文件的存放路径,同时因为这类文件并不常修改,通过 expires 指令来控制其在浏览器的缓存,以减少不必要的请求。 expires 指令可以控制 HTTP 应答中的“ Expires ”和“ Cache-Control ”的头标(起到控制页面缓存的作用)。您可以使用例如以下的格式来书写 Expires:
expires 1 January, 1970, 00:00:01 GMT;
expires 60s;
expires 30m;
expires 24h;
expires 1d;
expires max;
expires off;

动态页面请求处理
Nginx 本身并不支持现在流行的 JSP、ASP、PHP、PERL 等动态页面,但是它可以通过反向代理将请求发送到后端的服务器,例如 Tomcat、Apache、IIS 等来完成动态页面的请求处理。前面的配置示例中,我们首先定义了由 Nginx 直接处理的一些静态文件请求后,其他所有的请求通过 proxy_pass 指令传送给后端的服务器(在上述例子中是 Tomcat)。最简单的 proxy_pass 用法如下:
location / {
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
}

这里我们没有使用到集群,而是将请求直接送到运行在 8080 端口的 Tomcat 服务上来完成类似 JSP 和 Servlet 的请求处理。
当页面的访问量非常大的时候,往往需要多个应用服务器来共同承担动态页面的执行操作,这时我们就需要使用集群的架构。 Nginx 通过 upstream 指令来定义一个服务器的集群,最前面那个完整的例子中我们定义了一个名为 tomcats 的集群,这个集群中包括了三台服务器共 6 个 Tomcat 服务。而 proxy_pass 指令的写法变成了:
location / {
proxy_pass http://tomcats;
proxy_set_header X-Real-IP $remote_addr;
}

在 Nginx 的集群配置中,Nginx 使用最简单的平均分配规则给集群中的每个节点分配请求。一旦某个节点失效时,或者重新起效时,Nginx 都会非常及时的处理状态的变化,以保证不会影响到用户的访问。
总结
尽管整个程序包只有五百多 K,但麻雀虽小、五脏俱全。 Nginx 官方提供的各种功能模块应有尽有,结合这些模块可以完整各种各样的配置要求,例如:压缩、防盗链、集群、FastCGI、流媒体服务器、Memcached 支持、URL 重写等等,更关键的是 Nginx 拥有 Apache 和其他 HTTP 服务器无法比拟的高性能。您甚至可以在不改变原有网站的架构上,通过在前端引入 Nginx 来提升网站的访问速度。
本文只是简单介绍了 Nginx 的安装以及常见的基本的配置和使用,更多关于 Nginx 的信息请阅读文章后面的参考资源。在这里要非常感谢我的朋友——陈磊(chanix@msn.com),他一直在做 Nginx 的中文 WIKI(http://wiki.codemongers.com/NginxChs),同时也是他介绍给我这么好的一款软件。
如果您的网站是运行在 Linux 下,如果您并没有使用一些非常复杂的而且确定 Nginx 无法完成的功能,那您应该试试 Nginx 。

参考资料
学习
查阅 Nginx 英文站点。

查阅 Nginx 中文 WIKI。

查阅 Nginx 英文 WIKI。

另外一个轻量级 HTTP 服务器 lighttpd。

获得产品和技术
下载 最新版本的 Nginx。

下载 PCRE。

关于作者

刘柄成一直使用 J2EE/J2ME 从事移动互联网方面的开发。DLOG4J 的作者

安泰信息: nginx 0.7.14 ubuntu8.04 安装

归档在: ubuntu — JACKEYJ @ 12:38 PM

nginx-0.7.14 ubuntu8.04 安装

原创:如果转载,请保留原地址:www.zjant.com,作者:jackey jiao
一. 安装环境
dell sc430 1g 40G
ubuntu 8.04

二.准备工作
下载相关的安装包,其中ubuntu需要安装下列几个包.
apt-get install libpcre3 && \
apt-get install zlib1g && \
apt-get install libpcre3-dev && \
apt-get install zlib1g-dev && \
apt-get install libssl && \
apt-get install libssl-dev

如果你在build过程中发现缺少其他的包可以继续通过apt-get来安装.

当然要下载主角nginx了. 下载路径 http://sysoev.ru/nginx/nginx-0.7.16.tar.gz

三.编译安装
 tar xzf nginx-0.7.14.tar.gz
 cd nginx-0.7.14/
 sudo -s

./configure –prefix=/zjant/nginx7 –with-openssl=/usr/include/openssl
注意:
a) –with-pcre 我没加,是因为我通过apt-get安装之后系统会自动找到.
b) 对应openssl,在debian和ubuntu系统中使用的是libssl和libssl-dev的包,这点可能和其他的环境不同. — www.zjant.com

#最后configure执行的结果如下,如果你中间遇到其他错误,请根据具体问题进行解决.
Configuration summary
  + using PCRE library: using system pcre library
  + using OpenSSL library: /usr/include
  + md5 library is not used
  + sha1 library is not used
  + using system zlib library

  nginx path prefix: “/zjant/nginx7″
  nginx binary file: “/zjant/nginx7/sbin/nginx”
  nginx configuration prefix: “/zjant/nginx7/conf”
  nginx configuration file: “/zjant/nginx7/conf/nginx.conf”
  nginx pid file: “/zjant/nginx7/logs/nginx.pid”
  nginx error log file: “/zjant/nginx7/logs/error.log”
  nginx http access log file: “/zjant/nginx7/logs/access.log”
  nginx http client request body temporary files: “/zjant/nginx7/client_body_temp”
  nginx http proxy temporary files: “/zjant/nginx7/proxy_temp”
  nginx http fastcgi temporary files: “/zjant/nginx7/fastcgi_temp”

make
make install

四.运行测试
cd /zjant/nginx7/sbin
./nginx

在浏览器中输入http://localhost 就会出现问候页面.
恭喜你,已经安装成功了:)

五. 参考资料
http://wiki.codemongers.com/NginxChs
http://www.zjant.com

预先编译好的安装包
Nginx在一些Linux发行版和BSD的各个变种版本的安装包仓库中都会有,通过各个系统自带的软件包管理方法即可安装。需要注意的是,很多预先编译好的安装包都比较陈旧,大多数情况下还是推荐直接从源码编译。

官方源代码下载 — www.zjant.com
http://sysoev.ru/nginx/download.html

使用源代码进行构建
Nginx 使用 Unix 下常用的 ‘./configure && make && make install’ 过程来编译安装。

configure 脚本确定系统所具有一些特性,特别是 nginx 用来处理连接的方法。然后,它创建 Makefile 文件。

configure 支持下面的选项:

–prefix=<path> - Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。

–sbin-path=<path> - Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。

–conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。

–pid-path=<path> - 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。

–lock-path=<path> - nginx.lock文件的路径。

–error-log-path=<path> - 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。

–http-log-path=<path> - 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。

–user=<user> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。

–group=<group> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。

–builddir=DIR - 指定编译的目录

–with-rtsig_module - 启用 rtsig 模块

–with-select_module –without-select_module - Whether or not to enable the select module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.

//允许或不允许开启SELECT模式,如果 configure 没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相同,都是采用轮训方法) SELECT模式将是默认安装模式

–with-poll_module –without-poll_module - Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.

–with-http_ssl_module - Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.

//开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl

–with-http_realip_module - 启用 ngx_http_realip_module

–with-http_addition_module - 启用 ngx_http_addition_module

–with-http_sub_module - 启用 ngx_http_sub_module

–with-http_dav_module - 启用 ngx_http_dav_module

–with-http_flv_module - 启用 ngx_http_flv_module

–with-http_stub_status_module - 启用 “server status” 页

–without-http_charset_module - 禁用 ngx_http_charset_module

–without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。

–without-http_ssi_module - 禁用 ngx_http_ssi_module

–without-http_userid_module - 禁用 ngx_http_userid_module

–without-http_access_module - 禁用 ngx_http_access_module

–without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module

–without-http_autoindex_module - 禁用 ngx_http_autoindex_module

–without-http_geo_module - 禁用 ngx_http_geo_module

–without-http_map_module - 禁用 ngx_http_map_module

–without-http_referer_module - 禁用 ngx_http_referer_module

–without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。

–without-http_proxy_module - 禁用 ngx_http_proxy_module

–without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module

–without-http_memcached_module - 禁用 ngx_http_memcached_module

–without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module

–without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module

–without-http_browser_module - 禁用 ngx_http_browser_module

–without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module

–with-http_perl_module - 启用 ngx_http_perl_module

–with-perl_modules_path=PATH - 指定 perl 模块的路径

–with-perl=PATH - 指定 perl 执行文件的路径

–http-log-path=PATH - Set path to the http access log

–http-client-body-temp-path=PATH - Set path to the http client request body temporary files

–http-proxy-temp-path=PATH - Set path to the http proxy temporary files

–http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files

–without-http - 禁用 HTTP server

–with-mail - 启用 IMAP4/POP3/SMTP 代理模块

–with-mail_ssl_module - 启用 ngx_mail_ssl_module

–with-cc=PATH - 指定 C 编译器的路径

–with-cpp=PATH - 指定 C 预处理器的路径

–with-cc-opt=OPTIONS - Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate –with-cc-opt=”-I /usr/local/include”. If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: –with-cc-opt=”-D FD_SETSIZE=2048″.

–with-ld-opt=OPTIONS - Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate –with-ld-opt=”-L /usr/local/lib”.

–with-cpu-opt=CPU - 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64

–without-pcre - 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 “location” 配置指令中的正则表达式也需要 PCRE 。

–with-pcre=DIR - 指定 PCRE 库的源代码的路径。

–with-pcre-opt=OPTIONS - Set additional options for PCRE building.

–with-md5=DIR - Set path to md5 library sources.

–with-md5-opt=OPTIONS - Set additional options for md5 building.

–with-md5-asm - Use md5 assembler sources.

–with-sha1=DIR - Set path to sha1 library sources.

–with-sha1-opt=OPTIONS - Set additional options for sha1 building.

–with-sha1-asm - Use sha1 assembler sources.

–with-zlib=DIR - Set path to zlib library sources.

–with-zlib-opt=OPTIONS - Set additional options for zlib building.

–with-zlib-asm=CPU - Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro

–with-openssl=DIR - Set path to OpenSSL library sources

–with-openssl-opt=OPTIONS - Set additional options for OpenSSL building

–with-debug - 启用调试日志

–add-module=PATH - Add in a third-party module found in directory PATH

在不同版本间,选项可能会有些许变化,请总是使用 ./configure –help 命令来检查一下当前的选项列表。

示例 (最好能在同一行):
    ./configure \
        –sbin-path=/usr/local/nginx/nginx \
        –conf-path=/usr/local/nginx/nginx.conf \
        –pid-path=/usr/local/nginx/nginx.pid \
        –with-http_ssl_module \
        –with-pcre=../pcre-4.4 \
        –with-zlib=../zlib-1.1.3
Example on Ubuntu/debian with libgcrypt11-dev, libpcre3-dev and libssl-dev installed (choose EITHER –with-md5 OR –with-sha1, but not both; on debian and ubuntu, they should both point to /usr/lib)
     ./configure –with-openssl=/usr/lib/ssl/ –with-md5=/usr/lib
An Ubuntu Edgy .deb for version 0.5.2 can be found here: nginx_0.5.2-1_i386.deb.

(NOTE: According to an October 2006 message md5 was used in a now broken http cache module and sha1 is used in an incomplete mysql library module and so are currently not needed.)

2008年09月25日

安泰资讯:主流web server对比

归档在: Linux, apache, ubuntu — JACKEYJ @ 4:58 PM

RoR的部署方式从架构上来说分为前端和后端:

一、前端
前端的作用就是处理静态资源,将动态请求分发到后端,有时候也带有一些额外的功能,例如对特定URL进行rewrite和redirect,对HTTP输出进行gzip压缩等等。

前端目前已知的可以选择apache, lighttpd, litespeed, nginx, haproxy

1、apache2.2
apache是全球市场占有率最高的web server,超过全球互联网网站50%的网站都用apache。apache2.2 + mod_proxy_balancer是一个非常流行,非常稳定的方案。

使用apache2.2唯一的问题就是apache的性能和后面那些轻量级web server相比,差太远了。一方面在处理静态请求方面apache要比lighttpd慢3-5倍,内存消耗和CPU消耗也高出一个数量级,另一方面 mod_proxy_balancer的分发性能也不高,比haproxy差很远。

2、lighttpd
lighttpd是一个轻量级高性能web server,一个在MySQL Inc工作的德国人写的。性能很好,内存和CPU资源消耗很低,支持绝大多数apache的功能,是apache的绝好替代者。目前lighttpd已经 上升到全球互联网第四大web server,市场占有率仅此于apache,IIS和Sun。

lighttpd唯一的问题是proxy功能不完善,因此不适合搭配mongrel来使用。lighttpd下一个版本1.5.0的proxy模块重写过了,将会解决这个问题。

3、litespeed
和lighttpd差不多,商业产品,收费的。比lighttpd来说,多一个web管理界面,不用写配置文件了。litespeed专门为单机 运行的RoR开发了一个lsapi协议,号称性能最好,比httpd和fcgi都要好。他的proxy功能比lighttpd完善。

litespeed的缺点我却认为恰恰是这个lsapi。因为lsapi不是web server启动的时候启动固定数目的ruby进程,而是根据请求繁忙程度,动态创建和销毁ruby进程,貌似节省资源,实则和apache2.2进程模 型一样,留下很大的黑客攻击漏洞。只要黑客瞬时发起大量动态请求,就会让服务器忙于创建ruby进程而导致CPU资源耗尽,失去响应。

当然,litespeed也支持httpd和fcgi,这个和lighttpd用法一样的,到没有这种问题。

4、nginx
一个俄国人开发的轻量级高性能web server,特点是做proxy性能很好,因此被推荐取代apache2.2的mod_proxy_balancer,来和mongrel cluster搭配。其他方面和lighttpd到差不多。

要说缺点,可能就是发展的时间比较短,至今没有正式版本,还是beta版。没有经过足够网站的验证。

5、haproxy
就是一个纯粹的高性能proxy,不处理静态资源的,所有请求统统分发到后端。

二、后端
后端就是跑ruby进程,处理RoR动态请求了。运行后端ruby进程有两种方式:

1、fcgi方式
准确的说,不能叫做fcgi方式,其实就是启动一个ruby进程,让这个ruby进程监听一个tcp/unix socket,以fcgi协议和前端通讯。所以fcgi不是指ruby进程的运行方式,而是ruby进程使用的通讯协议。这就好比你tomcat可以用 http,ajp通讯一样,tomcat自己的运行方式都一样的,只是通讯方式不一样。

fcgi方式启动ruby进程,可以使用lighttpd带的一个spawn-fcgi工具来启动(JavaEye目前采用这种方式)。

值得一提的是,apache2.2的mod_fastcgi的方式也上面还不太一样,由apache动态创建fcgi进程和管理fcgi进程,这 种方式和litespeed的lsapi面临的问题是一样的,此外apache的mod_fastcgi自己也有很多严重的bug,是一种很糟糕的部署方 式。这种糟糕的部署方式也败坏了fcgi的名声。

fastcgi只是一种协议,虽然古老,但并不是不好用,http协议也很古老。没有必要因为apache的mod_fastcgi的运行方式的问题而连带把fastcgi都一同否定了。

2、http方式
也就是用mongrel去跑ruby进程,由于mongrel实际上已经是一个简单的http server,所以也可以单独作为web server使用。mongrel现在越来越受欢迎了。

用fcgi方式还是http方式,我个人觉得区别不大,关键还是看应用的场合,一般而言,推荐的搭配是:

lighttpd + fcgi 或者 nginx +mongrel,而apache因为性能差距,而不被推荐。

JavaEye为什么用lighttpd + fcgi呢?原因如下:

1) lighttpd发展了好几年了,市场占有率也相当高,是一个经过实践检验的server,它的文档也很全;而nginx还没有经过足够的市场检验,文档也很缺乏
2) JavaEye的ruby进程和web server在一台机器上面跑,通过unix socket使用fcgi协议通讯可以避免tcp的网络开销,其通讯速度比使用tcp socket使用http协议通讯要快一些。

什么场合使用haproxy?

大规模部署,例如你的RoR应用到十几台服务器上面去,你用haproxy会更好,可以方便的添加删除应用服务器节点,proxy性能更好。

安泰资讯:Nginx–apache的有力挑战者

归档在: Linux — JACKEYJ @ 4:47 PM

nginx [engine x] is a HTTP server and mail proxy server written by me (Igor Sysoev).

nginx has been running for more than three years on many heavily loaded Russian sites including Rambler (RamblerMedia.com).
In March 2007 about 20% of all Russian virtual hosts were served or proxied by nginx.
According to Google Online Security Blog year ago nginx served or proxied about 4% of all Internet virtual hosts, although Netcraft showed much less percent.
According to Netcraft in March 2008 nginx served or proxied 1 million virtual hosts. The growing in picture and colour!
2 of Alexa US Top100 sites use nginx.
Here are some of success stories: FastMail.FM, Wordpress.com.

The latest development version is nginx-0.7.17, the change log.
The latest stable version is nginx-0.6.32, the change log.
The latest legacy stable version is nginx-0.5.37, the change log.
The sources are licensed under 2-clause BSD-like license.

The English Wiki.

One of English mailling list archives.
How to subscribe.

The Russian documenatation.

Basic HTTP features:

  • Handling of static files, index files, and autoindexing; open file descriptor cache;
  • Accelerated reverse proxying without caching; simple load balancing and fault tolerance;
  • Accelerated support without caching of remote FastCGI servers; simple load balancing and fault tolerance;
  • Modular architecture. Filters include gzipping, byte ranges, chunked responses, XSLT, and SSI. Multiple SSI inclusions within a single page can be processed in parallel if they are handled by FastCGI or proxied servers.
  • SSL and TLS SNI support.

Mail proxy server features:

  • User redirection to IMAP/POP3 backend using an external HTTP authentication server;
  • User authentication using an external HTTP authentication server and connection redirection to internal SMTP backend;
  • Authentication methods:
    • POP3: USER/PASS, APOP, AUTH LOGIN PLAIN CRAM-MD5;
    • IMAP: LOGIN, AUTH LOGIN PLAIN CRAM-MD5;
    • SMTP: AUTH LOGIN PLAIN CRAM-MD5;
  • SSL support;
  • STARTTLS and STLS support.

Tested OS and platforms:

  • FreeBSD 3 — 7 / i386; FreeBSD 5 — 7 / amd64;
  • Linux 2.2 — 2.6 / i386; Linux 2.6 / amd64;
  • Solaris 9 / i386, sun4u; Solaris 10 / i386, amd64, sun4v;
  • MacOS X / ppc, i386;

Architecture and scalability:

  • one master process and several workers processes. The workers run as unprivileged user;
  • kqueue (FreeBSD 4.1+), epoll (Linux 2.6+), rt signals (Linux 2.2.19+), /dev/poll (Solaris 7 11/99+), event ports (Solaris 10), select, and poll support;
  • various kqueue features support including EV_CLEAR, EV_DISABLE (to disable event temporalily), NOTE_LOWAT, EV_EOF, number of available data, error codes;
  • sendfile (FreeBSD 3.1+, Linux 2.2+, Mac OS X 10.5), sendfile64 (Linux 2.4.21+), and sendfilev (Solaris 8 7/01+) support;
  • accept-filter (FreeBSD 4.1+) and TCP_DEFER_ACCEPT (Linux 2.4+) support;
  • 10,000 inactive HTTP keep-alive connections take about 2.5M memory;
  • data copy operations are kept to a minimum.

Other HTTP features:

  • name- and IP-based virtual servers;
  • keep-alive and pipelined connections support;
  • flexible configuration;
  • reconfiguration and online upgrade without interruption of the client processing;
  • access log formats, bufferred log writing, and quick log rotation;
  • 4xx-5xx error codes redirection;
  • rewrite module;
  • access control based on client IP address and HTTP Basic authentication;
  • PUT, DELETE, MKCOL, COPY and MOVE methods;
  • FLV streaming;
  • speed limitation;
  • limitation of simultaneous connections from one address.

Experimental features:

  • embedded perl.

安泰信息:Debian Linux Apahe2.0.63 JBoss 4.2.2 Java 1.5 集群安装

归档在: Linux, apache, jboss, java, ubuntu — JACKEYJ @ 11:43 AM

Apahe2/JBoss/Java Cluster Guide

1 Download needed software

1.1 Jboss4.2.2 GA

Please go http://www.jboss.org/download/ to find the jboss-4.2.3.GA.zip to download.

 

1.2 Java SE 5.0 updated 16

Please go
https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=jdk-1.5.0_16-oth-JPR@CDS-CDS_Developer

And select Platform with Linux, Language with Multi-language, then download
jdk-1_5_0_16-linux-i586.bin.

 

1.3 Apache httpd 2.0.63

Please go http://httpd.apache.org/download.cgi to find httpd-2.0.63.tar.gz to download.

 

1.4 Tomcat Connectors 1.2.9

Please go http://apache.deathculture.net/tomcat/tomcat-connectors/jk/source/jk-1.2.26/ to find tomcat-connectors-1.2.26-src.tar.gz to download

 

2 Java installation

Login debian OS with root user, then create a linux user. E.g ‘deploy’.

Login with ‘deploy’ account, then upload jdk-1_5_0_16-linux-i586.bin to home directory.

chmod 755 jdk-1_5_0_16-linux-i586.bin

./ jdk-1_5_0_16-linux-i586.bin

ln –s jdk-1_5_0_16-linux-i586 jdk

Extract java and link it to jdk as above. Then modify envirement variable on profile.

cd /home/deploy

vi .bash_profile

#add for Jboss and jdk begin

JAVA_HOME=/home/deploy/jdk;export JAVA_HOME

CLASSPATH=./:/home/deploy/jdk/lib/dt.jar:/home/deploy/jdk/lib/tools.jar;export CLASSPATH

PATH=$PATH:/home/deploy/jdk/bin:/home/deploy/jdk/jre/bin;export PATH

alias l=”/bin/ls -al”

export HOME=/home/deploy

export PS1=’$PWD>’

set EDITOR=vi

set -o vi

after modify the .bash_profile and save it, then logout.

3 JBoss installation

Login with deploy account, then upload jboss-4.2.3.GA.zip to home directory.

3.1 Extract Jboss package

unzip jboss-4.2.3.zip

move jboss-4.2.3 jboss

Extract jboss-4.2.3.GA.zip to jboss-4.2.3.GA and rename the directory to jboss.

 

3.2 Copy need LIB and jar to default deployment

If Jboss need to run on cluster, copy some LIB and jar file from its ‘all’ directory to ‘default’ directory. Please execute those commands as follows:

cd ~/jboss/server/default/lib/

cp ~/jboss/server/all/lib/jgroups.jar .

cp ~/jboss/server/all/lib/jbossha.jar .

cd ~/jboss /server/default/deploy

cp -r ~/jboss/server/all/deploy/jboss-web-cluster.sar/ .

cp ~/jboss/server/all/deploy/cluster-service.xml .

cd jboss/server/default/deploy/jboss-web.deployer/META-INF/

 

3.3 Configure cluster parameter

vi jboss-server.xml

Then, for each JBoss Tomcat instance in the cluster, we need to tell it to add the jvmRoute value to its session cookies so that mod_jk can route incoming requests. Locate the <attribute> element with a name of UseJK, and set its value to true line 114 on jboss-server.xml.

 

cd ~/jboss/server/default/deploy/jboss-web.deployer/

vi server.xml

Please find the line as <Engine name=”jboss.web” defaultHost=”localhost”>, and add jvmRoute=”debian01″ to this line, different nodes should different jvmRoute name, result as follows:

<Engine name=”jboss.web” jvmRoute=”debian01″ defaultHost=”localhost”>

 

cd ~/jboss/server/default/deploy

vi cluster-service.xml

Cluster might apply UDP and TCP communication protocol, mask UDP configure node from line 39 to 80, then unmask TCP configure node.

locate line 85, find bind_addr=”thishost” and change thishost to ip address of the current host.

find start_port=”7800″ and change 7800 to 7820.

find TCPPING initial_hosts=”thishost[7800],otherhost[7800]”, change thishost to ip address of the current host,

change otherhost to ip address of another node. thange the two “7800″ to “7820″.

Sample TCP configure as follows:

         <Config>

            <TCP bind_addr=”10.5.6.44” start_port=”7820” loopback=”true”

                 tcp_nodelay=”true”

                 recv_buf_size=”20000000″   

                 send_buf_size=”640000″

                 discard_incompatible_packets=”true”          

                 enable_bundling=”false”

                 max_bundle_size=”64000″

                 max_bundle_timeout=”30″       

                 use_incoming_packet_handler=”true”

                 use_outgoing_packet_handler=”false”

                 down_thread=”false” up_thread=”false”   

                 use_send_queues=”false”                            

                 sock_conn_timeout=”300″

                 skip_suspected_members=”true”/>

            <TCPPING initial_hosts=”10.5.6.44[7820],10.5.6.246[7820]” port_range=”3″

                     timeout=”3000″

                     down_thread=”false” up_thread=”false”

                     num_initial_members=”3″/>    

            <MERGE2 max_interval=”100000″                       

                    down_thread=”false” up_thread=”false” min_interval=”20000″/>

            <FD_SOCK down_thread=”false” up_thread=”false”/>

            <FD timeout=”10000″ max_tries=”5″ down_thread=”false” up_thread=”false” shun=”true”/>

            <VERIFY_SUSPECT timeout=”1500″ down_thread=”false” up_thread=”false”/>

            <pbcast.NAKACK max_xmit_size=”60000″

                           use_mcast_xmit=”false” gc_lag=”0″

                           retransmit_timeout=”300,600,1200,2400,4800″

                           down_thread=”false” up_thread=”false”

                           discard_delivered_msgs=”true”/>

            <pbcast.STABLE stability_delay=”1000″ desired_avg_gossip=”50000″

                           down_thread=”false” up_thread=”false”

                           max_bytes=”400000″/>

            <pbcast.GMS print_local_addr=”true” join_timeout=”3000″

                        down_thread=”false” up_thread=”false”

                        join_retry_timeout=”2000″ shun=”true”

                        view_bundling=”true”/>

            <pbcast.STATE_TRANSFER down_thread=”false” up_thread=”false” use_flush=”false”/>

         </Config>

 

3.4 JNDI DataSource

cd ~/jboss/server/default/deploy

cp ~/jboss/docs/examples/jca/postgres-ds.xml .

vi postgres-ds.xml

Please modify servername, port, database name, user name and password on postgres-ds.xml.

 

3.5 Configure other node as above.

 

4 Apache2.0.x and connctors installation

It seem that debian sarge don’t support apache2.2.x version. If you install apache via apt-get method, apache might not support mod_jk. So should compile and install apache2.0.x by manual.

4.1 Upload apache package

Please upload httpd-2.0.63.tar.gz to /usr/src directory.

 

4.2 Compile & Install apache2.0.x

Login with root

cd /usr/src/

tar zxvf httpd-2.0.63.tar.gz

cd httpd-2.0.63

./configure -prefix=/usr/local/apache2 –enable-so –enable-modules=all  –enable-mods-shared=all

If debian has multiple CPU, please add -with-mpm=prefork to above command line.

make clean

make

make install

 

4.3 Complie connectors

Upload tomcat-connectors-1.2.26-src.tar.gz to /usr/src directory.

cd /usr/src/tomcat-connectors-1.2.26-src/native

./configure –with-apxs=/usr/local/apache2/bin/apxs

If debian has multiple CPU, please add –enable-prefork to above command line.

make

make install

You can see mod_jk.so on /usr/local/apache2/modules directory.

 

4.4 Apache & connectors configure

cd /usr/local/apache2/conf

Please add the line to httpd.conf.

Include conf/mod_jk.conf 

Please add mod_mk.conf on /usr/local/apache2/conf directory, its content as follows:

# Load mod_jk module

# Specify the filename of the mod_jk lib

LoadModule jk_module modules/mod_jk.so

#LoadModule jk_module modules/mod_jk-1.2.26-httpd-2.2.4.so

 

# Where to find workers.properties

JkWorkersFile conf/workers.properties

 

# Where to put jk logs

JkLogFile logs/mod_jk.log

 

# Set the jk log level [debug/error/info]

JkLogLevel info

 

# Select the log format

JkLogStampFormat  “[%a %b %d %H:%M:%S %Y]”

 

# JkOptions indicates to send SSK KEY SIZE

JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

 

# JkRequestLogFormat

JkRequestLogFormat “%w %V %T”

 

# Mount your applications

JkMount /application/* loadbalancer

 

# You can use external file for mount points.

# It will be checked for updates each 60 seconds.

# The format of the file is: /url=worker

# /examples/*=loadbalancer

#JkMountFile conf/uriworkermap.properties

 

# Add shared memory.

# This directive is present with 1.2.10 and

# later versions of mod_jk, and is needed for

# for load balancing to work properly

JkShmFile logs/jk.shm

 

# Add jkstatus for managing runtime data

<Location /jkstatus/>

    JkMount status

    Order deny,allow

    Deny from all

    Allow from 127.0.0.1

</Location>

 

Please add workers.properties on /usr/local/apache2/conf directory, its content as follows:

worker.list=loadbalancer,debian01,debian02

 

# Define the first node…

worker.debian01.port=8009

worker.debian01.host=10.5.6.246

worker.debian01.type=ajp13

 

worker.debian01.lbfactor=1

#worker.debian01.local_worker=1

worker.debian01.cachesize=100

 

# Define the 2nd node…

worker.debian02.port=8009

worker.debian02.host=10.5.6.44

worker.debian02.type=ajp13

worker.debian02.lbfactor=1

#worker.debian02.local_worker=1

worker.debian02.cachesize=100

 

# Now we define the load-balancing behaviour

worker.loadbalancer.type=lb

worker.loadbalancer.balanced_workers=debian01,debian02

 

worker.loadbalancer.sticky_session=1

 

the debian01, debian02 are jvmRoute alias that defined on ~/jboss/server/default/deploy/jboss-web.deployer/server.xml.

10.5.6.246, 10.5.6.44 are ip address of two nodes.

 

4.5 Start/stop jboss and apache

Login with deploy account, then

cd ~/jboss/bin

./run.sh -b 0.0.0.0

If stop jboss, please input ctrl+c on terminal.

 

Login with root,

Cd /usr/local/apache2/bin

Start apache as follows:

./apachectl –k start

Stop apache as follows:

./apachectl stop

 

5 Deploy application on Jboss

安泰信息:Apache 2.0性能优化—MPM的选择与配置

归档在: apache — JACKEYJ @ 11:25 AM


  Apache 2.0在性能上的改善最吸引人。在支持POSIX线程的Unix系统上,Apache可以通过不同的MPM运行在一种多进程与多线程相 混合的模式下,增强部分配置的可扩充性能。相比于Apache 1.3,2.0版本做了大量的优化来提升处理能力和可伸缩性,并且大多数改进在默认状态下 即可生效。但是在编译和运行时刻,2.0也有许多可以显著提高性能的选择。本文不想叙述那些以功能换取速度的指令,如HostnameLookups等, 而只是说明在2.0中影响性能的最核心特性:MPM(Multi -Processing Modules,多道处理模块)的基本工作原理和配置指令。

  毫不夸张地说,MPM的引入是Apache 2.0最重要的变化。大家知道,Apache是基于模块化的设计,而Apache 2.0更扩展 了模块化设计到Web服务器的最基本功能。服务器装载了一种多道处理模块,负责绑定本机网络端口、接受请求,并调度子进程来处理请求。扩展模块化设计有两 个重要好处:

  ◆ Apache可以更简洁、有效地支持多种操作系统;

  ◆ 服务器可以按站点的特殊需要进行自定制。

  在用户级,MPM看起来和其它Apache模块非常类似。主要区别是在任意时刻只能有一种MPM被装载到服务器中。

  指定MPM的方法

  下面以Red Hat Linux 9为平台,说明在Apache 2.0中如何指定MPM (Apache采用2.0.45)。先解压缩源 代码包httpd-2.0.45.tar.gz,生成httpd-2.0.45目录(Apache 1.3源代码包的命名规则是 apache_1.3.NN.tar.gz,而2.0版则是httpd-2.0.NN.tar.gz,其中NN是次版本号)。

  进入httpd-2.0.45目录,运行以下代码:

$ ./configure –help|grep mpm

  显示如下:

–with-mpm=MPM
Choose the process model for Apache to use.
MPM={beos|worker|prefork|mpmt_os2| perchild|leader|threadpool}

  上述操作用来选择要使用的进程模型,即哪种MPM模块。Beos、mpmt_os2分别是BeOS和OS/2上缺省的 MPM, perchild主要设计目的是以不同的用户和组的身份来运行不同的子进程。这在运行多个需要CGI的虚拟主机时特别有用,会比1.3版中的 SuExec 机制做得更好。leader和threadpool都是基于worker的变体,还处于实验性阶段,某些情况下并不会按照预期设想的那样工 作,所以 Apache官方也并不推荐使用。因此,我们主要阐述prefork和worker这两种和性能关系最大的产品级MPM ( 有关其它的MPM 详细说明,请参见Apache官方文档:http://httpd.apache.org/docs-2.0/mod/)。

  prefork的工作原理及配置

  如果不用“–with-mpm”显式指定某种MPM,prefork就是Unix平台上缺省的MPM。它所采用的预派生子进程方式也 是 Apache 1.3中采用的模式。prefork本身并没有使用到线程,2.0版使用它是为了与1.3版保持兼容性;另一方面,prefork用单 独的子进程来处理不同的请求,进程之间是彼此独立的,这也使其成为最稳定的MPM之一。

  若使用prefork,在make编译和make install安装后,使用“httpd -l”来确定当前使用的MPM,应该会看到 prefork.c(如果看到worker.c说明使用的是worker MPM,依此类推)。再查看缺省生成的httpd.conf配置文件,里面包含 如下配置段:

<IfModule prefork.c>;
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>;

  prefork的工作原理是,控制进程在最初建立“StartServers”个子进程后,为了满足MinSpareServers设置的需 要创建一个进程,等待一秒钟,继续创建两个,再等待一秒钟,继续创建四个……如此按指数级增加创建的进程数,最多达到每秒32个,直到满 足 MinSpareServers设置的值为止。这就是预派生(prefork)的由来。这种模式可以不必在请求到来时再产生新的进程,从而减小了系统 开销以增加性能。

  MaxSpareServers设置了最大的空闲进程数,如果空闲进程数大于这个值,Apache会自动kill掉一些多余进程。这个值不要 设得过大,但如果设的值比MinSpareServers小,Apache会自动把其调整为MinSpareServers+1。如果站点负载较大,可考 虑同时加大MinSpareServers和MaxSpareServers。

  MaxRequestsPerChild设置的是每个子进程可处理的请求数。每个子进程在处理了 “MaxRequestsPerChild” 个请求后将自动销毁。0意味着无限,即子进程永不销毁。虽然缺省设为0可以使每个子进程处理更多的请求,但 如果设成非零值也有两点重要的好处:

  ◆ 可防止意外的内存泄漏;

  ◆ 在服务器负载下降的时侯会自动减少子进程数。

  因此,可根据服务器的负载来调整这个值。笔者认为10000左右比较合适。

  MaxClients是这些指令中最为重要的一个,设定的是Apache可以同时处理的请求,是对Apache性能影响最大的参数。其缺省 值 150是远远不够的,如果请求总数已达到这个值(可通过ps -ef|grep http|wc -l来确认),那么后面的请求就要排队,直到某个已 处理请求完毕。这就是系统资源还剩下很多而HTTP访问却很慢的主要原因。系统管理员可以根据硬件配置和负载情况来动态调整这个值。虽然理论上这个值越 大,可以处理的请求就越多,但Apache默认的限制不能大于256。如果把这个值设为大于256,那么 Apache将无法起动。事实上,256对于负 载稍重的站点也是不够的。在Apache 1.3中,这是个硬限制。如果要加大这个值,必须在“configure”前手工修改的源代码树下的 src/include/httpd.h中查找 256,就会发现“#define HARD_SERVER_LIMIT 256”这行。把256改为要 增大的值(如4000),然后重新编译Apache即可。在Apache 2.0中新加入了ServerLimit指令,使得无须重编译Apache就可 以加大MaxClients。下面是笔者的prefork配置段:

<IfModule prefork.c>;
StartServers 10
MinSpareServers 10
MaxSpareServers 15
ServerLimit 2000
MaxClients 1000
MaxRequestsPerChild 10000
</IfModule>;

  上述配置中,ServerLimit的最大值是20000,对于大多数站点已经足够。如果一定要再加大这个数值,对位于源代码树下server/mpm/prefork/prefork.c中以下两行做相应修改即可:

#define DEFAULT_SERVER_LIMIT 256
#define MAX_SERVER_LIMIT 20000

  worker的工作原理及配置

  相对于prefork,worker是2.0 版中全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理,所以可以处理相对海量的 请求,而系统资源的开销要小于基于进程的服务器。但是, worker也使用了多进程,每个进程又生成多个线程,以获得基于进程服务器的稳定性。这种 MPM的工作方式将是Apache 2.0的发展趋势。

  在configure -with-mpm=worker后,进行make编译、make install安装。在缺省生成的httpd.conf中有以下配置段:

<IfModule worker.c>;
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>;

  worker的工作原理是,由主控制进程生成“StartServers”个子进程,每个子进程中包含固定的 ThreadsPerChild 线程数,各个线程独立地处理请求。同样,为了不在请求到来时再生成线程,MinSpareThreads和 MaxSpareThreads设置了最少和最多的空闲线程数;而MaxClients设置了所有子进程中的线程总数。如果现有子进程中的线程总数不能满 足负载,控制进程将派生新的子进程。

  MinSpareThreads和MaxSpareThreads的最大缺省值分别是75和250。这两个参数对Apache的性能影响并不大,可以按照实际情况相应调节。

  ThreadsPerChild是worker MPM中与性能相关最密切的指令。ThreadsPerChild的最大缺省值是64,如果 负载较大,64也是不够的。这时要显式使用 ThreadLimit指令,它的最大缺省值是20000。上述两个值位于源码树server/mpm /worker/worker.c中的以下两行:

#define DEFAULT_THREAD_LIMIT 64
#define MAX_THREAD_LIMIT 20000

  这两行对应着ThreadsPerChild和ThreadLimit的限制数。最好在configure之前就把64改成所希望的值。注意,不要把这两个值设得太高,超过系统的处理能力,从而因Apache不起动使系统很不稳定。

  Worker模式下所能同时处理的请求总数是由子进程总数乘以ThreadsPerChild值决定的,应该大于等于MaxClients。 如果负载很大,现有的子进程数不能满足时,控制进程会派生新的子进程。默认最大的子进程总数是16,加大时也需要显式声明ServerLimit(最大值 是20000)。这两个值位于源码树server/mpm/worker/worker.c中的以下两行:

#define DEFAULT_SERVER_LIMIT 16
#define MAX_SERVER_LIMIT 20000

  需要注意的是,如果显式声明了ServerLimit,那么它乘以ThreadsPerChild的值必须大于等于MaxClients,而 且MaxClients必须是ThreadsPerChild的整数倍,否则Apache将会自动调节到一个相应值(可能是个非期望值)。下面是笔者 的 worker配置段:

<IfModule worker.c>;
StartServers 3
MaxClients 2000
ServerLimit 25
MinSpareThreads 50
MaxSpareThreads 200
ThreadLimit 200
ThreadsPerChild 100
MaxRequestsPerChild 0
</IfModule>;

  通过上面的叙述,可以了解到Apache 2.0中prefork和worker这两个重要MPM的工作原理,并可根据实际情况来配置Apache相关的核心参数,以获得最大的性能和稳定性。 

其它更详细的写以到http://httpd.apache.org/docs-2.0/

2008年09月16日

安泰资讯:联想旭日150C彻底寿终正寝

归档在: IT新闻 — JACKEYJ @ 9:11 AM

本子是05年 6月份买的,当时还有一下插曲,原来买的神舟的本子出现了质量问题,退掉后买了旭日的本子.刚开始还好的.不过到了 07年夏天本子突然罢工了,加电后一会就不动静了.送修后说是主板故障,于是就进行维修.
再后来都很少使用它,基本上每周不会超过5个小时.
就这样的使用状况,居然也会罢工,中秋节回来开机居然无任何反应,也没任何提示.现在距离过保修期才2个多月就彻底完蛋了.

安泰资讯:windows平台下的opennms

归档在: Linux — JACKEYJ @ 8:47 AM

       OpenNMS 从1.3.8起,提供了Windows的运行支持。虽然还存在一些问题,但是在Windows XP以及以上版本上安装运行OpenNMS还是可行的。
1       Install the JDK
n         从SUN网站下载Java 5 (1.5)或者以上JDK版本(java.sun.com),并安装
n         设置PATH和CLASSPATH
2       Install PostgreSQL
下载PostgreSQL for windows v8.2.6-2,并安装。(注意,文件分区格式要采用NTFS)
n         缺省数据库是:postgres,用户名是:postgres,密码自己设定(postgres是系统用户)
n         使用数据库提供的pgAdmin III 工具,针对postgres用户,新建数据库opennms
3       Install Jicmp
JAVA从来没有提供良好的icmp工具,OpenNMS 从1.3.6版本以来,Jicmp需要单独安装。
下载Jicmp(http://www.opennms.org/index.php/jicmp),并安装,并在环境变量Path中添加“JICMP_HOME”变量。
4       【可选】Install Jrrd
OpenNMS 从1.3.6版本以来,Jrrd需要单独安装。
下载Jrrd(http://www.opennms.org/index.php/jrrd,官方网站上没有提供Widows版本)并安装,并在环境变量Path中添加“JRRD_HOME”变量。
5       Install OpenNMS
准备工作:
n         下载opennms-installer-1.5.90.jar
n         从“服务”管理窗口或者“程序”启动PostgreSQL数据库服务
软件安装:
使用“JAVA(TM)2 Platform Standard Edition Binary”打开opennms-installer-1.5.90.jar,执行安装程式。安装过程中需要提供下面参数:
n         选择JDK Home
n         OpenNMS安装路径$OPENNMS_HOME
n         数据库连接参数
6       配置OpenNMS
6.1    log4j.properties
原始不是Windows下的,依据现有的安装环境进行修改:修改方法用$OPENNMS_HOME全文替换/opt/OpenNMS
$OPENNMS_HOME \contrib\qosdaemon\qos_example_configuration\opennms\log4j.properties
把修改后的文件复制到$OPENNMS_HOME \jetty-webapps\opennms\WEB-INF
6.2    其他配置
通过以上配置,系统已经能够正常运行了。根据情况自己进行其他配置。
7       Run OpenNMS
n         转至目录$OPENNMS_HOME\bin下,运行:opennms.bat start
n         打开IE,输入http://localhost:8980/opennms,用户名/密码都是admin
8       补充
OpenNMS是世界上第一个用开放原始码模式开发的企业级网络管理系统,通过这个文章,希望更多的爱好者能够使用它。当当网购书

Powered by ZJANT