httpd-2.2安裝,請(qǐng)求狀態(tài)詳解及虛擬主機(jī)設(shè)定

Centos7系統(tǒng)下實(shí)現(xiàn)httpd-2.2的安裝,并分別實(shí)現(xiàn)prefork、worker、event等幾種工作方式

centos7中光盤的本地yum源默認(rèn)httpd程序包是2.4.6版本的,要安裝httpd-2.2就要下載好源碼包和對(duì)應(yīng)的依賴包進(jìn)行編譯安裝,安裝之前先簡(jiǎn)單講解一下httpd的這三種處理模型,需要注意的是httpd-2.2的event事件驅(qū)動(dòng)模型還只是測(cè)試使用功能并且不支持同時(shí)編譯多個(gè)模塊,每次只能選定一個(gè)模塊.
其對(duì)應(yīng)的配置文件為:/etc/httpd/conf/httpd.conf

prefork:多進(jìn)程模型,每個(gè)進(jìn)程響應(yīng)一個(gè)請(qǐng)求,一個(gè)主進(jìn)程負(fù)責(zé)生成n個(gè)子進(jìn)程及回收子進(jìn)程,創(chuàng)建套接字,接收請(qǐng)求,并將其派發(fā)給某子進(jìn)程進(jìn)行處理.每個(gè)子進(jìn)程處理一個(gè)請(qǐng)求即使沒有用戶請(qǐng)求也會(huì)預(yù)先生成幾個(gè)空閑進(jìn)程,隨時(shí)等待用于響應(yīng)用戶請(qǐng)求,最大空閑進(jìn)程數(shù)不能超過1024個(gè).

prefork的配置      
    <IfModule prefork.c>
    StartServers       8  進(jìn)程啟動(dòng)后立即啟動(dòng)的空閑進(jìn)程數(shù)量
    MinSpareServers    5  最小空閑進(jìn)程數(shù)量
    MaxSpareServers   20  最大空閑進(jìn)程數(shù)量
    ServerLimit      256  一個(gè)周期內(nèi)允許最大的進(jìn)程數(shù)量
    MaxClients       256  最大允許啟動(dòng)的服務(wù)器子進(jìn)程數(shù)量
    MaxRequestsPerChild  4000  允許最大請(qǐng)求的數(shù)量(超過數(shù)量就會(huì)關(guān)閉該進(jìn)程)
    </IfModule> 

worker:多進(jìn)程多線程模型,每線程處理一個(gè)用戶請(qǐng)求,一個(gè)主進(jìn)程負(fù)責(zé)生成多個(gè)子進(jìn)程每個(gè)子進(jìn)程負(fù)責(zé)生成多個(gè)線程,主進(jìn)程負(fù)責(zé)創(chuàng)建套接字,接收請(qǐng)求,并將其派發(fā)給某子進(jìn)程進(jìn)行處理.每個(gè)線程負(fù)責(zé)響應(yīng)用戶請(qǐng)求.
并發(fā)響應(yīng)數(shù)量:m*n(m:子進(jìn)程數(shù)量,n:每個(gè)子進(jìn)程所能創(chuàng)建的最大線程數(shù)量)

worker的配置:
    <IfModule worker.c>
    StartServers         4
    MaxClients         300
    MinSpareThreads     25
    MaxSpareThreads     75
    ThreadsPerChild     25   每個(gè)子進(jìn)程所能創(chuàng)建的最大線程數(shù)量
    MaxRequestsPerChild  0   0表示不限制
    </IfModule> 

event:事件驅(qū)動(dòng)模型,多進(jìn)程模型,每個(gè)進(jìn)程響應(yīng)多個(gè)請(qǐng)求,一個(gè)主進(jìn)程 負(fù)責(zé)生成子進(jìn)程,創(chuàng)建套接字,接收請(qǐng)求,并將其派發(fā)給某子進(jìn)程進(jìn)行處理,每個(gè)子進(jìn)程基于事件驅(qū)動(dòng)機(jī)制直接響應(yīng)多個(gè)請(qǐng)求.
并發(fā)響應(yīng)數(shù)量:m*n(m:子進(jìn)程數(shù)量,n:每個(gè)子進(jìn)程響應(yīng)的請(qǐng)求數(shù)量)
在httpd2.2中未對(duì)event給出配置模版,并且只是測(cè)試功能,不建議使用.

在centos7系統(tǒng)中編譯安裝httpd2.2要先下載好依賴包(官方網(wǎng)站提示apr/arp-util版本要1.4(含)版本以上)和源碼包,下載過程不做表述可以做官網(wǎng)中自行下載,再安裝開發(fā)環(huán)境包組才能編譯安裝源碼包.
1.安裝依賴包:zlib-devel

