Apache的优化
实验环境:两台linuxhttp服务器。互相访问,互相配置
centos1IP地址:192.168.1.1 域名:www.baidu.com
centos2IP地址:192.168.1.2 域名:www.souhu.com
都需要安装DNS服务以及http服务
制作网页:192.168.1.1 www.baidu.com
制作网页:192.168.1.2 www.souhu.com
保持连接
配置前效果
配置保持连接
vim /etc/httpd/conf/httpd.conf
#添加
keepalive off #关闭保持会话,on开启
keepalivetimeout 30 #设置保持会话超时时间
maxkeepaliverequests 1000 #设置保持会话最大请求数量
配置完成后
网页压缩
配置网页压缩
vim /etc/httpd/conf/httpd.conf
#添加
addoutputfilterbytype deflate text/html text/plain text/css text/xml text/javascript #指定需要压缩的文件类型
DeflateCompressionLevel 9 #指定压缩级别1到9 ,数值越大压缩比例越高
SetOutputFilter DEFLATE #设置压缩方式,gzip和deflate
Apache页面缓存时间
配置页面缓存时间
vim /etc/httpd/conf/httpd.conf
#添加
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 60 seconds" #页面缓存时间 60 秒
</IfModule>
隐藏apache版本信息
配置版本隐藏
vim /etc/httpd/conf/httpd.conf
#添加
ServerTokens Prod ##显示最少的信
ServerSignature Off ##生成页面的页脚
防盗链
1、配置网页(编辑网页)
192.168.1.1
<html>
<head><title></title></head>
<body>
<h1>www.baidu.com</h1>
<img src="./1.jpg" />
</body>
</html>
本地访问
192.168.1.2
<html>
<head><title></title></head>
<body>
<h1>www.guiguedu.cn</h1>
<img src="http://192.168.1.1/1.jpg" alt="访问被拒绝了!对方设置了防盗链!" /> #调用192.168.1.1的图片
</body>
</html>
2、设置防盗链(让192.168.1.2不能访问192.168.1.1的图片)
在192.168.1.1上配置
vim /etc/httpd/conf/httpd.conf
在<Directory “/var/www/html”> 区域中,添加:
RewriteEngine On ##启用重写功能
RewriteCond %{HTTP_REFERER} !^http://baidu.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://baidu.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.baidu.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.baidu.com$ [NC]
RewriteRule .*\.(gif|jpg|png|swf)$ http://www.baidu.com/error.html [R,NC]
重启服务,发现192.168.1.2 不能调用192.168.1.1的网页
压力测试
[root@localhost chin]# ab -n 10000 -c 100 http://192.168.1.1/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.1.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: Apache
Server Hostname: 192.168.1.1
Server Port: 80
Document Path: /index.html
Document Length: 112 bytes
Concurrency Level: 100
Time taken for tests: 2.334 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 4250000 bytes
HTML transferred: 1120000 bytes
Requests per second: 4284.98 [#/sec] (mean)
Time per request: 23.337 [ms] (mean)
Time per request: 0.233 [ms] (mean, across all concurrent requests)
Transfer rate: 1778.43 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.8 0 18
Processing: 7 23 1.9 22 37
Waiting: 0 23 1.9 22 36
Total: 8 23 2.1 23 41
Percentage of the requests served within a certain time (ms)
50% 23
66% 23
75% 23
80% 23
90% 26
95% 27
98% 28
99% 34
100% 41 (longest request)
分割日志
vim /etc/httpd/conf/httpd.conf
找到以下文件
<IfModule log_config_module>
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
#
# The location and format of the access logfile (Common Logfile Format).
# If you do not define any access logfiles within a <VirtualHost>
# container, they will be logged here. Contrariwise, if you *do*
# define per-<VirtualHost> access logfiles, transactions will be
# logged therein and *not* in this file.
#
#CustomLog "logs/access_log" common
#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can use the following directive.
#
CustomLog "|/sbin/rotatelogs -l /var/log/httpd/access_%Y%m%d%H%M%S.log 10" combined ##修改此行内容 此行为日志存放位置 并起名
</IfModule>
-----10秒切割一次日志文件(切割日志时间频率可以自行改变,根据PV来决定,),-l选项是小写的L表示local time(本地时间)------
日志合并
合并之前
日志合并命令
sort -m -t " " -k 4 -o log-all access_20210106090010.log access_20210106090010.log
-b:忽略每行前面开始的空格字符,空格数量不固定时,该选项几乎是必须要使用的("-n"选项隐含该选项,测试发现都隐含)
-c:检查文件是否已经按照顺序排序,如未排序,会提示从哪一行开始乱序
-C:类似于"-c",只不过不输出任何诊断信息。可以通过退出状态码1判断出文件未排序
-d:只处理英文字母、数字及空格,忽略其他的字符
-f:将小写字母视为大写字母
-h:使用易读性数字(例如:2K、1G)
-i:除了040至176之间的ASCII字符外(八进制0-177),忽略其他的字符(忽略无法打印的字符如退格/换页键/回车)
-k:以哪个区间 (field) 来进行排序
-m:将几个排序好的文件进行合并,只是单纯合并,不做排序
-M:将前面3个字母依照月份的缩写进行排序
-n:依照数值的大小排序
-o<输出文件>:将排序后的结果存入指定的文件 #输出的文件名字
-r:降序
-u:忽略相同行
-t<分隔字符>:指定分隔符,默认的分隔符为空白字符和非空白字符之间的空字符
日志合并之后