++++++++++HTTP基本特性及配置

ASF, 协议实现的软件程序, HTTPD特性

HTTP基本应用,客户端工具使用(telnet, elinks, curl)

ASF: Apache Software Foundation  Apache 软件基金会

FSF: Free Software Foundaton    自由软件基金会

ASF赖以起家的是: httpd<命名:apache>

http协议实现

Http特性

模块化设计:DSO(Dynamic Share Object)

MPM机制(Multipurpose Process Modules) 多路处理模块

HTTP安装

httpd.i686       //The Apache HTTP Server is a powerful, efficient, and extensible
                //强大、高效、可扩展
/etc/httpd                           //家目录                  
/etc/httpd/conf.d                    //配置片段目录
/etc/httpd/conf/httpd.conf           //主配置
/etc/httpd/logs                      //日志目录
/etc/httpd/modules                   //模块目录
/etc/httpd/run                       //运行中的进程的数据
/etc/rc.d/init.d/httpd               //服务脚本
/etc/sysconfig/httpd                 //服务脚本配置

/usr/lib/httpd/modules
/usr/lib/httpd/modules/mod_actions.so     //DSO机制,动态共享对象
/usr/sbin/apachectl               //apachectl程序
/usr/sbin/httpd                   //MPM为IO,串行响应的进程
/usr/sbin/httpd.event             //MPM为复用I/O模型,并行响应的进程 <2.2的httpd,event不成熟>
/usr/sbin/httpd.worker            //MPM.............................
    //ps axu | fgrep httpd可查看
    
/usr/share/doc/httpd-2.2.15         //<DOCUMENTATION>
/usr/share/doc/httpd-2.2.15/ABOUT_APACHE
/usr/share/doc/httpd-2.2.15/CHANGES
/usr/share/doc/httpd-2.2.15/LICENSE
/usr/share/doc/httpd-2.2.15/NOTICE
/usr/share/doc/httpd-2.2.15/README
/usr/share/doc/httpd-2.2.15/VERSIONING

/usr/share/man/man8/apachectl.8.gz      //<MAN Document>
/usr/share/man/man8/htcacheclean.8.gz
/usr/share/man/man8/httpd.8.gz
/usr/share/man/man8/rotatelogs.8.gz
/usr/share/man/man8/suexec.8.gz

/var/cache/mod_proxy                   //<缓存目录>
/var/lib/dav                           //<程序运行中的数据>
/var/log/httpd                         //《日志目录》
/var/run/httpd                         //《运行中的httpd进程的数据》

/var/www/error/                //错误页面html网页存放目录
/var/www/html                  //DocuRoot目录,资源路径的根映射的位置

httpd配置文件

/etc/httpd/conf/httpd.conf 

//首次配置需要备份文件:
# cp -v /etc/httpd/conf/httpd.conf{,.bak}

配置文件格式

# fgrep ‘Section‘ /etc/httpd/conf/httpd.conf  
### Section 1: Global Environment   //全局环境:定义httpd通用功能特性
### Section 2: ‘Main‘ server configuration //主配置段:一个服务器提供单个站点
### Section 3: Virtual Hosts               //虚拟主机:一个服务器提供多个站点

配置指令格式

Directive(指令)    Value(值)
指令:不区分大小写
值:可能区分大小写

1、port

使用示例

1、备份文件
# cp -v /etc/httpd/conf/httpd.conf{,.bak}

监听在不同端口
Listen 80
Listen 8080

2、重启
# service httpd restart
# ss -tnl 
State       Recv-Q Send-Q                                                               Local Address:Port                                                                 Peer Address:Port 
LISTEN      0      128                                                                             :::8080                                                                           :::*     
LISTEN      0      128                                                                             :::80                                                                             :::*

2、持久和非持久连接

//并发访问量过大场景,时间或数量限制过大时,可能会导致后续的请求被[拒绝服务],可以关闭或调整时间或数量限制

不同版本对应的特性

httpd-2.0配置持久连接

KeepAlive Off         //持久连接功能

