1. 程式人生 > >++++++++++HTTP基本特性及配置

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

博客 linux 運維

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









本文出自 “Reading” 博客,請務必保留此出處http://sonlich.blog.51cto.com/12825953/1968229

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