一、httpd 安裝組成
http 服務基于 C/S 結構
1?.常見http 服務器程序
-
httpd apache,存在C10K(10K connections)問題
-
nginx 解決C10K問題lighttpd
-
IIS .asp 應用程序服務器
-
tomcat .jsp 應用程序服務器
-
jetty 開源的servlet容器,基于Java的web容器
-
Resin CAUCHO公司,支持servlets和jsp的引擎
-
webshpere:IBM公司
-
weblogic:BEA,Oracle
-
jboss:RedHat,IBM
-
oc4j:Oracle
2.apache介紹和特點
apache 名字來源,流傳最廣的解釋是(也是最顯而易見的):這個名字來自于一個事實:當Apache在1995年初開發的時候,它是由當時最流行的HTTP服務器NCSA HTTPd 1.3的代碼修改而成的,因此是"一個修補的(a patchy)”服務器。
HTTP 和 Apache 之間的關系是:HTTP定義了客戶端和服務器之間的通信規則,
而 Apache 是一種能夠處理這些 HTTP 請求并提供網頁內容的 Web 服務器軟件。
apache 功能:
-
提供http協議服務
-
多個虛擬主機:IP、Port、FQDN? ?用一臺 物理服務器搭建多個網站? ? 百度? jd? 淘寶
-
CGI:Common Gateway Interface,通用網關接口,支持動態程序
-
反向代理
-
負載均衡
-
路徑別名
-
豐富的用戶認證機制:basic,digest
-
支持第三方模塊
apache特性:
-
高度模塊化:core + modules
-
DSO:Dynamic Shared Object 動態加載/卸載
-
MPM:multi-processing module 多路處理模塊
apache 功能多,穩定,處理靜態資源優秀
MPM multi-processing module 工作模式
prefork:多進程I/O模型,每個進程響應一個請求,CentOS 7 httpd默認模型一個主進程:生成和回收n個子進程,創建套接字,不響應請求多個子進程:工作 work進程,每個子進程處理一個請求;系統初始時,預先生成多個空閑進程,等待請求
Prefork MPM預派生模式,有一個主控制進程,然后生成多個子進程,每個子進程有一個獨立的線程響應用戶請求,相對比較占用內存,但是比較穩定,可以設置最大和最小進程數,是最古老的一種模式,也是最穩定的模式,適用于訪問量不是很大的場景
優點:穩定
缺點:慢,占用資源,不適用于高并發場景
?[root@centos1 ~]#rpm -q httpd
未安裝軟件包 httpd
[root@centos1 ~]#yum install httpd -y
[root@centos1 ~]#systemctl start httpd
[root@centos1 ~]#?
如果是yum安裝,默認是prefork模型
worker:復用的多進程I/O模型,多進程多線程,IIS使用此模型
一個主進程:生成m個子進程,每個子進程負責生個n個線程,每個線程響應一個請求,并發響應請求:m*n
worker MPM是一種多進程和多線程混合的模型,有一個控制進程,啟動多個子進程,每個子進程里面包含固定的線程,使用線程程來處理請求,當線程不夠使用的時候會再啟動一個新的子進程,然后在進程里面再啟動線程處理請求,由于其使用了線程處理請求,因此可以承受更高的并發。
優點:相比prefork 占用的內存較少,可以同時處理更多的請求
缺點:使用keep-alive的長連接方式,某個線程會一直被占據,即使沒有傳輸數據,也需要一直等待到超時才會被釋放。如果過多的線程,被這樣占據,也會導致在高并發場景下的無服務線程可用。(該問題在prefork模式下,同樣會發生)
event:事件驅動模型(worker模型的變種),CentOS8 默認模型
event MPM是Apache中最新的模式,2012年發布的apache 2.4.X系列正式支持event 模型. 屬于事件驅動模型(epoll),每個進程響應多個請求,在現在版本里的已經是穩定可用的模式。
優點:單線程響應多請求,占據更少的內存,高并發下表現更優秀,會有一個專門的線程來管理keep-alive類型的線程,當有真實請求過來的時候,將請求傳遞給服務線程,執行完畢后,又允許它釋放
缺點:沒有線程安全控制
centos 版本不一樣,可能默認工作模式不一樣,centos7 默認是prefork模式
3.Httpd 安裝和相關文件
3.1 包安裝httpd并啟動httpd服務
版本說明:
CentOS 7 以上,默認系統是httpd 2.4,CentOS 6 版默認為httpd 2.2
Ubuntu 18.04 默認 Apache/2.4.29
安裝方式:
-
包安裝: centos發行版,穩定,建議使用
-
編譯:定制或特殊需求
?
?[root@centos1 ~]#rpm -q httpd
未安裝軟件包 httpd
[root@centos1 ~]#yum install httpd -y
[root@centos1 ~]#systemctl start httpd
[root@centos1 ~]#
3.2 httpd-2.4 相關文件
配置文件:
-
/etc/httpd/conf/httpd.conf 主配置文件
-
/etc/httpd/conf.d/*.conf 子配置文件
-
/etc/httpd/conf.d/conf.modules.d/ 模塊加載的配置文件
檢查配置語法:httpd -t 或 apache2 -t
服務單元文件:
-
/usr/lib/systemd/system/httpd.service
-
配置文件:/etc/sysconfig/httpd
服務控制和啟動
-
systemctl enable|disable httpd.service
-
systemctl {start|stop|restart|status|reload} httpd.service
-
apachectl start|stop|restart|configtest
-
service httpd start|stop|restart|configtest
站點網頁文檔根目錄:/var/www/html
模塊文件路徑:
-
/etc/httpd/modules
-
/usr/lib64/httpd/modules
主服務器程序文件:/usr/sbin/httpd
3.3 CentOS 7 編譯安裝httpd 2.4
編譯說明和準備
APR:Apache portable Run-time libraries,Apache可移植運行庫,主要為上層的應用程序提供一個可以跨越多操作系統平臺使用的底層支持接口庫。在早期的Apache版本中,應用程序本身必須能夠處理各種具體操作系統平臺的細節,并針對不同的平臺調用不同的處理函數隨著Apache的進一步開發,Apache組織決定將這些通用的函數獨立出來并發展成為一個新的項目。這樣,APR的開發就從Apache中獨立出來,Apache僅僅是使用 APR而已。目前APR主要還是由Apache使用,由于APR的較好的移植性,因此一些需要進行移植的C程序也開始使用APR,開源項目:比如用于服務器壓力測試的Flood loader tester
Apache安裝
? Apache即阿帕奇是一款開源的、世界使用排名第一的Web服務器軟件,其特點是簡單高效、穩定安全所以被廣泛應用于計算機技術的各個領域,但現在由于其抗并發性問題現在新公司大部分都使用Nginx代替。
2、Yum安裝
①yum安裝與其他程序一樣可以直接使用命令:yum install ?httpd ?-y。
②安裝過程中注意查看提示信息,若無外網則需要配置本地yum源進行安裝。
③出現以下提示即表示安裝成功。注意:若出現error字樣則表示安裝出錯!!!
?[root@centos1 ~]#rpm -q httpd
未安裝軟件包 httpd
[root@centos1 ~]#yum install httpd -y
[root@centos1 ~]#systemctl start httpd
[root@centos1 ~]#
④?yum安裝默認的主配置文件位置:?/etc/httpd/conf/httpd.conf
⑤ yum安裝默認的主頁面配置文件夾位置:?/var/www/html/
⑥ yum安裝默認的日志文件位置:/var/log/httpd/access_log? 此為正常日志記錄,/var/log/httpd/error此為錯誤日志記錄。
二、httpd常見配置
1.指定服務器名
[root@centos1 ~]#cd /etc/httpd/conf/
[root@centos1 conf]#ls
httpd.conf magic
[root@centos1 conf]#cp httpd.conf httpd.conf.bak
[root@centos1 conf]#ls
httpd.conf httpd.conf.bak magic
[root@centos1 conf]#httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::183e:c32:9272:8ece. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@centos1 conf]#vim /etc/httpd/conf/httpd.conf95 ServerName www.example.com:80
[root@centos1 conf]#httpd -t
Syntax OK
2.包含其它配置文件
指令:
Include file-path|directory-path|wildcard
IncludeOptional file-path|directory-path|wildcard
說明:
-
Include和IncludeOptional功能相同,都可以包括其它配置文件
-
但是當無匹配文件時,include會報錯,IncludeOptional會忽略錯誤
include 子配置文件
[root@node2 ~]#grep -i include /etc/httpd/conf/httpd.conf
Include conf.modules.d/*.conf# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
# Possible values include: debug, info, notice, warn, error, crit,# If you include a trailing / on /webpath then the server will# To parse .shtml files for server-side includes (SSI):# (You will also need to add "Includes" to the "Options" directive.)AddOutputFilter INCLUDES .shtml
IncludeOptional conf.d/*.conf
總目錄
[root@node2 httpd]#grep -i serverroot /etc/httpd/conf/httpd.conf
# with "/", the value of ServerRoot is prepended -- so 'log/access_log'
# with ServerRoot set to '/www' will be interpreted by the
# ServerRoot: The top of the directory tree under which the server's
# ServerRoot at a non-local disk, be sure to specify a local disk on the
# same ServerRoot for multiple httpd daemons, you will need to change at
ServerRoot "/etc/httpd"
3 .監聽地址
Listen [IP:]PORT
說明:
(1) 省略IP表示為本機所有IP
(2) Listen指令至少一個,可重復出現多次
[root@centos1 conf]#vim /etc/httpd/conf/httpd.conf
#Listen 80
Listen 192.168.246.7:80
Listen 192.168.246.7:9527
[root@centos1 conf]#systemctl restart httpd
[root@centos1 conf]#httpd -t
Syntax OK
[root@centos1 conf]#
實驗1:指明具體地址
驗證:
實驗2:
驗證
/etc/httpd/conf/httpd.conf 一些基礎配置
4.隱藏服務器版本信息
再去訪問
5.持久連接
Persistent Connection:連接建立,每個資源獲取完成后不會斷開連接,而是繼續等待其它的請求完成,默認開啟持久連接
斷開條件:
-
時間限制:以秒為單位, 默認5s,httpd-2.4 支持毫秒級
-
請求數量: 請求數達到指定值,也會斷開
副作用:對并發訪問量大的服務器,持久連接會使有些請求得不到響應
折衷:使用較短的持久連接時間
/etc/httpd/conf.d/*.conf 子配置文件? ?
下圖test是自定義? .conf結尾就可以
?
KeepAlive On|Off # 開啟或關閉長連接
KeepAliveTimeout 15 ? ? ?#連接持續15s,可以以ms為單位,默認值為5s
MaxKeepAliveRequests 500 ?#持久連接最大接收的請求數,默認值100
進入7-2
安裝
測試方法:
6.DSO (Dynamic Shared Object)
Dynamic Shared Object,加載動態模塊配置,不需重啟即生效動態模塊所在路徑: /usr/lib64/httpd/modules/
主配置 /etc/httpd/conf/httpd.conf 文件中指定加載模塊配置文件
查看靜態編譯的模塊:httpd -l
查看靜態編譯及動態裝載的模塊:httpd -M
[root@centos1 html]#httpd -M|grep basicauth_basic_module (shared)
[root@centos1 html]#
[root@centos1 html]#
[root@centos1 html]#
[root@centos1 html]#pwd
/var/www/html
[root@centos1 html]#cd /etc/httpd/
[root@centos1 httpd]#ls
conf conf.d conf.modules.d logs modules run
[root@centos1 httpd]#cd conf.modules.d/
[root@centos1 conf.modules.d]#ls
00-base.conf 00-dav.conf 00-lua.conf 00-mpm.conf 00-proxy.conf 00-systemd.conf 01-cgi.conf
[root@centos1 conf.modules.d]#vim 00-base.conf 1 #2 # This file loads most of the modules included with the Apache HTTP3 # Server itself.4 #5 6 LoadModule access_compat_module modules/mod_access_compat.so7 LoadModule actions_module modules/mod_actions.so8 LoadModule alias_module modules/mod_alias.so9 LoadModule allowmethods_module modules/mod_allowmethods.so10 #LoadModule auth_basic_module modules/mod_auth_basic.so11 LoadModule auth_digest_module modules/mod_auth_digest.so12 LoadModule authn_anon_module modules/mod_authn_anon.so13 LoadModule authn_core_module modules/mod_authn_core.so14 LoadModule authn_dbd_module modules/mod_authn_dbd.so15 LoadModule authn_dbm_module modules/mod_authn_dbm.so16 LoadModule authn_file_module modules/mod_authn_file.so17 LoadModule authn_socache_module modules/mod_authn_socache.so18 LoadModule authz_core_module modules/mod_authz_core.so19 LoadModule authz_dbd_module modules/mod_authz_dbd.so20 LoadModule authz_dbm_module modules/mod_authz_dbm.so21 LoadModule authz_groupfile_module modules/mod_authz_groupfile.so22 LoadModule authz_host_module modules/mod_authz_host.so23 LoadModule authz_owner_module modules/mod_authz_owner.so24 LoadModule authz_user_module modules/mod_authz_user.so25 LoadModule autoindex_module modules/mod_autoindex.so26 LoadModule cache_module modules/mod_cache.so27 LoadModule cache_disk_module modules/mod_cache_disk.so28 LoadModule data_module modules/mod_data.so29 LoadModule dbd_module modules/mod_dbd.so30 LoadModule deflate_module modules/mod_deflate.so
"00-base.conf" 77L, 3740C 已寫入
[root@centos1 conf.modules.d]#systemctl restart httpd
[root@centos1 conf.modules.d]#httpd -M|grep basic
[root@centos1 conf.modules.d]#
7.MPM (Multi-Processing Module)多路處理模塊
httpd 支持三種MPM工作模式:prefork, worker, event
[root@centos7 ~]#vim /etc/httpd/conf.modules.d/00-mpm.conf
[root@centos7 ~]#grep Load /etc/httpd/conf.modules.d/00-mpm.conf
# one of the following LoadModule lines. See the httpd.conf(5) man
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
#LoadModule mpm_event_module modules/mod_mpm_event.so
[root@centos7 ~]#httpd -M | grep mpm
AH00558: httpd: Could not reliably determine the server's fully qualified domain
name, using centos8.localdomain. Set the 'ServerName' directive globally to
suppress this messagempm_prefork_module (shared)
8.?prefork模式相關的配置
StartServers ? ? ? 100
MinSpareServers ? 50
MaxSpareServers ? 80
ServerLimit ? ? 2560 #最多進程數,最大值 20000
MaxRequestWorkers ? ?2560 #最大的并發連接數,默認256
MaxConnectionsPerChild ?4000 #子進程最多能處理的請求數量。在處理MaxRequestsPerChild 個
請求之后,子進程將會被父進程終止,這時候子進程占用的內存就會釋放(為0時永遠不釋放)
MaxRequestsPerChild 4000 ?#從 httpd.2.3.9開始被MaxConnectionsPerChild代替
[root@centos1 conf.modules.d]#vim 00-mpm.conf
# Select the MPM module which should be used by uncommenting exactly
# one of the following LoadModule lines:# prefork MPM: Implements a non-threaded, pre-forking web server
# See: http://httpd.apache.org/docs/2.4/mod/prefork.html
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so# worker MPM: Multi-Processing Module implementing a hybrid
# multi-threaded multi-process web server
# See: http://httpd.apache.org/docs/2.4/mod/worker.html
#
#LoadModule mpm_worker_module modules/mod_mpm_worker.so# event MPM: A variant of the worker MPM with the goal of consuming
# threads only for connections with active processing
# See: http://httpd.apache.org/docs/2.4/mod/event.html
#
#LoadModule mpm_event_module modules/mod_mpm_event.soStartServers 10
[root@centos1 conf.modules.d]#systemctl restart httpd
[root@centos1 conf.modules.d]#pstree -p|grep httpd|-httpd(7485)-+-httpd(7486)| |-httpd(7487)| |-httpd(7488)| |-httpd(7489)| |-httpd(7490)| |-httpd(7491)| |-httpd(7493)| |-httpd(7495)| |-httpd(7496)| `-httpd(7497)
[root@centos1 conf.modules.d]#
9.worker和event 模式相關的配置
ServerLimit ? ? ? ? 16 ?#最多worker進程數 Upper limit on configurable number of
processes
StartServers ? ? ? ?10 ?#Number of child server processes created at startup
MaxRequestWorkers ?150 ?#Maximum number of connections that will be processed
simultaneously
MinSpareThreads ? ? 25
MaxSpareThreads ? ? 75
ThreadsPerChild ? ? 25 ?#Number of threads created by each child process
10.定義Main server的文檔頁面路徑
DocumentRoot ? "/path”
<directory /path>Require all granted
</directory>
-
DocumentRoot指向的路徑為URL路徑的起始位置
-
/path 必須顯式授權后才可以訪問
[root@centos1 html]#vim /etc/httpd/conf/httpd.conf
120 DocumentRoot "/data/html"
121
122 <Directory "/data/html">
123 # Allow open access:
124 Require all granted
125 </Directory>
126 #
127 # Relax access to content within /var/www.
[root@centos1 html]#mkdir /data
[root@centos1 html]#mkdir /data/html
[root@centos1 html]#cd /data/html
[root@centos1 html]#ls
[root@centos1 html]#echo data data > index.html
[root@centos1 html]#ls
index.html
[root@centos1 html]#pwd
/data/html
[root@centos1 html]#cat /data/html/index.html
data data
[root@centos1 html]#httpd -t
Syntax OK
[root@centos1 html]#systemctl restart httpd
[root@centos1 html]#
去檢測:
別名 alias
[root@centos1 opt]#vim /etc/httpd/conf.d/test.conf
KeepAlive On
KeepAliveTimeout 1000
MaxKeepAliveRequests 1<Directory "/opt/blog"># Allow open access:Require all granted
</Directory>alias /test /opt/blog/
"/etc/httpd/conf.d/test.conf" 12L, 177C 已寫入
[root@centos1 opt]#systemctl restart httpd
[root@centos1 opt]#
11. 定義站點默認主頁面文件
當我們訪問服務器時 省略了最后的文件,默認自動會加上 index.html這個是可以修改的
DirectoryIndex index.php index.html
?
[root@centos1 ~]#grep -n index /etc/httpd/conf/httpd.conf
169: DirectoryIndex index.html
[root@centos1 ~]#
去瀏覽器訪問
針對目錄和URL實現訪問控制
Options 指令
后跟1個或多個以空白字符分隔的選項列表,在選項前的+,-表示增加或刪除指定選項
Options 可以寫在目錄里 < > 也可以寫在外面
接下來:
[root@centos1 ~]#vim /etc/httpd/conf.d/test.conf
KeepAlive On
KeepAliveTimeout 1000
MaxKeepAliveRequests 1<Directory "/opt/blog"># Allow open access:Require all grantedoptions Indexes
</Directory>alias /test /opt/blog/
[root@centos1 ~]#systemctl restart httpd
再去訪問不支持
[root@centos1 ~]#vim /etc/httpd/conf.d/test.conf
<Directory "/opt/blog"># Allow open access:Require all grantedoptions Indexes FollowSymLinks
</Directory>alias /test /opt/blog/
[root@centos1 ~]#systemctl restart httpd
再去訪問
12.虛擬主機
httpd 支持在一臺物理主機上實現多個網站,即多虛擬主機
網站的唯一標識:
-
IP相同,但端口不同
-
IP不同,但端口均為默認端口
-
FQDN不同, IP和端口都相同
多虛擬主機有三種實現方案:
-
基于ip:為每個虛擬主機準備至少一個ip地址
-
基于port:為每個虛擬主機使用至少一個獨立的port
-
基于FQDN:為每個虛擬主機使用至少一個FQDN,請求報文中首部 Host:www.kgc.com
理解:
基于ip地址
192.168.246.7 ---------> jd
192.168.246.8---------> taobao
基于端口
192.168.246.7:80 ---------> jd
192.168.246.7:800--------> taobao
基于域名
www.lucky.com ?--------> lucky
www.cloud.com ?---------> cloud
?虛擬主機的三種實現方式:基于IP、基于端口、基于域名; 最常用的是? ?基于域名
[root@centos1 ~]#cd /usr/share/doc/httpd-
httpd-2.4.6/ httpd-tools-2.4.6/
[root@centos1 ~]#cd /usr/share/doc/httpd-2.4.6/
[root@centos1 httpd-2.4.6]#lsOUT_APACHE httpd-default.conf httpd-manual.conf httpd-vhosts.conf proxy-html.conf
?HANGES httpd-info.conf httpd-mpm.conf LICENSE README
httpd-dav.conf httpd-languages.conf httpd-multilang-errordoc.conf NOTICE VERSIONING
[root@centos1 httpd-2.4.6]#vim httpd-vhosts.conf
12.1 基于ip地址
[root@centos1 html]#vim /etc/httpd/conf.d/test.conf
KeepAlive On
KeepAliveTimeout 1000
MaxKeepAliveRequests 1<Directory "/opt/html">AllowOverride None# Allow open access:Require all grantedoptions Indexes FollowSymLinks
</Directory><VirtualHost 192.168.246.7>ServerAdmin support@jfedu.netDocumentRoot "/opt/html/7"ServerName www.accp.comErrorLog "logs/7_error_log"CustomLog "logs/7_access_log" common
</VirtualHost><VirtualHost 192.168.246.111>ServerAdmin support@jfedu.netDocumentRoot "/opt/html/111"ServerName www.accp.comErrorLog "logs/111_error_log"CustomLog "logs/111_access_log" common
</VirtualHost>
[root@centos1 opt]#cd /opt
[root@centos1 opt]#mkdir html
mkdir: 無法創建目錄"html": 文件已存在
[root@centos1 opt]#mkdir html/{7,111}
[root@centos1 opt]#tree
bash: tree: 未找到命令...
[root@centos1 opt]#ls
blog html test.exe test.zz
[root@centos1 opt]#cd html/
[root@centos1 html]#ls
111 7 {7.111}
[root@centos1 html]#echo 7 > 7/index.html
[root@centos1 html]#echo 111 > 111/index.html
[root@centos1 html]#cat 7/index.html
7
[root@centos1 html]#cat 111/index.html
111
[root@centos1 html]#httpd -t
Syntax OK
[root@centos1 html]#systemctl restart httpd
[root@centos1 html]#ifconfig ens33:0 192.168.246.111/24
檢測:
12.2 基于端口地址
[root@centos1 html]#vim /etc/httpd/conf.d/test.conf
Listen 9527
KeepAlive On
KeepAliveTimeout 1000
MaxKeepAliveRequests 1<Directory "/opt/html">AllowOverride None# Allow open access:Require all grantedoptions Indexes FollowSymLinks
</Directory><VirtualHost 192.168.246.7:80>ServerAdmin support@jfedu.netDocumentRoot "/opt/html/7"ServerName www.accp.comErrorLog "logs/7_error_log"CustomLog "logs/7_access_log" common
</VirtualHost><VirtualHost 192.168.246.7:9527>ServerAdmin support@jfedu.netDocumentRoot "/opt/html/111"ServerName www.accp.comErrorLog "logs/111_error_log"CustomLog "logs/111_access_log" common
</VirtualHost>
[root@centos1 html]#systemctl start httpd
檢測:
12.3 基于域名
[root@centos1 html]#vim /etc/httpd/conf.d/test.conf
Listen 9527
KeepAlive On
KeepAliveTimeout 1000
MaxKeepAliveRequests 1<Directory "/opt/html">AllowOverride None# Allow open access:Require all grantedoptions Indexes FollowSymLinks
</Directory><VirtualHost 192.168.246.7>ServerAdmin support@jfedu.netDocumentRoot "/opt/html/7"ServerName www.kgc.com
# ErrorLog "logs/7_error_log"
# CustomLog "logs/7_access_log" common
</VirtualHost><VirtualHost 192.168.246.7>ServerAdmin support@jfedu.netDocumentRoot "/opt/html/111"ServerName www.zzz.com
# ErrorLog "logs/111_error_log"
# CustomLog "logs/111_access_log" common
</VirtualHost>
[root@centos1 html]#systemctl start httpd
[root@centos1 html]#systemctl restart httpd
[root@centos1 html]#
驗證:
13.基于客戶端?IP 地址實現訪問控制
黑名單,不能有失敗,至少有一個成功匹配才成功,即失敗優先
<RequireAll>
RequireAll all granted
RequireAll not ip 172.16.1.1 #拒絕特定IP
</RequireAll>
白名單,多個語句有一個成功,則成功,即成功優先
<RequireAny>
RequireAny all denied
require ip 172.16.1.1 #允許特定IP
</RequireAny>
實驗:
[root@centos1 html]#vim /etc/httpd/conf.d/test.conf
<directory /mnt>
<RequireAll>Require all grantedRequire not ip 192.168.246.1
</RequireAll>
</directory>alias /mnt /mnt/html
[root@centos1 html]#mkdir /mnt/html
[root@centos1 html]#echo /mnt/html > /mnt/html/index.html
[root@centos1 html]#cat /mnt/html/index.html
/mnt/html
[root@centos1 html]#systemctl restart httpd
驗證:
去虛擬機訪問:
去真機網頁驗證
14.日志
三、Web相關工具
1.Wget相關工具
格式:
wget [OPTION]... [URL]...
常用選項:
-q 靜默模式
-c 斷點續傳
-P /path 保存在指定目錄
-O filename 保存為指定文件名,filename 為 - 時,發送至標準輸出
--limit-rate= 指定傳輸速率,單位K,M等
例子:
[root@centos7 ~]#wget --limit-rate 1M -P /data https://mirrors.aliyun.com/centos/8/isos/x86_64/CentOS-8-x86_64-1905-dvd1.iso
2.curl? ? 文字版瀏覽器
curl是基于URL語法在命令行方式下工作的文件傳輸工具,它支持FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE及LDAP等協議。curl支持HTTPS認證,并且支持HTTP的POST、PUT等方法, FTP上傳, kerberos認證,HTTP上傳,代理服務器,cookies,用戶名/密碼認證, 下載文件斷點續傳,上載文件斷點續傳, http代理服務器管道( proxy tunneling),還支持IPv6,socks5代理服務器,通過http代理服務器上傳文件到FTP服務器等,功能十分強大.
格式:
curl [options] [URL...]
常用選項
-A/--user-agent <string> 設置用戶代理發送給服務器
-e/--referer <URL> 來源網址
--cacert <file> CA證書 (SSL)
-k/--insecure ? 允許忽略證書進行 SSL 連接
--compressed 要求返回是壓縮的格式
-H/--header "key:value” 自定義首部字段傳遞給服務器
-i 顯示頁面內容,包括報文首部信息
-I/--head 只顯示響應報文首部信息
-D/--dump-header <file>將url的header信息存放在指定文件中
--basic 使用HTTP基本認證
-u/--user <user[:password]>設置服務器的用戶和密碼
-L ? 如果有3xx響應碼,重新發請求到新位置
-O 使用URL中默認的文件名保存文件到本地
-o <file> 將網絡文件保存為指定的文件中
--limit-rate <rate> 設置傳輸速度
-0/--http1.0 數字0,使用HTTP 1.0
-v/--verbose 更詳細
-C 選項可對文件使用斷點續傳功能
-c/--cookie-jar <file name> 將url中cookie存放在指定文件中
-x/--proxy <proxyhost[:port]> 指定代理服務器地址
-X/--request <command> 向服務器發送指定請求方法
-U/--proxy-user <user:password> 代理服務器用戶和密碼
-T 選項可將指定的本地文件上傳到FTP服務器上
--data/-d 方式指定使用POST方式傳遞數據
-s --silent ? Silent mode
-b name=data 從服務器響應set-cookie得到值,返回給服務器
-w <format> 顯示相應的指定的報文信息,如:%{http_code},%{remote_ip}等
-m, --max-time <time> 允許最大傳輸時間
-I/--head 只顯示響應報文首部信息
[root@centos7 ~]#curl -I http://www.163.com
[root@localhost ~]#curl www.163.com -vA chrome
-L ??如果有3xx響應碼,重新發請求到新位置
-v/--verbose 更詳細
補充:
提取狀態碼
[root@centos1 ~]#curl -s -I -m10 -o /dev/null ? -w %{http_code} http://www.baidu.com/
提取遠端ip
[root@centos1 ~]#curl -s -I -m10 -o /dev/null ? -w %{remote_ip} http://www.baidu.com/
提取本地ip (自己)
[root@centos1 ~]#curl -s -I -m10 -o /dev/null ? -w %{local_ip} http://www.baidu.com/
[root@centos1 ~]#curl -s -I -m10 -o /dev/null ? -w %{local_port} http://www.baidu.com/
[root@centos1 ~]#curl -s -I -m10 -o /dev/null ? -w %{remote_port} http://www.baidu.com/
匯總:
curl -s -I -m10 -o /dev/null ? -w %{http_code} http://www.baidu.com/
curl -s -I -m10 -o /dev/null ? -w %{remote_ip} http://www.baidu.com/
curl -s -I -m10 -o /dev/null ? -w %{local_ip} http://www.baidu.com/
curl -s -I -m10 -o /dev/null ? -w %{local_port} http://www.baidu.com/
curl -s -I -m10 -o /dev/null ? -w %{remote_port} http://www.baidu.com/
3.壓力測試工具
httpd的壓力測試工具:
-
ab, webbench, http_load, seige
-
Jmeter 開源
-
Loadrunner 商業,有相關認證
-
tcpcopy:網易,復制生產環境中的真實請求,并將之保存
ab 來自httpd-tools包
命令格式:
ab [OPTIONS] URL
選項:
-n:總請求數
-c:模擬的并發數
-k:以持久連接模式測試