apache可以理解为一个非常灵活可配置的高性能web服务框架。
包括最前面处理http请求的MPM可配:perfork, worker, event
包括可动态加载若干第三方动态库,完成插件任务:mod_proxy, mod_ssl, mod_cache, mod_rewrite ......
包括上层应用通过hook机制,实现自己的请求handler,并通过mod加载机制加载。
高性能体现在:
1、worker采用进程+线程动态可调整的策略,每个请求会被委派到某个空闲进程的某个线程。类似动态可扩展的BIO
event采用事件驱动模式(epoll),主线程监听socket事件,若干子进程和子线程,执行具体业务逻辑。类似NIO
2、通过mmap完成父子进程的通信
3、通过sendfile处理静态文件,直接在操作系统内核将静态文件从磁盘发送到网络配适层,绕过了内核到用户层在到内核的数据copy
============== install ======================
make distclean && ./configure --prefix=/home/admin/apache --with-mpm=worker --enable-modules=all --enable-ssl --enable-proxy && make && make install
compile all modules as static in default
--enable-modules=all , if not set, some modules like mod_expire will not load
--enable-modules-shared=all, compile all modules as xx.so, and need to LoadModule in config
--enable-ssl, --enable-proxy 需要额外开启
--enable-proxy 会开启全部proxy模块,包括:mod_proxy_ajp, mod_proxy_http, mod_proxy_ftp, mod_proxy_balancer, mod_proxy_scgi
/apache/bin/apachectl -f /home/admin/xx/httpd.conf -t ( check conf syntax)
/apache/bin/apachectl -l (list all modules)
================= apr =====================
对于httpd-2.4+,需要升级linux下面的apr环境
下载:http://apr.apache.org/download.cgi
# tar -zxvf apr-1.4.2.tar.gz
#cd apr-1.4.2
#./configure --prefix=/usr/local/apr
#make
#sudo make install
#tar -zxvf apr-util-1.3.9.tar.gz
#cd apr-util-1.3.9
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
#make
#sudo make install
下载:http://sourceforge.net/projects/pcre
#unzip -o pcre-8.10.zip
#cd pcre-8.10
#./configure --prefix=/usr/local/pcre
#make
#sudo make install
================= rewrite ==================
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/styles/
RewriteRule ^/8081.html http://10.249.192.168:8081/8081.html [R] ## means 302 redirect to other server
RewriteRule ^/8081.html http://10.249.192.168:8081/8081.html [P] ## means forward to other server, need mod_proxy in apache,不支持https的代理
================= mod_proxy ==================
proxy转发后,ip地址变为apache主机的ip,源地址ip存储在request header中: x-forwarded-for = 10.22.9.195
SSLProxyEngine On
ProxyRequests off ( 停止正向代理)
ProxyPass /images ! (不往此处转发)
ProxyPass /styles !
ProxyPass /scripts !
ProxyPass / https://10.249.192.168:54602/
ProxyPassReverse / https://10.249.192.168:54602/ (必须加,否则在子系统中的redirect跳转地址就不对)
ProxyPass /openapi https://10.249.192.168:54802/openapi (可以将部分url定向到另一个back server)
ProxyPassReverse /openapi https://10.249.192.168:54802/openapi
<IfModule worker.c>
StartServers 10 ( 启动时的httpd进程数)
MaxClients 1024 (最大并发连接)
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 64 (每个进程的最大线程数)
ServerLimit 75 (进程最大数量)
</IfModule>
===================== mod_jk =====================
wget ...tomcat-connectors-1.2.35-src.tar.gz
tar -zxvf tomcat-connectors-1.2.35-src.tar.gz
cd tomcat-connectors-1.2.35-src/native
./configuration --with-apxs=/home/admin/apache/bin/apxs
make
make install
// find mod_jk.so in apache/modules
通过AJP协议和tomcat进行连接,一个apache可以配置映射多个tomcat。ajp协议下,tomcat接收到的ip为源地址ip。
mod_jk.conf配置:
LoadModule jk_module /apsara/alimail/webdeploy/aliyunid/aliyunid_apache_running/modules/mod_jk_for_httpd_2.2.15.so
JkLogFile "|/apsara/alimail/webdeploy/aliyunid/httpd-2.2.15/bin/rotatelogs /apsara/alimail/webdeploy/aliyunid/aliyunid_apache_runnin
g/output/logs/mod_jk.log 86400"
JkLogLevel error
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkOptions +ForwardURICompatUnparsed
JkOptions +ForwardDirectories
JkMountCopy All
JkShmFile /apsara/alimail/webdeploy/aliyunid/aliyunid_apache_running/output/logs/jk.shm
JkMount /openapi/* openapi
JkMount /innerapi/* openapi
JkMount /oauth/request_token openapi
JkMount /oauth/access_token openapi
JkMount /* webpage
JkUnMount /images/* webpage
JkUnMount /styles/* webpage
JkUnMount /scripts/* webpage
JkWorkersFile /apsara/alimail/webdeploy/aliyunid/aliyunid_apache_running/conf/workers.properties
workers.properties:
worker.template.type=ajp13
worker.template.socket_timeout=60
worker.template.connect_timeout=60000
worker.template.reply_timeout=60000
worker.list=openapi,webpage
worker.openapi.type=lb # load balancer
worker.openapi.balance_workers=openapi0,openapi1
worker.webpage.type=lb
worker.webpage.balance_workers=webpage0,webpage1
worker.openapi0.port=54990
worker.openapi0.host=10.249.20.48
worker.openapi0.reference=worker.template
worker.openapi1.port=54990
worker.openapi1.host=10.249.26.45
worker.openapi1.reference=worker.template
worker.webpage0.port=54700
worker.webpage0.host=10.249.20.48
worker.webpage0.reference=worker.template
worker.webpage1.port=54700
worker.webpage1.host=10.249.26.45
worker.webpage1.reference=worker.template
tomcat connector配置:
<Connector port="__MY_JBOSS_AJP_PORT__" address="${jboss.bind.address}"backlog="256"
maxThreads="300"
emptySessionPath="true"
enableLookups="false"
connectionTimeout="600000"
disableUploadTimeout="true"
protocol="AJP/1.3"
URIEncoding="UTF-8"
maxPostSize="6291456" />
转自:http://blog.csdn.net/cnhzgb/article/details/6795144