web容器(01):Apache配置监控

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web容器(01):Apache配置监控相关的知识,希望对你有一定的参考价值。

响应时间长可能原因:

硬件、应用服务器、网络、负载机、中间件线程池排队、数据库连接池排队、sql语句、jvmGc、代码逻辑

如何分析:

分析系统架构节点

开发在日志中打印方法在调用时消耗的时间

 

web容器:nginx apache tomcat resin weblogic websphere

apache:处理静态资源性能好些,处理servlet这样的动态请求性能会差些

tomcat:处理动态请求性能会好些

 

apache:

1、通渠配置文件httpd.conf

1)位置:

yum安装:/etc/httpd/conf/

编译安装:/opt/lampp/etc/  

etc/extra目录下有多个conf文件,在通渠配置文件中,如果要用到extra中的conf文件,需要在通渠配置文件中include对应的extra中的conf文件,或者将要使用的配置文件的内容拷到通渠配置文件中。

2)通渠配置文件相关配置解释:

ServerRoot "/etc/httpd" → apache的启动路径

PidFile run/httpd.pid → apache启动的时候会产生一个pid

Timeout 60 → 接收和发送的超时时间是60s

KeepAlive Off → 长连接(建立握手,通道一直在),off此处标识短连接,长连接的性能较好,但是弊端也是显而易见的,线程和线程池之间的通道一直存在,建立连接的线程不用的时候连接还是存在,那么其它的线程,无法和线程池进行连接,导致系统无响应。

在jmeter中默认使用use keepalive长连接,lr中默认使用短连接,使用长短连接对性能测试的tps影响可能会相差很大。使用长连接测试出来的tps可能会大很多。

MaxKeepAliveRequests 100 → 最大使用多少个长连接

KeepAliveTimeout 15 → 长连接最大的等待时间,如果这个时间超过配置的时间,这个连接不生效

# prefork MPM → 进程工作模式。多路处理模块(MPM)实现了一个非线程型的、预派生的web服务器,它适合于没有线程安全库,需要避免线程兼容性问题的系统。它是要求将每个请求相互独立的情况下最好的MPM,这样若一个请求出现问题就不会影响到其他请求。

StartServers 8 → 初始化启动进程数
MinSpareServers 5 → 最小服务器空闲进程数(预派生)
MaxSpareServers 20 → 最大服务器空闲进程数
ServerLimit 256 → apache同时处理服务进程数
MaxClients 256 → apache可以同时处理的请求数,其缺省值150是远远不够的,如果请求总数已达到这个值(可通过ps -ef|grep http|wc -l来确认),那么后面的请求就要排队,直到某个已处理请求完毕。这就是系统资源还剩下很多而HTTP访问却很慢的主要原因。Apache默认的限制不能大于256。
MaxRequestsPerChild 4000 → 一个进程在其生存周期内,最多处理4000次请求,如果处理4000次请求后,该进程被销毁,避免僵尸进程,防止意外的内存泄漏,在服务器负载下降的时候,自动减少进程数

# worker MPM → 混合的多进程多线程工作模式。使用了多进程,每个进程又有多个线程,以获得基于进程的MPM的稳定性。每个进程可以拥有的线程数量是固定的。服务器会根据负载情况增加或减少进程数量。

StartServers 4 → 初始化启动进程数
MaxClients 300 → 最大启动的进程数
MinSpareThreads 25 → 最小空闲线程数
MaxSpareThreads 75 → 最大空闲线程数
ThreadsPerChild 25 → 每个进程下有25个线程
MaxRequestsPerChild 0 → 进程永远存活

Listen 80 → 监听的端口号

LoadModule → 加载动态库 .so标识动态库