[root@localhost ~]# yum install zlib-devel
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
base                                                                              | 3.6 kB  00:00:00     
epel                                                                              | 4.7 kB  00:00:00     
extras                                                                            | 3.4 kB  00:00:00     
.....

Total download size: 50 k
Installed size: 132 k
Is this ok [y/d/N]: y
Downloading packages:
zlib-devel-1.2.7-17.el7.x86_64.rpm                                                |  50 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : zlib-devel-1.2.7-17.el7.x86_64                                                        1/1 
  Verifying  : zlib-devel-1.2.7-17.el7.x86_64                                                        1/1 

Installed:
  zlib-devel.x86_64 0:1.2.7-17.el7 

2.安裝開發(fā)環(huán)境包組

[root@localhost ~]#  yum groupinstall "Development Tools" "ServerPlatform Development" -y
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.njupt.edu.cn

Installed:
  autoconf.noarch 0:2.69-11.el7                 automake.noarch 0:1.13.4-3.el7                          
  bison.x86_64 0:3.0.4-1.el7                    byacc.x86_64 0:1.9.20130304-3.el7                       
  cscope.x86_64 0:15.8-10.el7                   ctags.x86_64 0:5.8-13.el7                               
  diffstat.x86_64 0:1.57-4.el7                  doxygen.x86_64 1:1.8.5-3.el7                            
  flex.x86_64 0:2.5.37-3.el7                    gcc.x86_64 0:4.8.5-16.el7_4.2                           
  gcc-c++.x86_64 0:4.8.5-16.el7_4.2             gcc-gfortran.x86_64 0:4.8.5-16.el7_4.2                  
  git.x86_64 0:1.8.3.1-12.el7_4                 indent.x86_64 0:2.2.11-13.el7                           
  intltool.noarch 0:0.50.2-7.el7                libtool.x86_64 0:2.4.2-22.el7_3                         
  patch.x86_64 0:2.7.1-8.el7                    patchutils.x86_64 0:0.3.3-4.el7                         
  rcs.x86_64 0:5.9.0-5.el7                      redhat-rpm-config.noarch 0:9.1.0-76.el7.centos          
  rpm-build.x86_64 0:4.11.3-25.el7              rpm-sign.x86_64 0:4.11.3-25.el7                         
  subversion.x86_64 0:1.7.14-11.el7_4           swig.x86_64 0:2.0.10-5.el7                              
  systemtap.x86_64 0:3.1-5.el7_4               

Dependency Installed:
  cpp.x86_64 0:4.8.5-16.el7_4.2                     dwz.x86_64 0:0.11-3.el7                              
  efivar-libs.x86_64 0:31-4.el7                     gettext-common-devel.noarch 0:0.19.8.1-2.el7         
  gettext-devel.x86_64 0:0.19.8.1-2.el7             glibc-devel.x86_64 0:2.17-196.el7_4.2                
  glibc-headers.x86_64 0:2.17-196.el7_4.2           kernel-debug-devel.x86_64 0:3.10.0-693.21.1.el7      
  kernel-headers.x86_64 0:3.10.0-693.21.1.el7       libgfortran.x86_64 0:4.8.5-16.el7_4.2                
  libmpc.x86_64 0:1.0.1-3.el7                       libquadmath.x86_64 0:4.8.5-16.el7_4.2                
  libquadmath-devel.x86_64 0:4.8.5-16.el7_4.2       libstdc++-devel.x86_64 0:4.8.5-16.el7_4.2            
  m4.x86_64 0:1.4.16-10.el7                         mokutil.x86_64 0:12-1.el7.centos                     
  perl-Error.noarch 1:0.17020-2.el7                 perl-Git.noarch 0:1.8.3.1-12.el7_4                   
  perl-TermReadKey.x86_64 0:2.30-20.el7             perl-Test-Harness.noarch 0:3.28-3.el7                
  perl-Thread-Queue.noarch 0:3.02-2.el7             perl-XML-Parser.x86_64 0:2.41-10.el7                 
  perl-srpm-macros.noarch 0:1-8.el7                 subversion-libs.x86_64 0:1.7.14-11.el7_4             
  systemtap-client.x86_64 0:3.1-5.el7_4             systemtap-devel.x86_64 0:3.1-5.el7_4                 

Dependency Updated:
  glibc.x86_64 0:2.17-196.el7_4.2                   glibc-common.x86_64 0:2.17-196.el7_4.2              
  libgcc.x86_64 0:4.8.5-16.el7_4.2                  libgomp.x86_64 0:4.8.5-16.el7_4.2                   
  libstdc++.x86_64 0:4.8.5-16.el7_4.2               systemtap-runtime.x86_64 0:3.1-5.el7_4              