MaxKeepAliveRequests 100 // persistent connetion 内所允许的最大请求数量,<调高数字获取较大性能>

KeepAliveTimeout 15    //在TCP连接上等待请求的最大秒数

telnet协议客户端命令

使用示例

连接即刻断开

连接不会立即断开

3、MPM 并发响应模型Multipath Process Module

10多种[适用在多种平台之上]
Linux: prefork, worker, event
编译时,一次只能编译单个模块

1)查看当前使用的模块

2)查看装载的模块

3)更换MPM

复用IO,线程

启动瞬间:5个,1个僵尸
root      2494  1.0  1.4  11688  3560 ?        Ss   19:07   0:00 /usr/sbin/httpd.worker
apache    2496  0.0  0.0      0     0 ?        Z    19:07   0:00 [httpd.worker] <defunct>
apache    2497  0.0  1.1 289420  2964 ?        Sl   19:07   0:00 /usr/sbin/httpd.worker
apache    2498  0.0  1.1 289420  2960 ?        Sl   19:07   0:00 /usr/sbin/httpd.worker
apache    2499  0.0  1.1 289420  2960 ?        Sl   19:07   0:00 /usr/sbin/httpd.worker
apache    2528  0.0  1.1 289420  2960 ?        Sl   19:07   0:00 /usr/sbin/httpd.worker

过了几秒后,4个线程
root      2224  0.0  1.4  11688  3568 ?        Ss   19:06   0:00 /usr/sbin/httpd.worker
apache    2228  0.0  1.1 289420  2972 ?        Sl   19:06   0:00 /usr/sbin/httpd.worker
apache    2229  0.0  1.1 289420  2972 ?        Sl   19:06   0:00 /usr/sbin/httpd.worker
apache    2230  0.0  1.1 289420  2964 ?        Sl   19:06   0:00 /usr/sbin/httpd.worker

复用IO,线程
root      2644  0.2  1.4  11676  3548 ?        Ss   19:08   0:00 /usr/sbin/httpd.event
apache    2647  0.0  1.1 289408  2848 ?        Sl   19:08   0:00 /usr/sbin/httpd.event
apache    2648  0.0  1.1 289408  2852 ?        Sl   19:08   0:00 /usr/sbin/httpd.event
apache    2649  0.0  1.1 289408  2852 ?        Sl   19:08   0:00 /usr/sbin/httpd.event

4)MPM配置详解 /etc/httpd/conf/httpd.conf <网站的承载能力有一定影响>

# prefork MPM
# StartServers: 启动服务器后,预留的进程数
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# ServerLimit: 服务器的生命周期内为MaxClients准备的最大值
# MaxClients:  最大并发数
# MaxRequestsPerChild: 每个进程最大响应请求数
<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      256
MaxClients       256
MaxRequestsPerChild  4000
</IfModule>

# worker MPM
# StartServers:  启动服务预留进程数
# MaxClients:    最大并发数
# MinSpareThreads: 最小并发数
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: 每个进程内的线程数
# MaxRequestsPerChild: 每个线程最多响应请求数
<IfModule worker.c>
StartServers         4
MaxClients         300
MinSpareThreads     25
MaxSpareThreads     75
ThreadsPerChild     25 
MaxRequestsPerChild  0 //0表示没有上限
</IfModule>

配置:影响网站的承载能力

PV承载量= \(每秒请求资源数 * 86400 \)/每个页面的资源数 //满载工作

考虑:全天满负荷运行?带宽使用大小?

带宽:每个PV消耗多少KB * PV承载量

使用示例

查看此模块已经被装载
# httpd -M | fgrep deflate_module
deflate_module (shared)

禁用 #LoadModule deflate_module modules/mod_deflate.so

重载 # service httpd reload

查看 
# httpd -M | fgrep deflate_module
Syntax OK

4、URL资源路径映射之一:DocRoot <资源路径的根映射为文件系统的哪个目录>

使用示例:

默认配置

在配置文件中,定义DocumentRoot指令

DocumentRoot "/var/www/html"

给出默认主页

