设为首页 收藏本站
查看: 1333|回复: 0

[经验分享] Apache的基础东西

[复制链接]

尚未签到

发表于 2018-11-23 11:50:19 | 显示全部楼层 |阅读模式
  下文描述的都是关于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左右,所以采用如下设置便可。
  
  StartServers    8
  MinSpareServers  5
  MaxSpareServers  20
  ServerLimit   1500
  MaxClients   1500
  MaxRequestsPerChild 4000
  
  

  注: 在PREFORK模式下,每个处理连接的都是以进程为单位,每个进程一个时间点只能处理一个连接。每个连接可能都有随之而来的CPU时间,磁盘IO操作,网络中断处理数据等。 如果在处理大数据的时候,如果有源源不断的请求,就会造成进程阻塞,进而会有调度带来的上下文切换。
  

  

  其二,通过rpm包安装在这里的中间件,默认没有开启gzip压缩与cache设定,需要我们手动增加,涉及到的模块有delfate,expires,cache等模块。如下,不作解释。
  

  
  ExpiresActive On
  ExpiresByType image/* "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"
  
  

  

  

  
  
  # 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 images 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
  
  
  

  

  其三, 模块会占用一定的内存资源,与系统调用及加载。所以无关的模块我们需要注掉。根据自身系统裁制。
  

  

  其四,一些小细节。诸如如下。
  ServerSignature Off
  ServerTokens Prod
  User www
  Group www
  Options 后的Indexes 去掉之类。




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-638645-1-1.html 上篇帖子: 两种apache域名跳转法简单完成重定向 下篇帖子: svn+apache 安装和使用 并与Nginx 整合
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表