Complete!

3.下載源碼包及依賴包并解壓

[root@localhost Downloads]# ls
apr-1.5.0.tar.bz2  apr-util-1.5.3.tar.bz2  httpd-2.2.32.tar.gz

---------------分割線---------------

[root@localhost Downloads]# tar -xvf apr-1.5.0.tar.bz2 
apr-1.5.0/
apr-1.5.0/libapr.mak
apr-1.5.0/apr.mak
apr-1.5.0/apr.spec
apr-1.5.0/docs/
...
#依次用tar命令解壓apr-util-1.5.3.tar.bz2 和httpd-2.2.32.tar.gz 到當(dāng)前目錄即可

4.編譯安裝依賴包apr/apr-util

編譯安裝apr-1.5.0
[root@localhost Downloads]# cd apr-1.5.0

[root@localhost apr-1.5.0]# ./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/

[root@localhost apr-1.5.0]# make && make install
...
done
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr-httpd//build-1/apr_rules.mk
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr-httpd//bin/apr-1-config

---------------分割線---------------
編譯安裝apr-util-1.5.3
[root@localhost apr-1.5.0]# cd ..
[root@localhost Downloads]# cd apr-util-1.5.3/
[root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/
[root@localhost apr-util-1.5.3]# make && make install
...
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util-httpd//lib
/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util-httpd//bin/apu-1-config

5.編譯安裝httpd-2.2.32

[root@localhost apr-util-1.5.3]# cd ..
[root@localhost Downloads]# cd httpd-2.2.32
[root@localhost httpd-2.2.32]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd/ --enable-so --enable-deflate --enable-headers --enable-rewrite --with-mpm=prefork --with-apr=/usr/local/apr-httpd/ --with-apr-util=/usr/local/apr-util-httpd/
...
Installing build system files
mkdir /usr/local/apache/build
Installing man pages and online manual
mkdir /usr/local/apache/man
mkdir /usr/local/apache/man/man1
mkdir /usr/local/apache/man/man8
mkdir /usr/local/apache/manual
make[1]: Leaving directory `/root/Downloads/httpd-2.2.32'
一些參數(shù)的解析:
--prefix:安裝路徑

--sysconfdir:指定配置文件路徑

--enable-so:DSO兼容,DSO=Dynamic Shared Object,動(dòng)態(tài)共享對(duì)象,可實(shí)現(xiàn)模塊動(dòng)態(tài)生效

--enable-deflate:支持壓縮功能;需要zlib-devel包支持

--enable-headers:提供允許對(duì)HTTP請(qǐng)求頭的控制

--enable-rewrite:提供基于URL規(guī)則的重寫功能

--with-mpm:設(shè)置默認(rèn)啟用的MPM模式,{prefork|worker|event}

--with-apr:apr安裝路徑

--with-apr-util:apr-util安裝的路徑

6.在安裝目錄/usr/local/apache下檢查主配置文件語法錯(cuò)誤

[root@localhost apache]# bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
Syntax OK
#ok表示正常

7.啟動(dòng)httpd服務(wù)

[root@localhost apache]# /usr/local/apache/bin/httpd -k start
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName

#這個(gè)報(bào)錯(cuò)是主機(jī)名沒有設(shè)置好

8.查看80端口是否監(jiān)聽

[root@localhost apache]# ss -tnl
State       Recv-Q Send-Q       Local Address:Port                      Peer Address:Port              
LISTEN      0      128                      *:111                                  *:*                  
LISTEN      0      5            192.168.122.1:53                                   *:*                  
LISTEN      0      128                      *:22                                   *:*                  
LISTEN      0      128              127.0.0.1:631                                  *:*                  
LISTEN      0      100              127.0.0.1:25                                   *:*                  
LISTEN      0      128              127.0.0.1:6010                                 *:*                  
LISTEN      0      128                     :::111                                 :::*                  
LISTEN      0      128                     :::80                                  :::*                  
LISTEN      0      128                     :::22                                  :::*                  
LISTEN      0      128                    ::1:631                                 :::*                  
LISTEN      0      100                    ::1:25                                  :::*                  
LISTEN      0      128                    ::1:6010                                :::*   

9.關(guān)閉iptables和selinux之后測(cè)試是否安裝成功

[root@localhost apache]# iptables -F
[root@localhost apache]# setenforce 0
[root@localhost apache]# getenforce 
Permissive

10.輸入主機(jī)ip測(cè)試

http2.2安裝成功.png

出現(xiàn)這個(gè)頁面就證明安裝并啟動(dòng)成功了.

httpd2.2默認(rèn)是在prefork方式下工作的,要在幾個(gè)工作方式中切換,則需要修改/etc/sysconfig/httpd下對(duì)應(yīng)的配置.并重啟服務(wù)即可.

[root@localhost ~]# vim /etc/sysconfig/httpd 

# Configuration file for the httpd service.

#
# The default processing model (MPM) is the process-based
# 'prefork' model.  A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
#HTTPD=/usr/sbin/httpd.worker
HTTPD=/usr/sbin/httpd.event     #將該選項(xiàng)切換到對(duì)應(yīng)模式,重啟服務(wù)即可以

#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=

#
# By default, the httpd process is started in the C locale; to 
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
....
"/etc/sysconfig/httpd" 32L, 975C       

1.默認(rèn)的prefork方式:
修改/etc/sysconfig/httpd

[root@localhost ~]# vim /etc/sysconfig/httpd 

# Configuration file for the httpd service.

#
# The default processing model (MPM) is the process-based
# 'prefork' model.  A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
#HTTPD=/usr/sbin/httpd.worker
HTTPD=/usr/sbin/httpd

#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=

#
# By default, the httpd process is started in the C locale; to 
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
"/etc/sysconfig/httpd" 32L, 969C         

重啟服務(wù)

[root@localhost ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

查看httpd啟動(dòng)模式

[root@localhost ~]# ps aux | grep httpd
root      2365  0.0  0.2 184324  3864 ?        Ss   23:13   0:00 /usr/sbin/httpd
apache    2368  0.0  0.1 184324  2500 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2369  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2370  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2371  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2372  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2373  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2374  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2375  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
root      2377  0.0  0.0 103252   828 pts/1    S+   23:14   0:00 grep httpd

2.worker工作方式
修改etc/sysconfig/httpd文件中的HTTTPD=/usr/sbin/httpd.worker
重啟服務(wù)
驗(yàn)證worker工作方式

[root@localhost ~]# ps aux | grep httpd
root      2039  0.0  0.2 184532  4052 ?        Ss   22:56   0:00 /usr/sbin/httpd.worker
apache    2042  0.0  0.2 528792  5372 ?        Sl   22:56   0:00 /usr/sbin/httpd.worker
apache    2043  0.0  0.2 528792  5388 ?        Sl   22:56   0:00 /usr/sbin/httpd.worker
apache    2044  0.0  0.2 528792  5376 ?        Sl   22:56   0:00 /usr/sbin/httpd.worker
root      2157  0.0  0.1 143676  3816 pts/0    S+   22:57   0:00 vim /etc/sysconfig/httpd
root      2216  0.0  0.0 103252   828 pts/1    S+   23:07   0:00 grep httpd

3.event工作方式
修改etc/sysconfig/httpd文件中的HTTTPD=/usr/sbin/httpd.event
重啟服務(wù)
驗(yàn)證event工作方式

[root@localhost ~]# ps aux | grep httpd
root      2234  0.0  0.2 184540  4044 ?        Ss   23:07   0:00 /usr/sbin/httpd.event
apache    2237  0.0  0.2 528800  5356 ?        Sl   23:07   0:00 /usr/sbin/httpd.event
apache    2238  0.0  0.2 528800  5344 ?        Sl   23:07   0:00 /usr/sbin/httpd.event
apache    2239  0.0  0.2 528800  5344 ?        Sl   23:07   0:00 /usr/sbin/httpd.event
root      2322  0.0  0.0 103252   828 pts/1    S+   23:08   0:00 grep httpd

簡(jiǎn)述request報(bào)文請(qǐng)求方法和狀態(tài)響應(yīng)碼

一個(gè)http事務(wù)由請(qǐng)求(request) 和響應(yīng)(response)兩種狀態(tài),并在響應(yīng)中返回各種響應(yīng)碼,可以從不同的狀態(tài)碼中了解不同的響應(yīng)狀態(tài).

報(bào)文語法格式:
    request報(bào)文
        <method> <request-URL> <version>
        <headers>

        <entity-body>

    response報(bào)文
        <version> <status> <reason-phrase>
        <headers>

        <entity-body>

method: 請(qǐng)求方法
標(biāo)明客戶端希望服務(wù)器對(duì)資源執(zhí)行的動(dòng)作,如下為常用method的解釋:
GET:從服務(wù)器獲取一個(gè)資源
HEAD:只從服務(wù)器獲取文檔的響應(yīng)首部
POST:向服務(wù)器發(fā)送要處理的數(shù)據(jù)
PUT:將請(qǐng)求的主體部分存儲(chǔ)在服務(wù)器上
DELETE:請(qǐng)求刪除服務(wù)器上指定的文檔
TRACE:追蹤請(qǐng)求到達(dá)服務(wù)器中間經(jīng)過的代理服務(wù)器
OPTIONS:請(qǐng)求服務(wù)器返回對(duì)指定資源支持使用的請(qǐng)求方法

version:HTTPD的版本號(hào)
HTTP/<major>.<minor>,由主版本和次版本號(hào)組成

status:狀態(tài)碼
由三位數(shù)字組成,如200,301, 302, 404, 502; 標(biāo)記請(qǐng)求處理過程中發(fā)生的情況,不同的狀態(tài)碼表示的意思不一樣.
1xx:100-101, 信息提示
2xx:200-206, 成功
3xx:300-305, 重定向
4xx:400-415, 錯(cuò)誤類信息,客戶端錯(cuò)誤
5xx:500-505, 錯(cuò)誤類信息,服務(wù)器端錯(cuò)誤

常用的狀態(tài)碼:
    200: 成功,請(qǐng)求的所有數(shù)據(jù)通過響應(yīng)報(bào)文的entity-body部分發(fā)送;OK
    301: 請(qǐng)求的URL指向的資源已經(jīng)被刪除;但在響應(yīng)報(bào)文中通過首部Location指明了資源現(xiàn)在所處的新位置;Moved Permanently
    302: 與301相似,但在響應(yīng)報(bào)文中通過Location指明資源現(xiàn)在所處臨時(shí)新位置; Found
    304: 客戶端發(fā)出了條件式請(qǐng)求,但服務(wù)器上的資源未曾發(fā)生改變,則通過響應(yīng)此響應(yīng)狀態(tài)碼通知客戶端;Not Modified
    401: 需要輸入賬號(hào)和密碼認(rèn)證方能訪問資源;Unauthorized
    403: 請(qǐng)求被禁止;Forbidden
    404: 服務(wù)器無法找到客戶端請(qǐng)求的資源;Not Found
    500: 服務(wù)器內(nèi)部錯(cuò)誤;Internal Server Error
    502: 代理服務(wù)器從后端服務(wù)器收到了一條偽響應(yīng);Bad Gateway

reason-phrase:狀態(tài)碼所標(biāo)記的狀態(tài)的簡(jiǎn)要描述

headers:首部
每個(gè)請(qǐng)求或響應(yīng)報(bào)文可包含任意個(gè)首部;每個(gè)首部都有首部名稱,后面跟一個(gè)冒號(hào),而后跟上一個(gè)可選空格,接著是一個(gè)值
首部的分類:通用首部,請(qǐng)求首部,響應(yīng)首部,實(shí)體首部,擴(kuò)展首部等等,
格式為:Name: Value,常見的首部格式如下:

通用首部:
Date: 報(bào)文的創(chuàng)建時(shí)間
Connection:連接狀態(tài),如keep-alive, close
Via:顯示報(bào)文經(jīng)過的中間節(jié)點(diǎn)
Cache-Control:控制緩存
Pragma: no-cache.相當(dāng)于Cache-Control: no-cache

請(qǐng)求首部:
Accept:通過服務(wù)器自己可接受的媒體類型;
Accept-Charset:瀏覽器指明自己接收的字符集
Accept-Encoding:接受編碼格式,如gzip
Accept-Language:接受的語言
Client-IP: 客戶端ip地址
Host: 請(qǐng)求的服務(wù)器名稱和端口號(hào)
Referer:包含當(dāng)前正在請(qǐng)求的資源的上一級(jí)資源;
User-Agent:客戶端代理

條件式請(qǐng)求首部:
Expect:服務(wù)器指明該實(shí)體什么時(shí)候過期.
If-Modified-Since:自從指定的時(shí)間之后,請(qǐng)求的資源是否發(fā)生過修改
If-Unmodified-Since:自從指定的時(shí)間之后,請(qǐng)求的資源是否沒有修改
If-None-Match:本地緩存中存儲(chǔ)的文檔的ETag標(biāo)簽是否與服務(wù)器文檔的
If-Match:Etag沒改變,才執(zhí)行請(qǐng)求

安全請(qǐng)求首部:
Authorization:向服務(wù)器發(fā)送認(rèn)證信息,如賬號(hào)和密碼
Cookie: 客戶端向服務(wù)器發(fā)送cookie

代理請(qǐng)求首部:
Proxy-Authorization: 向代理服務(wù)器認(rèn)證

響應(yīng)首部:
1.信息性:
Age:響應(yīng)持續(xù)時(shí)長(zhǎng)
Server:服務(wù)器程序軟件名稱和版本
2.協(xié)商首部:某資源有多種表示方法時(shí)使用
Accept-Ranges:服務(wù)器可接受的請(qǐng)求范圍類型
Vary:服務(wù)器查看的其它首部列表;

安全響應(yīng)首部:
Set-Cookie:向客戶端設(shè)置cookie
Set-Cookie2: 同上,版本不一樣
WWW-Authenticate:來自服務(wù)器的對(duì)客戶端的質(zhì)詢認(rèn)證表單

實(shí)體首部:
Allow: 列出對(duì)此實(shí)體可使用的請(qǐng)求方法
Location:告訴客戶端真正的實(shí)體位于何處
Content-Length: 主體的長(zhǎng)度
Content-Location: 實(shí)體真正所處位置
Content-Type:主體的對(duì)象類型

緩存相關(guān):
ETag:實(shí)體的擴(kuò)展標(biāo)簽
Expires:實(shí)體的過期時(shí)間
Last-Modified:最后一次修改的時(shí)間

entity-body:請(qǐng)求時(shí)附加的數(shù)據(jù)或響應(yīng)時(shí)附加的數(shù)據(jù)

[root@localhost ~]# curl -I www.baidu.com   #-I:只顯示響應(yīng)報(bào)文首部信息
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Sun, 22 Apr 2018 15:25:13 GMT
Content-Type: text/html
Content-Length: 277
Last-Modified: Mon, 13 Jun 2016 02:50:23 GMT
Connection: Keep-Alive
ETag: "575e1f6f-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytes

詳細(xì)描述httpd虛擬主機(jī)、站點(diǎn)訪問控制、基于用戶的訪問控制、持久鏈接等應(yīng)用配置實(shí)例

在httpd2.2中的相關(guān)配置文件路徑如下:

    配置文件:
        /etc/httpd/conf/httpd.conf
        /etc/httpd/conf.d/*.conf

    服務(wù)腳本:
        /etc/rc.d/init.d/httpd
        腳本配置文件:/etc/sysconfig/httpd

    主程序文件:
        /usr/sbin/httpd
        /usr/sbin/httpd.event
        /usr/sbin/httpd.worker

    日志文件:
        /var/log/httpd:
                access_log:訪問日志
                error_log:錯(cuò)誤日志
    站點(diǎn)文檔:
        /var/www/html

    模塊文件路徑:
        /usr/lib64/httpd/modules
                
    服務(wù)控制和啟動(dòng):
        chkconfig  httpd  on|off
        service  {start|stop|restart|status|configtest|reload}  httpd

1.虛擬主機(jī):一個(gè)物理機(jī)可以服務(wù)多個(gè)站點(diǎn),沒一個(gè)站點(diǎn)可以通過一個(gè)或多個(gè)虛擬主機(jī)來實(shí)現(xiàn),httpd有三種模式:
基于IP:通過IP地址訪問不同主機(jī),為每個(gè)虛擬主機(jī)準(zhǔn)備至少一個(gè)ip地址
基于Port:通過Port訪問不同主機(jī),為每個(gè)虛擬主機(jī)準(zhǔn)備至少一個(gè)Port
基于FQDN主機(jī)名:通過主機(jī)名訪問不同主機(jī),為每個(gè)虛擬主機(jī)準(zhǔn)備至少一個(gè)主機(jī)名
在httpd2.2中開啟虛擬主機(jī)需要先關(guān)閉中心主機(jī)(Main server),在配置文件中注釋掉DocumentRoot指令即可.

虛擬主機(jī)的配置方法:
    <VirtualHost  IP:PORT>
        ServerName FQDN
        DocumentRoot  ""
    </VirtualHost>

這三種模式可以一起混用,可以在/etc/httpd/conf/httpd.conf主配置文件中寫入配置信息也可以在/etc/httpd/conf.d/virtualhost.conf模塊化的配置文件路徑下單獨(dú)寫入配置.

  • 基于ip的虛擬機(jī)要保證一個(gè)主機(jī)中有多個(gè)ip地址.
  • 基于port的虛擬機(jī)要在配置文件中指明要監(jiān)聽的端口.
  • 基于主機(jī)名的虛擬機(jī)要在host文件或DNS服務(wù)器中,支配好主機(jī)域名,保證不同的域名都訪問相同的ip地址.
    配置文件格式如下:
[root@localhost ~]# vim /etc/httpd/conf.d/virtualhost.conf

listen 8008   #指明要監(jiān)聽的端口,
<VirtualHost 192.168.1.107:80>   #基于IP的訪問控制,保證主機(jī)有此ip
        ServerName "www.a.com"
        DocumentRoot "/var/www/a"   #這個(gè)目錄為服務(wù)的根目錄要?jiǎng)?chuàng)建好
</VirtualHost>

<VirtualHost 192.168.1.102:80>  #基于IP的訪問控制,保證主機(jī)有此ip
        ServerName "www.b.com"
        DocumentRoot "/var/www/b"
</VirtualHost>

<VirtualHost 192.168.1.107:8008>  #基于Port的訪問控制,要先listen port
        ServerName "www.c.com"
        DocumentRoot "/var/www/c"
</VirtualHost>

<VirtualHost 192.168.1.107:80>  ##基于主機(jī)名的訪問控制
        ServerName "www.d.com"  #這個(gè)域名要能解析,可以寫入host配置文件或DNS解析文件中
        DocumentRoot "/var/www/d"
</VirtualHost>

<VirtualHost 192.168.1.107:80>
        ServerName "www.f.com"  #同上
        DocumentRoot "/var/www/f"
</VirtualHost>

<VirtualHost 192.168.1.105:8008>  #基于IP和Port混用的訪問控制
        ServerName "www.g.com"  
        DocumentRoot "/var/www/g"
</VirtualHost>

2.站點(diǎn)訪問控制:文件系統(tǒng)路徑和URL路徑兩種機(jī)制明對(duì)哪些資源進(jìn)行訪問控制,可以來源的ip地址來控制哪些地址可以訪問資源哪些ip地址不能訪問.httpd2.2的配置格式如下:

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny  #order:生效次序.指明只有allow的地址可以訪問,其他的都deny
    Allow from all   #指明允許的范圍,匹配是先匹配小范圍在匹配打范圍
</Directory>
#在配置文件/etc/httpd/conf/httpd.conf中加入如下配置并保存退出重啟服務(wù)
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from 192.168.1.106
</Directory>

---------------分割線---------------

[root@localhost ~]# service httpd restart   #重啟服務(wù)
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

---------------分割線---------------

#在非192.168.1.106的主機(jī)上測(cè)試
[root@localhost ~]# curl http://192.168.1.107/cgi-bin/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>   #顯示禁止了,說明訪問受限了
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /cgi-bin/
on this server.</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at 192.168.1.107 Port 80</address>
</body></html>

3.基于用戶的訪問控制:對(duì)于網(wǎng)站中的一些資源,需要只對(duì)特定的用戶開放,可以通過用戶的訪問控制來實(shí)現(xiàn).有質(zhì)詢和認(rèn)證兩種方式.
質(zhì)詢:服務(wù)器用401響應(yīng)碼拒絕客戶端請(qǐng)求,并說明要求客戶端提供賬號(hào)和密碼
認(rèn)證:客戶端用戶填入賬號(hào)和密碼后再次發(fā)送請(qǐng)求報(bào)文,認(rèn)證通過時(shí),則服務(wù)器發(fā)送響應(yīng)的資源.有basic基本認(rèn)證和digest摘要認(rèn)證兩種.這里以basic認(rèn)證做實(shí)驗(yàn).
basic認(rèn)證的配置格式如下:

<Directory "/somepath/to/file">  # 需要訪問控制的文件路徑
    Options None
    AllowOverride None
    AuthType Basic   # 認(rèn)證方式
    AuthName "String“   #認(rèn)證提示信息
    AuthUserFile  "/PATH/TO/HTTPD_USER_PASSWD_FILE"   #認(rèn)證用戶的文件路徑
    Require  user  username1  username2 ...   #允許訪問的用戶
</Directory>
#  Require  valid-user  表示允許所有AuthUserFile 文件中所以的賬號(hào)
#  AuthGroupFile "/PATH/TO/HTTPD_GROUP_FILE"  基于用戶組做訪問控制
#  Require  group  grpname1  grpname2 ... 同上

登陸使用的賬號(hào)為虛擬賬號(hào)而非系統(tǒng)的賬號(hào),而且httpd是明文傳輸?shù)?所以使用htpasswd命令來創(chuàng)建賬號(hào)文件格式如下:

htpasswd  [options]   /PATH/TO/HTTPD_PASSWD_FILE  username 
        -c:自動(dòng)創(chuàng)建此處指定的文件,因此,僅應(yīng)該在此文件不存在時(shí)使用
        -m:md5格式加密
        -s: sha格式加密
        -D:刪除指定用戶
[root@localhost ~]# mkdir /etc/httpd/users
[root@localhost ~]# htpasswd -c -m /etc/httpd/users/.htpasswd tom
New password: 
Re-type new password: 
Adding password for user tom
[root@localhost ~]# htpasswd -m /etc/httpd/users/.htpasswd jeck
New password: 
Re-type new password: 
Adding password for user jeck
[root@localhost ~]# cat /etc/httpd/users/.htpasswd 
jeck:$apr1$FnsoQ9yU$cjHvdY0VtLKNmpRvLrXG00
tom:$apr1$Z0pZGKXv$HYvkDBq5tW.RkPUQcqj9x1

創(chuàng)建要控制的文件路徑及文件并在/etc/httpd/conf.d/admin.conf文件中添加配置

[root@localhost ~]# mkdir /var/www/html/date
[root@localhost ~]# vim /var/www/html/date/index.html

admin page
~            

[root@localhost ~]# vim /etc/httpd/conf.d/admin.conf

<Directory "var/www/html/date">
        Options None
        AllowOverride None
        AuthType Basic
        AuthName "please enter the user"
        AuthUserFile "/etc/http/users/.htpasswd"  #htpasswd命令保存的文件路徑
        Require user tom  #允許的用戶,在htpasswd文件中必須存在,
</Directory>

保存退出之后,檢查語法錯(cuò)誤并重啟服務(wù)

[root@localhost ~]# httpd -t
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
Syntax OK
[root@localhost ~]# !serv
service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

最后測(cè)試一下


用戶認(rèn)證登陸.png

出現(xiàn)認(rèn)證登陸界面說明配置成功了.

4.持久鏈接:服務(wù)器連續(xù)建立后,每個(gè)資源獲取完成后不全斷開連接,而是繼續(xù)等待其它資源請(qǐng)求的進(jìn)行,通過連接次數(shù)或等待時(shí)間的限制來斷開連接.可以在/etc/httpd/conf.d/創(chuàng)建xxx.conf來配置,相關(guān)參數(shù)如下:

KeepAlive  On|Off  開啟或關(guān)閉保持連接
KeepAliveTimeout  15   等待超時(shí)時(shí)間(超過時(shí)間就關(guān)閉)
MaxKeepAliveRequests  100  (最大允許請(qǐng)求數(shù)量,和Timeout兩者誰先滿足條件就關(guān)閉)

可以使用telnet命令進(jìn)行測(cè)試,格式如下

telnet  WEB_SERVER_IP  PORT   #服務(wù)器的ip換端口
GET  /URL  HTTP/1.1 #/url  httpd的版本號(hào)
Host: WEB_SERVER_IP
[root@localhost ~]# vim /etc/httpd/conf.d/keepalive.conf

keepalive  On
KeepAliveTimeout  15   
MaxKeepAliveRequests  100  
#報(bào)錯(cuò)退出后可以用httpd -t 檢查語法,并重啟服務(wù)
--------------分割線--------------

[root@localhost ~]# telnet 192.168.1.107 80
Trying 192.168.1.107...
Connected to 192.168.1.107.
Escape character is '^]'.
GET /index.html HTTP/1.1
Host: 192.168.1.107

HTTP/1.1 200 OK
Date: Sun, 22 Apr 2018 17:18:21 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sun, 22 Apr 2018 17:15:44 GMT
ETag: "425a1-10-56a73124ff4ad"
Accept-Ranges: bytes
Content-Length: 16
Content-Type: text/html; charset=UTF-8

KeeepAlive page
#這里連接之后不會(huì)立即斷開,會(huì)等到最大連接數(shù)或連接時(shí)長(zhǎng)到達(dá)才會(huì)斷開.

5.路徑別名:將一個(gè)長(zhǎng)格式的路徑名稱使用 Alias去定義一個(gè)短路徑,在訪問的上鉆就可以不用寫那么長(zhǎng)的路徑了.具體格式如下

Alias /icons/ "/var/www/icons/"  #將/var/www/icons/定義成/icons/別名,注意格式正確

<Directory "/var/www/icons"> #定義這個(gè)路徑允許的操作
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • http協(xié)議及Apache服務(wù) http協(xié)議 什么是http? http全稱為超文件傳輸協(xié)議(Hyper text...
    魏鎮(zhèn)坪閱讀 2,436評(píng)論 0 1
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評(píng)論 19 139
  • httpd相關(guān): httpd程序版本: 。1.3 停止維護(hù) 。2.0 。 2.2 event為測(cè)試使用 。 2.4...
    ckhzw閱讀 498評(píng)論 0 0
  • 1、第八章 Samba服務(wù)器2、第八章 NFS服務(wù)器3、第十章 Linux下DNS服務(wù)器配站點(diǎn),域名解析概念命令:...
    哈熝少主閱讀 3,906評(píng)論 0 10
  • 小貝潔閱讀 285評(píng)論 0 0

友情鏈接更多精彩內(nèi)容