# cat > /var/www/html/index.html << EOF
<html>
  <head>
  <title>MageEdu</title>
  </head>
  <body>
    <h1>Hello</h1>
	<p> <a href="https://s5.51cto.com/wyfs02/M00/07/31/wKiom1nEuS7Tii_KAAA2TqP_0qY316.png">haha</p>
  </body>
</html>
EOF

重载配置文件

# httpd -t //先测试语法
# service httpd reload   //重载配置文件

在浏览器中访问时:

非默认配置

在配置文件中,定义DocumentRoot指令

DocumentRoot "/www/htdocs"

准备根映射路径

# install -d /www/htdocs
# echo ‘Test Page‘ > /www/htdocs/index.html

重载配置文件

# httpd -t
# service httpd reload

二次测试

5、站点访问控制

6、默认主页面

DirectoryIndex index.html index.html.var

自左向右,依次匹配给出目录下的文件,存在时,显示

7、文件系统路径访问控制

7、1基于来源地址

使用示例

Indexes<下载站使用>

1、允许索引
DocumentRoot "/www/htdocs"
<Directory "/var/www/html">
Options Indexes FollowSymLinks

# mv /etc/httpd/conf.d/welcome.conf{,.bak}
# mv /var/www/html/index.html{,.bak}

2、重载
# httpd -t
# service httpd reload

2、允许索引
DocumentRoot "/www/htdocs"
<Directory "/www/htdocs">
Options Indexes FollowSymLinks

# httpd -t
# service httpd reload

FollowSymlinks

1、允许跟踪
DocumentRoot "/www/htdocs"
<Directory "/www/htdocs">
Options Indexes FollowSymLinks

# ln -s / /www/htdocs/hello

2、不允许跟踪
DocumentRoot "/www/htdocs"
<Directory "/www/htdocs">
Options Indexes -FollowSymLinks

# httpd -t
# service httpd reload

来源地址访问控制

配置
Order deny,allow
deny from 192.168.1.100

7、2用户访问控制

安全域:认证后方能访问的目录,应该用名称标识

定义在/etc/httpd/conf/httpd.conf文件中

1)基于用户认证

2)基于组认证

使用示例

1、创建目录,使之能访问

# install -d /www/htdocs/admin
# echo "hello" > /www/htdocs/admin/index.html

2、添加配置

<Directory "/www/htdocs/admin">
    Options None
    AllowOverride None
    AuthType Basic
    AuthName "Administrator private"
    AuthUserFile "/etc/httpd/conf.d/.htpasswd"
    Require valid-user
</Directory>
# httpd -t
# service httpd reload
# htpasswd -c -m /etc/httpd/conf.d/.htpasswd tom
# htpasswd  -m /etc/httpd/conf.d/.htpasswd obama
# htpasswd  -m /etc/httpd/conf.d/.htpasswd obama

3、添加组配置

<Directory "/www/htdocs/admin">
    Options None
    AllowOverride None
    AuthType Basic
    AuthName "Administrator private"
    AuthUserFile "/etc/httpd/conf.d/.htpasswd"
    AuthGroupFile "/etc/httpd/conf.d/.htgroup"
    Require group weblogic
</Directory>
# httpd -t
# service httpd reload

8、日志设定

错误日志

1、日志为链接路径

访问日志

官方:httpd.apache.org http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#logformat
Documetation --> 2.2 --> 日志文件
192.168.206.1 - tom [18/Sep/2017:02:59:24 +0800] "GET /admin/ HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
%h 宏,替换为请求者IP 
%h remote host, 客户端IP
%l remote logname,登陆的用户名, mod_ident模块 为空时,用 " - " 表示 
%u remote user, 用户访问控制的basic, digst认证的用户名 为空时,用 " - " 表示 
%t Time the request was received (standard english format) 请求被服务器接收的时间
%r First line of request 请求报文的首行: telnet> GET / HTTP/1.1
%>s Status 响应状态码
%b  Size of response in bytes excluding[排除] HTTP headers

%{Referer}i  请求报文当中referer变量值,到达当前页面的入口
    " - " 网站有名
    "不为空时,网站的广告有效"
