下文描述的都是关于APACHE服务器的一些简单东西,基于2.2系统版本、且不涉及linux系统参数调整。
我们知道APACHE用了多路处理模块来工作。 也即官方提到的MPM,英文全拼是multi-processing moudule。 自从2系列之后,apache主动加入了基于线程的WORKER模式来替代1.3时代以基于进程的PREFORK,当然并不是说后者已经退出了历史舞台,相反随着时代的进步,它也在与日俱新。
以操作系统CENTOS 6.3为模板,讲述HTTPD中需要注意到的东西。安装采用便捷的系统RPM包,安装完后在/etc/下会有相当规整的目录结构,其实在conf.d中更是包含了许多配置文件。我们可以删除其中有关manual,welcome等之类的东西后用作线上正式服务器。不过仍有几点需要我们注意。
若配置Apache为处理静态资源的中间件,我们需要注意几点
其一,优化进程参数,默认的不符合高并发下的要求。下面有文档中有关这几点的一些说明
MaxClients
Description:Maximum number of connections that will be processed simultaneously。 Any connection attempts over the MaxClients limit will normally be queued。
up to a number based on the ListenBacklog directive。Once a child process is freed at the end of a different request, the connection will then be serviced.
For non-threaded servers (i.e., prefork), MaxClients translates into the maximum number of child processes that will be launched to serve requests. The default value is 256; to increase it, you must also raise ServerLimit.
MaxRequestsPerChild
Description:Limit on the number of requests that an individual child server will handle during its life
The MaxRequestsPerChild directive sets the limit on the number of requests that an individual child server process will handle. After MaxRequestsPerChild requests, the child process will die. If MaxRequestsPerChild is 0, then the process will never expire.
ServerLimit
Description:Upper limit on configurable number of processes
For the prefork MPM, this directive sets the maximum configured value for MaxClients for the lifetime of the Apache process. For the worker MPM, this directive in combination with ThreadLimit sets the maximum configured value for MaxClients for the lifetime of the Apache process. Any attempts to change this directive during a restart will be ignored, but MaxClients can be modified during a restart.
Special care must be taken when using this directive. If ServerLimit is set to a value much higher than necessary, extra, unused shared memory will be allocated. If both ServerLimit and MaxClients are set to values higher than the system can handle, Apache may not start or the system may become unstable.
With the prefork MPM, use this directive only if you need to set MaxClients higher than 256 (default). Do not set the value of this directive any higher than what you might want to set MaxClients to.There is a hard limit of ServerLimit 20000 compiled into the server (for the prefork MPM 200000). This is intended to avoid nasty effects caused by typos.
ListenBackLog
The maximum length of the queue of pending connections. Generally no tuning is needed or desired, however on some systems it is desirable to increase this when under a TCP SYN flood attack. See the backlog parameter to the listen(2) system call.
This will often be limited to a smaller number by the operating system. This varies from OS to OS. Also note that many OSes do not use exactly what is specified as the backlog, but use a number based on (but normally larger than) what is set.
在prefork模式下,Maxlients 也就是希望你的中间件并行处理的最大客户端数.如果连接超过这个数,就会进入队列,这个值是由ListenBackLog这个值决定的,默认值是511。 ServerLimit这个值是用来限制maxclients的,设为与maxclients相同即可。serverlimit这个值可不是越大越好,如果太大会分配不必要的内存给共享内存的。MaxRequestsPerChild 这个值就有意思了,它是用来限定一个进程在它的生命周期内处理多少次请求的,也就是处理多少次请求后进程消亡的, 这样设计的目的是为了预防程序中万一的内存泄漏问题。
根据你系统每天处理的最大并发与闲时情况,设置参数。
由于系统最大并发在1500左右,所以采用如下设置便可。
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 1500
MaxClients 1500
MaxRequestsPerChild 4000
</IfModule>
注: 在PREFORK模式下,每个处理连接的都是以进程为单位,每个进程一个时间点只能处理一个连接。每个连接可能都有随之而来的CPU时间,磁盘IO操作,网络中断处理数据等。 如果在处理大数据的时候,如果有源源不断的请求,就会造成进程阻塞,进而会有调度带来的上下文切换。
其二,通过rpm包安装在这里的中间件,默认没有开启gzip压缩与cache设定,需要我们手动增加,涉及到的模块有delfate,expires,cache等模块。如下,不作解释。
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType p_w_picpath/* "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/js "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
</IfModule>
<IfModule mod_deflate.c>
<Location />
# Insert filter
SetOutputFilter DEFLATE
# Netscape 4.x has same problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Don't compress p_w_picpaths and other
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
AddOutputFilterByType DEFLATE application/x-javascript
# Make sure proxies don't deliver the wrong content
#Header append Vary User-Agent env=!dont-vary
</Location>
</IfModule>
其三, 模块会占用一定的内存资源,与系统调用及加载。所以无关的模块我们需要注掉。根据自身系统裁制。
其四,一些小细节。诸如如下。
ServerSignature Off
ServerTokens Prod
User www
Group www
Options 后的Indexes 去掉之类。