Include conf.d/*.conf → 将conf.d/*.conf中的配置文件加载进来

DocumentRoot "/var/www/html" → apache的工作路径,存放应用程序代码

ErrorLog logs/error_log → 错误日志路径

LogLevel warn → 日志级别,一般配置info级别

AddDefaultCharset UTF-8 → 默认字符集

# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html

配置监听:

<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
# Allow from .example.com
</Location>

<Location /server-info>
SetHandler server-info
Order deny,allow
Deny from all
# Allow from .example.com
</Location>

修改好通渠配置文件后,重启apache:

service httpd stop/service httpd start

 

2、Apache的监听

1)监听server-status

在通渠配置文件httpd.conf中配置监听<Location /server-status>的Allow from .example.com注释掉时,访问http://192.168.20.129/server-status,页面提示不允许访问,如下所示:

技术分享

配置监听<Location /server-status>的Allow from all,访问http://192.168.20.129/server-status,页面访问如下图所示:

技术分享

对照配置文件页面内容释义,画图解释:技术分享

方便我们监控进程的状态,进而分析系统的性能。

2)监听server-info

在通渠配置文件中配置监听<Location /server-info>的Allow from all,访问http://192.168.20.129/server-info,页面访问如下图所示:

技术分享

 

3、切换apache为线程工作模式

默认情况下,apache为进程工作模式,那么如何将apache切换为线程工作模式呢?

1)进入/usr/sbin目录

[[email protected] sbin]# cd /usr/sbin/
[[email protected] sbin]# ls http*
httpd httpd.event httpd.worker

 2)重命名httpd文件

[[email protected] sbin]# mv httpd httpd_prefork
[[email protected] sbin]# mv httpd.worker httpd
[[email protected] sbin]# httpd -l → 查看apache的工作模式
Compiled in modules:
core.c
worker.c → 修改后,工作模式变为worker.c
http_core.c
mod_so.c

3)重启apache

[[email protected] sbin]# service httpd stop
停止 httpd: [确定]
[[email protected] sbin]# service httpd start
正在启动 httpd: [确定]

4)再次访问http://192.168.20.129/server-status页面

通渠配置文件中的配置如下:

# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule worker.c>
StartServers 4
MaxClients 300
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>

第一次访问页面:

1 requests currently being processed, 74 idle workers

第二次第三次... ...第N次访问页面:

1 requests currently being processed, 49 idle workers

根据通渠配置文件里的配置,apache在启动时,开启了4个进程,每个进程里有25个线程,那么启动后,总共是启动了100个线程,又由于配置了MinSpareThreads和MaxSpareThreads,apache kill掉1个进程,那么在页面显示中,就显示了1个繁忙线程和74个空闲线程。

第N次访问页面后,最终稳定下来的是50个线程。

5)页面释义,类似进程工作模式

Apache Server Status for 192.168.20.129
1 requests currently being processed, 49 idle workers
_____________W___________.......................................
................................................................
_________________________.......................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................

Scoreboard Key:
"_" Waiting for Connection, "S" Starting up, "R" Reading Request,
"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
"C" Closing connection, "L" Logging, "G" Gracefully finishing,
"I" Idle cleanup of worker, "." Open slot with no current process

PID Key:


2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: W , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2555 in state: _ , 2555 in state: _
2555 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ , 2445 in state: _
2445 in state: _ , 2445 in state: _ ,

6)进程和线程监控页面的意义

举例:

max:75线程

70左右的线程都处于工作状态(发送、接收、写日志、解析、连接断开再连接)

tps:3000  分布式负载1秒发送200个请求 wait1

web容器:75个线程 wait2

16颗cpu,同时处理16个请求 wait3

 

响应时间慢,看耗时地方,看线程池是否在排队(线程池工作状态,大部分都在工作)

最终发送到数据库的请求越来越少

负载机越好,性能表现可能越好

需要检测线程池请求是否存在排队现象,如果存在,需要加大线程池

看线程池状态耗时的地方

 

4、apache日志

1)位置

[[email protected] httpd]# ls
conf conf.d logs modules run
[[email protected] httpd]# pwd
/etc/httpd
[[email protected] httpd]# cd logs/
[[email protected] logs]# ls
access_log access_log-20170702 error_log-20170618 error_log-20170702
access_log-20170618 error_log error_log-20170626

2)日志

error_log → 错误日志

access_log → 增长日志

可以看到线上的功能访问热度,热度:性能测试的功能点

每一秒功能最大的并发数,重点功能,用户用得多的功能

 

5、apache的功能

和nginx一样,既可以做web容器,也可以做负载均衡服务器

apache+tomcat做负载均衡,前端apache后端tomcat

 
































































































以上是关于web容器(01):Apache配置监控的主要内容,如果未能解决你的问题,请参考以下文章

Apache ActiveMQ 远程代码执行漏洞 (CVE-2016-3088)分析

web中间件之apache

Apache配置文件重要指令详解

如何配置高性能的web服务器apache,nginx

Web服务器(Apache)与Servlet容器(Tomcat)

Apache的安装与AWstats分析系统