${User-Agent}i 请求报文中,usergent变量值:
    用户代理:发起请求的用到的进程< elink, ab, telnet, 浏览器, curl > 搜索引擎是爬虫爬出来的>

9、路径别名

使用示例

DocumentRoot "/www/htdocs"
Alias /bbs "/forum/htdocs"
# mkdir -pv /forum/htdocs
# cat /forum/htdocs/index.html 
<html>
   <head> 
   <title>Test</title>
   </head> 
   <body>
     <h1></h1>
     <p> <a href="https://s5.51cto.com/wyfs02/M01/A5/E2/wKioL1nEuHezY3RsAABFaz2unFQ496.png">hello</a>
   </body>
</html>

访问: http://192.168.206.128/bbs/

10、字符集设定

11、虚拟主机

虚拟主机的类型

基于名称完成不同虚拟主机的识别

封装时: HTTP首部、TCP首部、IP首部、帧首部..

Host: 浏览器键入的主机名 //"应用层首部"中附加,非通信子网

配置前提

虚拟主机配置格式

1、基于IP,配置虚拟主机(不易记,IP需要购买)

#DocumentRoot "/www/htdocs"
<VirtualHost 192.168.206.128:80>
  ServerName web1.magedu.com
  DocumentRoot "/www/web1/htdocs"
</VirtualHost>
<VirtualHost 192.168.206.129:80>
  ServerName web2.magedu.com
  DocumentRoot "/www/web2/htdocs"
</VirtualHost>
# mkdir -pv /www/{web1,web2,web3,web4,web5,web6}/htdocs
# echo "web1.magedu.com" > /www/web1/htdocs/index.html
# echo "web2.magedu.com" > /www/web2/htdocs/index.html

# ip addr add 192.168.206.139/24 dev eth0

测试

# httpd -t
Syntax OK
# service httpd reload

登陆

2、基于端口,配置虚拟主机(用的少)

Listen 8080
Listen 8888
<VirtualHost 192.168.206.139:8080>
  ServerName web3.magedu.com
  DocumentRoot "/www/web3/htdocs"
</VirtualHost>
<VirtualHost 192.168.206.139:8888>
  ServerName web4.magedu.com
  DocumentRoot "/www/web4/htdocs"
</VirtualHost>

# echo "web3.magedu.com" > /www/web3/htdocs/index.html
# echo "web4.magedu.com" > /www/web4/htdocs/index.html

测试

# httpd -t 
# service httpd restart

登陆

3、基于Host,配置虚拟主机(在应用层封装Host首部即可)

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName web5.magedu.com
  DocumentRoot "/www/web5/htdocs"
</VirtualHost>
<VirtualHost *:80>
  ServerName web6.magedu.com
  DocumentRoot "/www/web6/htdocs"
</VirtualHost>

# echo "web5.magedu.com" > /www/web5/htdocs/index.html
# echo "web6.magedu.com" > /www/web6/htdocs/index.html

测试

# httpd -t
# service httpd reload

登陆

1、启动一个新的客户机
完成主机名到ip的映射
192.168.206.128 web1.magedu.com web2.magedu.com web3.magedu.com web5.magedu.com
2、测试
# yum -y install curl

# curl web5.magedu.com
web5.magedu.com
# curl web6.magedu.com
web6.magedu.com
时间: 2024-08-22 12:10:14

++++++++++HTTP基本特性及配置的相关文章

[Redis]Redis高级特性的配置及使用

---------------------------------------------------------------------------- [Redis安全性] 一 . 默认我们进入Redis客户端是不需要密码的,但是在使用任何命令前保证已授权,这在一个你不信任其它人的环境里可能有用. 1. 按照如下方式可设置密码: ① 修改配置文件(默认为/usr/local/etc/redis.conf),找到requirepass foobared,默认已注释掉:将密码设为123456只需要

asp.net Web API简单的特性路由配置

