HTTP笔记补充
1、URL基本语法
<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>
params:参数,根据用户传递给URL的参数返回页面特定内容
query:结合页面程序,向数据库发起对应查询操作
frag:对于一些页面的位置锚定
2、cookie基础
HTTP是无状态的,即服务器对用户的每一次请求来源处于未知状态。例如在打开某一网站进行登录认证后,随后的每一次的刷新或者打开新页面是需要再次登录认证的,这显然是不方便的,于是,HTTP引入了cookie。
cookie像是一个令牌,由客户端首次访问服务器时从服务器获取,当客户端再次请求该服务器资源时,客户端将携带cookie信息的数据向服务器发起请求,服务器识别cookie后,则认证了客户的身份,对于某些页面有变更,服务器则会重新生成新的cookie并发给客户端。cookie包含了用户的浏览行为与一些用户信息,因此特别注意cookie安全,避免一些非法程序获取分析浏览器cookie、非法窃取收集用户个人信息。
3、HTTP报文格式
请求报文:
<method> <request-URL> <version>
<headers>
<entity-body>
响应报文:
<version> <status> <reason-phrase>
<headers>
<entity-body>
报文字段解释
method: 请求方法,客户端对服务器资源的请求动作
GET:获取一个资源
HEAD:获取资源的响应头部
POST:向服务器发送要服务器处理的数据
PUT:上传主体内容到服务器
DELETE:删除服务器指定资源
TRACE:追踪到达服务所经过的代理服务器
OPTIONS:返回服务器某资源支持的请求方法
status: 响应状态码
1xx:信息提示
2xx:成功
200:成功,已将请求资源通过主体发送到客户端
3xx:重定向
301:请求的资源已被删除,并通过响应首部Location指向了资源新位置。Moved Permanently
302:请求资源被删除,并指向资源临时位置。Found
304:客户端发出条件式请求,服务器回应资源未发生修改。Not Modified
/*例:客户端对某页面资源已有缓存,询问服务器该资源是否被修改。
服务器回应未修改,客户端通过缓存访问页面资源。*/
4xx:客户端错误
401:需要认证来访问资源。Unauthorized
403:请求被禁止。Forbidden
404:找不到资源。Not Found
5xx:服务端错误
500:服务器内部错误。Server Interval Error
502:代理服务器从后端服务器收到了伪响应。Bad Gateway
headers: 首部信息,又分为几类
通用首部:
Date:报文创建时间
Connection:连接状态,如keep-alive,close
Via:报文经过的中间节点
Cache-Control:缓存控制
Pragma:
请求首部:
Accept:通知服务器自己可接受的媒体类型
Accept-Encoding:编码格式,如gzip
Accept-Language:语言
Accept-Charset:字符集
Client-IP:客户端ip
Host:服务器主机名与端口
Referer:正在请求资源的上一级资源
User-Agent:客户端代理,即浏览器
响应首部:
信息性:
Age:响应时长
Server:服务器程序软件名称和版本
协商首部:某资源有多种表示方法时使用
Accept-Ranges:服务器可接受的请求范围类型
Vary:服务器查看的其它首部列表
安全响应首部:
Set-Cookie:向客户端设置cookie;
Set-Cookie2::
WWW-Authenticate:来自服务器的对客户端的质询认证表单
实体首部:
Allow: 列出对此实体可使用的请求方法
Location:告诉客户端真正的实体位于何处
Content-Encoding:主体编码
Content-Language:主体语言
Content-Length:主体的长度
Content-Location:主体真正所处位置;
Content-Type:主体的对象类型
ETag:实体的扩展标签;
Expires:实体的过期时间;
Last-Modified:最后一次修改的时间
entity-body: 实体信息,即实际数据内容
4、常用工具
访问工具
curl [OPTIONS] URL
-A “STRING”:指定用户代理,如"IE11"
-I:仅获取响应报文头部信息
例:curl -I www.test1.com
elinks [OPTIONS] URL
-dump:仅文本显示信息,默认会进入文本式浏览器界面
例:elinks -dump www.test1.com
压力测试工具
ab [OPTIONS] URL
-n:总请求数
-c:请求并发数
-k:以持久连接模式测试
例:ab -n 2000 -c 100 https://172.27.100.1:443/
编译安装LAMP组合
环境:
系统环境:CentOS 6.10 Kernel 2.6.32
主要服务程序:httpd-2.4.9 MariaDB-5.5.46 php-5.4.26
1、安装httpd
# 注:安装"开发工具","服务平台开发"两个包组(系统安装语言不一致,包组名不一致)
# 准备好基础安装包:
apr-1.5.0.tar.bz2
apr-util-1.5.3.tar.bz2
httpd-2.4.9.tar.bz2
# yum install pcre-devel # 安装依赖程序
## 安装apr
# cd /tmp
# tar -xjf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr
## 安装apr-util
# cd /tmp
# tar -xjf apr-util-1.5.3.tar.bz2
# cd apr-util-1.5.3
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
## 安装httpd
# cd /tmp
# tar -xjf httpd-2.4.9.tar.bz2
# cd httpd-2.4.9
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event
选项说明:
--with-zlib 支持传输压缩
--with-pcre 支持pcre的正则表达式
--enable-modules=most 安装的模块,most表示大多数模块
--enable-mpms-shared=all 安装所有支持的mpm模块
--with-mpm=event 启用event模块
# cd /etc/rc.d/init.d/
# cp /etc/rc.d/init.d/httpd /etc/rc.d/init.d/httpd24
# vim /etc/rc.d/init.d/httpd24
/* 修改如下项
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
*/
# chkconfig --add httpd24
# vim /etc/profile.d/httpd24.sh
/* 添加响应可执行程序目录至PATH变量中
export PATH=/usr/local/apache/bin:$PATH
*/
# hash -r 清除命令缓存中的记录
# service httpd24 start 启用服务
2、安装MariaDB
注:安装包为已编译好的二进制程序包
mariadb-5.5.46-linux-x86_64.tar.gz
## 创建相关用户
# useradd -r mysql
# mkdir /data
# chown mysql:mysql /data
## 安装初始化
# tar -xzf /tmp/mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local
# ln -s mariadb-5.5.46-linux-x86_64.tar.gz mysql
# cd mysql
# chown -R root:mysql ./*
# ./scripts/mysql_install_db --datadir=/data --user=mysql
## 提供配置文件
# mkdir /etc/mysql
# cp ./support-files/my-large.cnf /etc/mysql/my.cnf
# vim /etc/mysql/my.cnf
/* 修改如下项
thread_concurrency = 2 线程数
datadir = /data 数据存放目录,建议使用lvm
innodb_file_per_table = on 每个innodb分配单个表空间
skip_name_resolve = on 跳过主机名解析
*/
## 提供服务脚本
# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# service mysqld start
# vim /etc/profile.d/mysqld.sh
/*
export PATH=/usr/local/mysql/bin:$PATH
*/
# mysql_secure_installation # mysql安装初始化,重读该脚本才能重读PATH,否则请使用绝对路径
3、安装php
程序包:php-5.4.26.tar.bz2
## 安装开发环境及依赖程序
# yum groupinstall "Desktop Plateform Development" 同上,不同系统安装语言名称不一致
# yum install bzip2-devel libmcrypt-devel 注:libmcrypt-devel在epel源中提供,
## 编译安装php
# tar -xjf php-5.4.26.tar.bz2
# cd php-5.4.26
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
选项说明:
--with-mysqli= mysql的一个访问接口
--enable-mbstring 多字符类型支持
--with-freetype-dir 多字体支持
--with-zlib 压缩库
--with-apxs2=/usr/local/apache/bin/apxs php作为apache的模块
--with-mcrypt 加解密库
--with-config-file-scan-dir= 以.ini结尾的其他配置文件存放路径
--enable-maintainer-zts event和worker模型必须选项
# make && make install
## 提供配置文件
# cp php.ini-production /etc/php.ini
4、完善工作
## 让apache识别支持php资源
# vim /etc/httpd24/http.conf
/* 添加如下两行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.php index.html 按需修改DirectoryIndex项
LoadModule modules/libphp5.so 如没有安装此模块,手动添加安装
*/
## 测试LAMP组合工作情况
## 注:编译安装的httpd默认DocumentRoot路径为/usr/local/apache/htdocs
# vim /usr/local/apache/htdocs/index.php
/* 编写测试页
<?php
$link = mysql_connect(‘127.0.0.1’,‘root’,‘passwd’);
if($link)
echo "success";
else
echo "failure";
mysql_close();
?>
*/
## 浏览器请求 http://172.27.100.4/ 查看页面是否正常响应