一,查看网页的header头信息
curl -I www.baidu.com
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Wed, 14 Dec 2016 08:49:46 GMT
Content-Type: text/html
Content-Length: 277
Last-Modified: Mon, 13 Jun 2016 02:50:17 GMT
Connection: Keep-Alive
ETag: "575e1f69-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytes
二,http协议请求原理
三,web架构优化方案
高并发网站从动态转为静态例子:
1,门户新闻业务(一旦发布,几乎不会更改)
2,视频网站业务(一旦发布完成,几乎不会改动)
3,blog / bbs / sns/微博社区业务(用户发布完成,可能会随时更新)
浅谈千万级PV/IP规模高性能高并发网站架构(http://oldboy.blog.51cto.com/2561410/736710)
四,并发的理解
单位时间内,服务器能够同时处理的最大连接数
五,APACHE2.2讲解
1)安装:
rpm -qa apache
wget http://xxxxxxx
tar zxvf httpd2.2.27.tar.gz
cd httpd2.2.27
./configure \
--prefix=/application/apache2.2.27 \ 安装目录(保持和老师一致)--enable-expires \ 网页头文件过期设定
--enable-headers \ 网页头
--enable-modules=most \ 激活大多数模块
--enable-so \
--with-mpm=worker \ 选择worker 线程模式,效率高安全低,(另个一个是 prefork 进程模式,安全高效率低,工作中用任何一个都行)
--enable-deflate \ 压缩
--enable-rewrite 地址重写
如果报错缺少 zlib ,则 yum install zlib zlib-devel -y (报错什么,一般就是缺某某-devel,这是经验)
echo $?
make && make install
ln -s /application/apache2.2.27/ /application/apache
cd /application/apache (apache安装目录)
2)运行
查看/application/apache/bin/apachectl 各种参数功能:
a,检查语法:/application/apache/bin/apachectl -t
b,启动httpd:/application/apache/bin/apachectl start
平滑重启:/application/apache/bin/apachectl graceful (替代 restart)
c,查看端口:lsof -i :80 或 查看httpd进程:ps -ef|grep httpd 或 netstat -lnt|grep 80
d,网页访问:http://IP地址
e,如果无法访问,查看防火墙,selinux是否关闭
3),目录文件:
/application/apache
|-- bin
| |-- ab 压力测试工具
| |-- apachectl apache启动命令,需要重点掌握,apachectl是一个脚本
| |-- apr-1-config
| |-- apu-1-config
| |-- apxs apache扩展编译工具,比如将来安装后,还要安装其他模块,就需要这个工具
| |-- checkgid
| |-- dbmmanage
| |-- envvars
| |-- envvars-std
| |-- htcacheclean
| |-- htdbm
| |-- htdigest
| |-- htpasswd apache本身也可以做一打开站点就需要输入密码的功能
| |-- httpd apachectl 实际是执行httpd工具
| |-- httxt2dbm
| |-- logresolve
| `-- rotatelogs apache的日志轮循工具,循环打印日志,不至于一个日志体积太大
|-- build
| |-- apr_rules.mk
| |-- config.nice
| |-- config_vars.mk
| |-- instdso.sh
| |-- library.mk
| |-- libtool
| |-- ltlib.mk
| |-- make_exports.awk
| |-- make_var_export.awk
| |-- mkdir.sh
| |-- program.mk
| |-- rules.mk
| `-- special.mk
|-- cgi-bin
| |-- printenv
| `-- test-cgi
|-- conf
| |-- extra
| | |-- httpd-autoindex.conf
| | |-- httpd-dav.conf
| | |-- httpd-default.conf
| | |-- httpd-info.conf
| | |-- httpd-languages.conf
| | |-- httpd-manual.conf
| | |-- httpd-mpm.conf
| | |-- httpd-multilang-errordoc.conf
| | |-- httpd-ssl.conf
| | |-- httpd-userdir.conf
| | `-- httpd-vhosts.conf
| |-- httpd.conf
| |-- magic
| |-- mime.types
| `-- original
| |-- extra 额外配置文件,经常访问使用
| | |-- httpd-autoindex.conf
| | |-- httpd-dav.conf
| | |-- httpd-default.conf
| | |-- httpd-info.conf
| | |-- httpd-languages.conf
| | |-- httpd-manual.conf
| | |-- httpd-mpm.conf
| | |-- httpd-multilang-errordoc.conf
| | |-- httpd-ssl.conf
| | |-- httpd-userdir.conf
| | `-- httpd-vhosts.conf
| `-- httpd.conf 主配置文件
|-- error
| |-- HTTP_BAD_GATEWAY.html.var
|-- htdocs 站点根目录
| `-- index.html 默认的首页文件,httpd.conf 中的 DirectoryIndex 中指定
|-- lib
| |-- apr.exp
| |-- aprutil.exp
| |-- libapr-1.a
| |-- libapr-1.la
| |-- libapr-1.so -> libapr-1.so.0.5.0
| |-- libapr-1.so.0 -> libapr-1.so.0.5.0
| |-- libapr-1.so.0.5.0
| |-- libaprutil-1.a
| |-- libaprutil-1.la
| |-- libaprutil-1.so -> libaprutil-1.so.0.5.3
| |-- libaprutil-1.so.0 -> libaprutil-1.so.0.5.3
| |-- libaprutil-1.so.0.5.3
| |-- libexpat.a
| |-- libexpat.la
| |-- libexpat.so -> libexpat.so.0.5.0
| |-- libexpat.so.0 -> libexpat.so.0.5.0
| |-- libexpat.so.0.5.0
| `-- pkgconfig
| |-- apr-1.pc
| `-- apr-util-1.pc
|-- logs
| |-- access_log 访问日志
| |-- cgisock.4473
| |-- error_log 错误日志
| `-- httpd.pid 服务进程文件
`-- modules
`-- httpd.exp
4)配置文件 httpd.conf
a),排除空行和注释的行,并生成新文件:grep -Ev "#|^$" httpd.conf > httpd.conf.ori
b),没有index默认页面时,禁止目录展示: Options -Indexes FollowSymLinks (去掉Indexes 或 -Indexes)
17 <Directory "/application/apache2.2.27/htdocs">
18 Options Indexes FollowSymLinks
19 AllowOverride None
20 Order allow,deny
21 Allow from all
22 </Directory>
5)扩展配置文件
httpd-vhosts.conf : 基于域名的虚拟主机
NameVirtualHost *:80 (*:任何域名 80:端口)
httpd-mpm.conf
<IfModule mpm_prefork_module> #prefork 模式
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150 #并发
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_worker_module>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
httpd-default.conf : 一些超时参数等
ServerTokens Full #控制不显示版本号
ServerSignature On #控制不显示版本号
5)虚拟主机:一个http服务器,要配置多个站点就需要虚拟主机(基于端口;基于IP(本机测试配置别名IP:ifconfig eth0:0 192.168.1.222/24 up,重启失效);基于域名)
基于域名(常用)配置:
a,目录结构
www.test.com /var/html/www
blog.test.com /var/html/blog
bbs.test.com /var/html/bbs
每个目录站点下新建index.html文件:
for name in www blog bbs;do echo "http://$name.test.com" > /var/html/$name/index.html; done
b,httpd.conf 中打开:
Include conf/extra/httpd-vhosts.conf
Include conf/extra/httpd-mpm.conf
c,extra/httpd-vhosts.conf 增加
<VirtualHost *:80>
ServerAdmin 1302520868@qq.com
DocumentRoot "/var/html/www"
ServerName www.test.com
ServerAlias test.com
ErrorLog "logs/www-error_log"
CustomLog "logs/www-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 1302520868@qq.com
DocumentRoot "/var/html/blog"
ServerName blog.test.com
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 1302520868@qq.com
DocumentRoot "/var/html/bbs"
ServerName bbs.test.com
ErrorLog "logs/bbs-error_log"
CustomLog "logs/bbs-access_log" common
</VirtualHost>
d,httpd.conf 底部增加:(三个域名都在/var/html 下,所以配置一个就OK了)
<Directory "/var/html">
Options FollowSymLinks
AllowOverride None #控制.htaccess是否可用,None不可用,All可用
Order allow,deny
Allow from all
</Directory>
e,测试配置文件:bin/apachectl -t
f,平滑重启httpd:bin/apachectl graceful
g,浏览器所在的主机windows 的 hosts 文件添加三个域名的DNS指向
h,解决FQDN (fully qualified domain name):
httpd.conf 增加:ServerName 127.0.0.1:80
6),apache日志
a,日志格式:
通用日志格式(Common Log Format)
组合日志格式(Combined Log Format 工作中常用)
b,日志轮循
工具:cronolog
cd /home/oldboy/tools
wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
tar zxvf cronolog-1.6.2.tar.gz
cd cronolog-1.6.2
./configure
make && makeinstall
会生产一个文件:/usr/local/sbin/cronolog
c,配置
按天轮询(生产环境常见用法,推荐使用):
CustomLog "|/usr/local/sbin/cronolog /app/logs/access_bbs_%Y%m%d.log" combined #路径写全路径,前面有一个竖线|
<VirtualHost *:80>
ServerAdmin 1302520868@qq.com
DocumentRoot "/var/html/bbs"
ServerName bbs.test.com
ErrorLog "logs/bbs-error_log"
CustomLog "|/usr/local/sbin/cronolog /app/logs/access_bbs_%Y%m%d.log" combined
</VirtualHost>