一,控制器代码: [RoutePrefix("ajilisiwei")]  //加URL前缀 (非必需)    public class ShoppingController : ApiController    { [HttpPost]        [Route("Test/{product}")]          public IEnumerable<Product> AddProduct([FromUri]string product,[Fro

AutoMapper小技巧:通过特性配置读取缓存

在项目开发中经常会遇到这样的场景:查询一个复杂实体,其中一部分字段数据从数据库中直接查出,另一部字段数据从缓存中取出.这里通过AutoMapper和特性,提供一种优雅的编码方法. 这种方法的大概思路是:在成员的特性中配置好[缓存字典的key].[与缓存字典关联的外键名称]和[缓存字典里目标字段的名称].然后根据上述参数从缓存里取出需要的数据,最后通过配置AutoMapper的Profile来将数据映射到要查询的list里. 可能这样说会让人有点摸不着头脑,接下来就开始一步一步讲解如何编码. 1.

Apatche配置基础

一.目录 Bin:Apatche执行文件所在目录 error:错误应答文件目录.服务器设置或浏览器要求的数据错误时,浏览器出现的错误信息存放的位置. htdocs:web文档的根目录.用户存放网页和默认首页的位置. icons:提供Apache预设给于的小图示. Logs:Apatche文档日志存放位置.包括访问日志acces_log和错误日志error_log.access_log记录了所有对服务器的访问活动,例如记录了客户端的IP地址和什么时间访问了哪个页面等信息.error_log记录了所

HAproxy负载均衡-配置篇

安装HAProxy及环境配置 红帽的yum源已经为我们提供了最新版本的haproxy,所以我们只需要yum安装即可 [[email protected] ~]# yum install haproxy -y 配置haproxy的日志 编辑rsyslog [[email protected] haproxy]# vim /etc/rsyslog.conf 将以下参数开启 $ModLoad imudp $UDPServerRun 514 加入参数: *.info;mail.none;authpriv

Jetty学习三:配置概览-需要配置什么

上一节讲述了怎么配置Jetty,这节将告诉你使用Jetty你需要配置些什么. 配置Server Server实例是Jetty服务端的中心协调对象,它为所有其他Jetty服务端组件提供服务和生命周期管理.在标准Jetty发布中,核心的服务端配置是在etc/jetty.xml文件中,你也能在其中包含其他服务端配置,可以包括: 1)ThreadPool Server实例提供了一个线程池,你可以在etc/jetty.xml中配置最大线程数和最小线程数. 2)Handlers Jetty服务端只能有一个H

Linux之Web服务(2)Httpd服务配置之一

Linux之Web服务(2)Httpd服务配置之一 Apache HTTP Server Apache HTTP 服务器,简称Apache,是非常留下的Web服务器软件.通常和脚步语言比如PHP,数据库MySQL一起工作,合成为LAMP栈(Linux, Apache, MySQL, PHP). 当然流行的Web服务器还有nigix,但是nigix虽然轻量级很稳定,但是功能并不如Apache HTTP功能丰富,并且现在的Apache HTTP还支持模块化功能,及可以开发自己的功能模块并加入到此We

[转载] zookeeper应用——集中配置管理系统的实现

转载自http://blog.csdn.net/huangfengxiao/article/details/8844239 1 场景描述 参考http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/index.html,实现自己的集中配置管理系统. 术语:配置管理(Configuration Management) 一个带配置的服务程序,部署在若干台机器上,如果配置发生了变化,接下去要进行的操作是停止所有机器上的该程序,修改

(转)C++开发人脸性别识别教程(3)——OpenCv配置和ImageWatch插件介绍

原文地址:http://blog.csdn.net/u013088062/article/details/50435079 OpenCv是C++图像处理的重要工具,这个人脸性别识别的项目就是借助OpenCv进行开发的.虽然网上已经有了很多关于OpenCv的配置教程,但出于教程完整性考虑,这里还是用专门的一篇博客来介绍OpenCv的具体配置方法,同时也介绍一下OpenCv中的一个强有力的图像处理插件——ImageWatch. 由于这个程序是一年前写的,当时的OpenCv的最新版本为2.4.9(现在