author:JevonWei
版權聲明:原創做品javascript
檢查配置文件時,以下提示,則由於沒有server的服務名稱致使,故設置網站的服務server名稱,若沒有設置web服務名,主默認解析系統主機名(添加主機名解析)css
[root@danran html]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using danran.com. Set the 'ServerName' directive globally to suppress this message
Syntax OKhtml
在/etc/httpd/conf/httpd.conf設置servername名稱
vim /etc/httpd/conf/httpd.conf
#ServerName www.example.com:80java
或 在主機名解析文件中,添加主機名解析
[root@danran html]# cat /etc/hosts
127.0.0.1 danran.commysql
顯示服務器版本信息web
ServerTokens Major|Minor|Min[imal]|Prod[uctOnly]|OS|Full \顯示web服務的頭部信息
ServerTokens Prod[uctOnly]:Server: Apache
ServerTokens Major: Server: Apache/2
ServerTokens Minor: Server: Apache/2.0
ServerTokens Min[imal]: Server: Apache/2.0.41
ServerTokens OS: Server: Apache/2.0.41 (Unix)
ServerTokens Full (or not specified): Server: Apache/2.0.41(Unix) PHP/4.2.2 MyMod/1.2
This setting applies to the entire server and cannot be enabled or disabled on a virtualhost-by-virtualhost basis.
After version 2.0.44, this directive also controls the information presented by the
ServerSignature directive.正則表達式
建議使用:ServerTokens Prodsql
示例
[root@danran mysql]# curl -I 192.168.198.134
HTTP/1.1 200 OK
Date: Mon, 31 Jul 2017 04:30:38 GMT
Server: Apache/2.4.6 (CentOS) \顯示web服務的版本信息
Last-Modified: Mon, 31 Jul 2017 03:02:48 GMT
ETag: "b-55594440a26a5"
Accept-Ranges: bytes
Content-Length: 11
Content-Type: text/html; charset=UTF-8數據庫
[root@danran conf]# vim /etc/httpd/conf/httpd.conf ServerTokens prod [root@danran conf]# service httpd restart [root@danran conf]# curl -I 192.168.198.136 HTTP/1.1 403 Forbidden Date: Sun, 02 Jul 2017 10:49:03 GMT Server: Apache Accept-Ranges: bytes Content-Length: 4961 Connection: close Content-Type: text/html; charset=UTF-8
修改監聽的IP和Portapache
Listen [IP:]PORT (1) 省略IP表示爲0.0.0.0; (2) Listen指令可重複出現屢次持久鏈接
KeepAlive On|Off \容許/禁止持久鏈接
KeepAliveTimeout 15 \最長等待鏈接時間
MaxKeepAliveRequests 100 \最大鏈接請求次數
MPM(Multi-Processing Module)多路處理模塊
prefork, worker, event(試驗階段)
httpd-2.2不支持同時編譯多個模塊,因此只能編譯時選定一個;rpm安裝的包提供三個二進制程序文件,分別用於實現對不一樣MPM機制的支持
確認方法:
ps aux | grep httpd
默認爲/usr/sbin/httpd,即prefork 模式
[root@danran mysql]# vim /etc/sysconfig/httpd
HTTPD=/usr/sbin/httpd.worker
查看靜態編譯的模塊
httpd.work -l
查看靜態編譯及動態裝載的模塊
httpd.work –M
prefork的默認配置:
<IfModule prefork.c>
StartServers 8 \開啓啓動開啓的進程
MinSpareServers 5 \最小的空閒進程個數
MaxSpareServers 20 \最大的空閒進程數
ServerLimit 256 \最多進程數, 最大20000
MaxClients 256 \最大併發
MaxRequestsPerChild 4000 \子進程最多能處理的請求數量。在處理MaxRequestsPerChild個請求以後, 子進程將會被父進程終止,這時候子進程佔用的內存就會釋放(爲0時永遠不釋放)
worker的默認配置: <IfModule worker.c> StartServers 4 \\開啓自啓進程數爲4 MaxClients 300 \\最大併發數 MinSpareThreads 25 \\最小空閒線程個數 MaxSpareThreads 75 \\最大空閒線程 ThreadsPerChild 25 \\每一個進程開啓的線程數 MaxRequestsPerChild 0 \\無限制 </IfModule>
DSO:Dynamic Shared Object
加載動態模塊配置
定義 'Main' server的文檔頁面路徑
DocumentRoot 「/path」 文檔路徑映射: DocumentRoot 指向的路徑爲URL路徑的起始位置 示例: DocumentRoot "/app/data「 http://HOST:PORT/test/index.html --> /app/data/test/index.html 注意:SELinux和iptables的狀態
定義默認站點主頁面
DirectoryIndex index.html index.html.var \當web目錄下沒有主頁文件index.html時,默認顯示頁面文件爲index.html.var頁面,存放路徑爲/var/www/error/noindex.html,經過文件/etc/httpd/conf.d/welcome.conf文件配置
站點訪問控制常見機制
可基於兩種機制指明對哪些資源進行何種訪問控制訪問控制機制有兩種:客戶端來源地址,用戶帳號
文件系統路徑:
<Directory 「/path">
...
<File 「/path/file」>
...
<FileMatch "PATTERN">
...
URL路徑: <Location ""> ... </Location> <LocationMatch ""> ... </LocationMatch> 示例: <FilesMatch "\.(gif|jpe?g|png)$"> <Files 「?at.*」> 通配符 <Location /status> <LocationMatch "/(extra|special)/data">
(1) Options:後跟1個或多個以空白字符分隔的選項列表
在選項前的+,-表示增長或刪除指定選項
常見選項:
Indexes:指明的URL路徑下不存在與定義的主頁面資源相符的資源文件時,返回索引列表給用戶,index默認關閉
FollowSymLinks:容許訪問符號連接文件所指向的源文件,FollowSymLinks默認開啓
None:所有禁用
All:所有容許
注意:index選項默認關閉,FollowSymLinks默認開啓
示例: <Directory "/web/docs"> Options Indexes FollowSymLinks </Directory> <Directory /web/docs/spec> \\子目錄的設置不受父目錄的影響,可單獨設置獨立生效 Options FollowSymLinks </Directory> <Directory /web/docs> Options Indexes FollowSymLinks </Directory> <Directory /web/docs/spec> Options +Includes -Indexes </Directory>
(2) AllowOverride
與訪問控制相關的哪些指令能夠放在指定目錄下的.htaccess(由AccessFileName 指定)文件中,覆蓋以前的配置指令
只對
AllowOverride All:全部指令都有效
AllowOverride None:.htaccess 文件無效
AllowOverride AuthConfig Indexes 除了AuthConfig和Indexes的其它指令都沒法覆蓋
示例:
vim /etc/httpd/conf/httpd.conf
AccessFileName .htaccess
DocumentRoot "/var/www/html" <Directory /> AllowOverride All </Directory> [root@danran error]# vim /var/www/html/.htaccess option indexes followSymlinks \\"/var/www/html"目錄的訪問權限以此設置生效
(3) order和allow、deny
order:定義生效次序;寫在後面的表示默認法則
Order allow,deny
Order deny,allow
Allow from, Deny from
來源地址:
IP
網絡:
172.16
172.16.0.0
172.16.0.0/16
172.16.0.0/255.255.0.0
示例: <files "*.txt"> order deny,allow deny from 172.16. 100.100 allow from 172.16 </files> <files "*.txt"> order allow,deny deny from 172.16.100.100 allow from 172.16 </files> DocumentRoot "/app/web" <Directory /> order allow,deny \\deny覆蓋allow,最後的生效 deny from 192.168.198.134 \\拒絕134的訪問 allow from 192.168.198.134 \\運行134訪問 </Directory>
日誌設定
日誌類型:
訪問日誌
錯誤日誌
錯誤日誌:
ErrorLog logs/error_log
LogLevel warn
loglevel 可選值:
debug,info,notice,warn,error crit,alert,emerg
日誌記錄等級:(/etc/httpd/conf/httpd.conf)
LogLevel warn
設定默認字符集
AddDefaultCharset UTF-8
中文字符集:GBK,GB2312, GB18030
定義路徑別名
格式:Alias /URL/ "/PATH/"
DocumentRoot "/www/htdocs"
http://www.magedu.com/download/bash.rpm ==>/www/htdocs/download/bash.rpm
Alias /download/ "/rpms/pub/" http://www.magedu.com/download/bash.rpm==>/rpms/pub/bash.rpm http://www.magedu.com/images/logo.png==>/www/htdocs/images/logo.png
示例:
alias "/download" "/app/download/" \/app/download/原路徑必須有/,若/app/download則不能正確訪問
正則表達式匹配 AliasMatch ^/danran/(.*)$ /app/danran$1 \\經過訪問danran目錄下的文件則將對應/app/danran$1對應的文件
基於用戶的訪問控制
認證質詢:WWW-Authenticate:響應碼爲401,拒絕客戶端請求,並說明要求客戶端提供帳號和密碼
認證:Authorization:客戶端用戶填入帳號和密碼後再次發送請求報文;認證經過時,則服務器發送響應的資源
認證方式兩種:
basic:明文
digest:消息摘要認證,兼容性差
安全域:須要用戶認證後方能訪問的路徑;應該經過名稱對其進行標識,以便於告知用戶認證的緣由
用戶的帳號和密碼
虛擬帳號:僅用於訪問某服務時用到的認證標識
存儲:文本文件,SQL數據庫,ldap目錄存儲,nis等
basic認證配置示例:
(1) 定義安全域
<Directory 「/path">
Options None
AllowOverride None \啓用.htaccess文件編輯配置信息
AuthType Basic \驗證方式
AuthName "String「 \驗證信息描述
AuthUserFile "/PATH/HTTPD_USER_PASSWD_FILE" \認證文件路徑
Require user username1 username2 ... \容許文件的指定用戶訪問
Require valid-user \容許文件中全部用戶都可訪問
容許帳號文件中的全部用戶登陸訪問: Require valid-user (2) 提供帳號和密碼存儲(文本文件) 使用專用命令完成此類文件的建立及用戶管理 htpasswd [options] /PATH/HTTPD_PASSWD_FILE username -c:自動建立文件,僅應該在文件不存在時使用 -m:md5格式加密 -s:sha格式加密 -D:刪除指定用戶
基於組帳號進行認證
(1) 定義安全域
<Directory 「/path">
AuthType Basic
AuthName "String「
AuthUserFile "/PATH/HTTPD_USER_PASSWD_FILE"
AuthGroupFile "/PATH/HTTPD_GROUP_FILE"
Require group grpname1 grpname2 ...
(2) 建立用戶帳號和組帳號文件;
組文件:每一行定義一個組
GRP_NAME: username1 username2 ...
示例 (基於用戶和組帳號認證):
htpasswd -c /etc/httpd/conf.d/.htuser httpuser1 \建立http服務的帳號及密碼,默認加密類型爲md5加密,初次建立http用戶需使用-c新建/etc/httpd/conf.d/.htuser用戶帳號密碼的存儲文件,/etc/httpd/conf.d/.htuser存儲文件路徑,可自定義其餘路徑
htpasswd -s /etc/httpd/conf.d/.htuser httpuser2 \-s指定加密類型爲sha
echo '<h1>/app/web/admin/</h1>' > /app/web/admin/index.html 新建/app/web/admin目錄,指定訪問此目錄時需使用帳號登陸 setfacl -m u:apache:r /etc/httpd/conf.d/.htuser \\受權apache讀文件的權限 1. 基於/etc/httpd/conf/httpd.conf配置文件驗證 vim /etc/httpd/conf/httpd.conf DocumentRoot "/app/web/" <Directory /app/web/admin> authtype basic authname "admin directory" authuserfile "/vim /etc/httpd/conf.d/.htuser" require user httpuser1 httpuser2 Require valid-user \\容許文件中全部都可訪問 </Directory> 2. 基於.htaccess文件驗證,文件可存放在web訪問路徑下 vim /etc/httpd/conf/httpd.conf DocumentRoot "/app/web/" <Directory /app/web/admin> allowoverride authconfig </Directory> vim /app/web/admin/.htaccess authtype basic authname "admin directory" authuserfile "/etc/httpd/conf.d/.htuser" Require valid-user 3. 基於組文件用戶驗證 vim /etc/httpd/conf.d/.htgroup g1:httpuser1 httpuser2 g2:httpuser3 vim /app/web/admin/.htaccess authuserfile "etc/httpd/conf.d/.htuser" authtype basic authname "admin directory" authuserfile "/etc/httpd/conf.d/.htuser" authgroupfile "etc/httpd/conf.d/.htgroup" Require group g1 \\容許g1訪問
遠程客戶端和用戶驗證的控制
Satisfy ALL|Any
ALL客戶機IP 和用戶驗證都須要經過才能夠
Any客戶機IP和用戶驗證, 有一個知足便可
示例:
Require valid-user
Order allow,deny
Allow from 192.168.1
Satisfy Any
虛擬主機
站點標識:socket
IP相同,但端口不一樣
IP不一樣,但端口均爲默認端口
FQDN不一樣;
請求報文中首部
Host: www.danran.com
有三種實現方案:
基於ip:爲每一個虛擬主機準備至少一個ip地址
基於port:爲每一個虛擬主機使用至少一個獨立的port
基於FQDN:爲每一個虛擬主機使用至少一個FQDN
注意:通常虛擬機不要與main 主機混用;所以,要使用虛擬主機,通常先禁用main主機
禁用方法:註釋中心主機DocumentRoot指令便可
虛擬主機的配置方法:
ServerName FQDN
DocumentRoot 「/path"
建議:上述配置存放在獨立的配置文件中
其它可用指令: ServerAlias:虛擬主機的別名;可屢次使用 ErrorLog:錯誤日誌 CustomLog:訪問日誌 <Directory 「/path"> </Directory> Alias
基於IP的虛擬主機示例:
<VirtualHost 172.16.100.6:80>
ServerName www.a.com
DocumentRoot "/www/a.com/htdocs"
<VirtualHost 172.16.100.7:80> ServerName www.b.net DocumentRoot "/www/b.net/htdocs" </VirtualHost> <VirtualHost 172.16.100.8:80> ServerName www.c.org DocumentRoot "/www/c.org/htdocs" </VirtualHost>
基於端口的虛擬主機:可和基於IP的虛擬主機混和使用
listen 808
listen 8080
<VirtualHost 172.16.100.6:80>
ServerName www.a.com
DocumentRoot "/www/a.com/htdocs"
<VirtualHost 172.16.100.6:808> ServerName www.b.net DocumentRoot "/www/b.net/htdocs" </VirtualHost> <VirtualHost 172.16.100.6:8080> ServerName www.c.org DocumentRoot "/www/c.org/htdocs" </VirtualHost>
基於FQDN的虛擬主機:
NameVirtualHost :80 httpd2.4 不須要此指令
<VirtualHost :80>
ServerName www.a.com
DocumentRoot "/www/a.com/htdocs"
<VirtualHost *:80> ServerName www.b.net DocumentRoot "/www/b.net/htdocs" </VirtualHost> <VirtualHost *:80> ServerName www.c.org DocumentRoot "/www/c.org/htdocs" </VirtualHost>
示例(三個基於IP的虛擬主機):
新建三個目錄/app/web一、/app/web二、/app/web3用來用虛擬主機的根目錄
mkdir /app/web1/ /app/web2/ /app/web3/
echo 'welcome /app/web1/danran' > /app/web1/index.html
echo 'welcome /app/web2/danran' > /app/web2/index.html
echo 'welcome /app/web3/danran' > /app/web3/index.html
setenfore 0
iptables -F
新建vhosts配置文件,用於編輯虛擬主機的配置信息(也可在/etc/httpd/conf/httpd.conf文件中直接修改) vim /etc/httpd/conf.d/vhosts.conf \\(複製/etc/httpd/conf/httpd.conf文件中的最後幾行虛擬主機的配置) <VirtualHost 192.168.198.136:80> \\指定虛擬主機的IP和端口 ServerAdmin danran.com \\指定web主機的郵件地址 DocumentRoot /app/web1 \\虛擬主機的根目錄 ServerName www.web1.com \\對應web服務的全稱域名 ErrorLog logs/web1_error_log \\虛擬主機web服務的錯誤日誌記錄 CustomLog logs/web1_access_log common \\\\虛擬主機web服務的訪問日誌記錄 </VirtualHost> <VirtualHost 192.168.198.137:80> \\指定虛擬主機的IP和端口 DocumentRoot /app/web2 \\虛擬主機的根目錄 ServerName www.web2.com ErrorLog logs/web2_error_log CustomLog logs/web2_access_log common </VirtualHost> <VirtualHost 192.168.198.138:80> DocumentRoot /app/web3 ServerName www.web3.com ErrorLog logs/web3_error_log CustomLog logs/web3_access_log common </VirtualHost>
示例(基於端口的虛擬主機)
新建三個目錄/app/web一、/app/web二、/app/web3用來用虛擬主機的根目錄
mkdir /app/web1/ /app/web2/ /app/web3/
echo 'welcome /app/web1/danran' > /app/web1/index.html
echo 'welcome /app/web2/danran' > /app/web2/index.html
echo 'welcome /app/web3/danran' > /app/web3/index.html
setenfore 0
iptables -F
新建vhosts配置文件,用於編輯虛擬主機的配置信息(也可在/etc/httpd/conf/httpd.conf文件中直接修改) vim /etc/httpd/conf.d/vhosts.conf \\(複製/etc/httpd/conf/httpd.conf文件中的最後幾行虛擬主機的配置) Listen 192.168.198.136:8001 \\監聽192.168.198.136主機的8001端口 Listen 192.168.198.136:8002 \\\\監聽192.168.198.136主機的8002端口 Listen 192.168.198.136:8003 \\\\監聽192.168.198.136主機的8003端口 <VirtualHost 192.168.198.136:8001> DocumentRoot /app/web1 ServerName www.web1.com ErrorLog logs/web1_error_log CustomLog logs/web1_access_log common </VirtualHost> <VirtualHost 192.168.198.136:8002> DocumentRoot /app/web2 ServerName www.web2.com ErrorLog logs/web2_error_log CustomLog logs/web2_access_log common </VirtualHost> <VirtualHost 192.168.198.136:8003> DocumentRoot /app/web3 ServerName www.web3.com ErrorLog logs/web3_error_log CustomLog logs/web3_access_log common </VirtualHost> 訪問測試 192.168.198.136:8001 192.168.198.136:8002 www.web1.com(需配置域名DNS解析實現)
示例(基於FQDN名稱的虛擬主機)
新建三個目錄/app/web一、/app/web二、/app/web3用來用虛擬主機的根目錄
mkdir /app/web1/ /app/web2/ /app/web3/
echo 'welcome /app/web1/danran' > /app/web1/index.html
echo 'welcome /app/web2/danran' > /app/web2/index.html
echo 'welcome /app/web3/danran' > /app/web3/index.html
setenfore 0
iptables -F
新建vhosts配置文件,用於編輯虛擬主機的配置信息(也可在/etc/httpd/conf/httpd.conf文件中直接修改) vim /etc/httpd/conf.d/vhosts.conf \\(複製/etc/httpd/conf/httpd.conf文件中的最後幾行虛擬主機的配置 # Use name-based virtual hosting. NameVirtualHost *:80 \\基於虛擬主機的名稱,對應全部主機的IP的80端口 <VirtualHost *:80> DocumentRoot /app/web1 ServerName www.web1.com ErrorLog logs/web1_error_log CustomLog logs/web1_access_log common </VirtualHost> <VirtualHost *:80> DocumentRoot /app/web2 ServerName www.web2.com ErrorLog logs/web2_error_log CustomLog logs/web2_access_log common </VirtualHost> <VirtualHost *:80> DocumentRoot /app/web3 ServerName www.web3.com ErrorLog logs/web3_error_log CustomLog logs/web3_access_log common </VirtualHost> 客戶端測試: vim /etc/hosts \\編輯主機名解析,一般利用DNS來解析 192.168.198.136 www.web1.com www.web2.com www.web3.com curl www.web1.com curl www.web2.com curl www.web3.com 注意:定義了基於主機名稱的虛擬主機,則其餘主機失效,若訪問其餘的web主機,則默認訪問第一個web的虛擬主機
status頁面
LoadModule status_module modules/mod_status.so
vim /etc/httpd/conf.d/status.conf
ExtendedStatus On \\顯示擴展信息 <Location /server-status> SetHandler server-status \\系統內部指令 Order deny,allow \\設置權限順序 Deny from all Allow from 172.16 \\除了172.16容許其餘都拒絕 </Location>
ExtendedStatus On 顯示擴展信息
訪問:
192.168.198.136/server-status
結果以下 Apache Server Status for 192.168.198.136 Server Version: Apache/2.2.15 (Unix) DAV/2 SVN/1.6.11 Server Built: Mar 22 2017 06:52:55 Current Time: Tuesday, 04-Jul-2017 20:52:00 CST Restart Time: Tuesday, 04-Jul-2017 20:52:00 CST Parent Server Generation: 0 Server uptime: 2 requests currently being processed, 0 idle workers WWSSSSSS........................................................ ................................................................ ................................................................ ................................................................ Scoreboard Key: "_" Waiting for Connection, "S" Starting up, "R" Reading Request, "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup, "C" Closing connection, "L" Logging, "G" Gracefully finishing, "I" Idle cleanup of worker, "." Open slot with no current process PID Key: 6426 in state: W , 6427 in state: W , 6428 in state: S 6429 in state: S , 6430 in state: S , 6431 in state: S 6432 in state: S , 6433 in state: S , To obtain a full report with current status information you need to use the ExtendedStatus On directive. Apache Server at 192.168.198.136 Port 80
使用mod_deflate模塊壓縮頁面優化傳輸速度
適用場景:
(1) 節約帶寬,額外消耗CPU;同時,可能有些較老瀏覽器不支持
(2) 壓縮適於壓縮的資源,例如文本文件
LoadModule deflate_module modules/mod_deflate.so \\指定加載模塊 SetOutputFilter DEFLATE \\開始默認壓縮 # Restrict compression to these MIME types DeflateCompressionLevel 9 \\壓縮比 AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/css # Level of compression (Highest 9 - Lowest 1) DeflateCompressionLevel 9 \\默認壓縮比 排除特定舊版本的瀏覽器,不支持壓縮 Netscape 4.x has some problems. BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0[678] no-gzip InternetMatch Explorer標識自己爲「Mozilla / 4」,但其實是可以處理請求的壓縮。若是用戶代理首部匹配字符串「MSIE」(「B」爲單詞邊界),就關閉以前定義的限制 # MSIE masquerades as Netscape, but it is fine BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
示例(數據壓縮後傳輸):
vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
DocumentRoot /app/web2
ServerName www.web2.com
ErrorLog logs/web2_error_log
CustomLog logs/web2_access_log common
LoadModule deflate_module modules/mod_deflate.so
SetOutputFilter DEFLATE DeflateCompressionLevel 9 # Restrict compression to these MIME types AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/css 客戶端訪問 curl --compressed -I www.web2.com
http重定向https
將http請求轉發至https的URL
重定向
Redirect [status] URL-path URL
status狀態:
Permanent:Returns a permanent redirect status (301) indicating that the resource has moved permanently
Temp:Returns a temporary redirect status (302). This is the default
示例:
vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80> DocumentRoot /app/web3 ServerName www.web3.com ErrorLog logs/web3_error_log CustomLog logs/web3_access_log common Redirect temp / https://www.web1/com </VirtualHost> 訪問測試: http://www.web3/com \\將會自動跳轉到https://web1.com
HSTS
HSTS:HTTP Strict Transport Security
服務器端配置支持HSTS後,會在給瀏覽器返回的HTTP首部中攜帶HSTS字段。瀏覽器獲取到該信息後,會將全部HTTP訪問請求在內部作307跳轉到HTTPS。而無需任何網絡過程
HSTS preload list 是Chrome瀏覽器中的HSTS預載入列表,在該列表中的網站,使用Chrome瀏覽器訪問時,會自動轉換成HTTPS。Firefox、Safari、Edge瀏覽器也會採用這個列表
實現HSTS
Header always set Strict-Transport-Security "max-age=15768000"
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
示例:
vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost :80> DocumentRoot /app/web3 ServerName www.web3.com ErrorLog logs/web3_error_log CustomLog logs/web3_access_log common Header always set Strict-Transport-Security "max-age=15768000" RewriteEngine on RewriteRule ^(/.)$ https://%{HTTP_HOST}$1 [redirect=301] 訪問測試: http://www.web3/com \將會自動跳轉到https://web3.com