httpd虚拟主机和压缩
17、虚拟主机
站点标识: socket
IP相同,但端口不同
IP不同,但端口均为默认端口
FQDN不同:请求报文中首部 Host: www.magedu.com
有三种实现方案:
基于ip:为每个虚拟主机准备至少一个ip地址
基于port:为每个虚拟主机使用至少一个独立的port
基于FQDN:为每个虚拟主机使用至少一个FQDN。推荐使用。
建议:虚拟主机配置存放在独立的配置文件中
虚拟主机的配置方法:
<VirtualHost IP:PORT>
ServerName FQDN
DocumentRoot “/path"
</VirtualHost>
其它可用指令:
ServerAlias:虚拟主机的别名;可多次使用
ErrorLog: 错误日志
CustomLog:访问日志
<Directory “/path"> </Directory>
Alias
基于IP的虚拟主机示例:
<VirtualHost 172.16.100.6:80>
ServerName www.a.com
DocumentRoot "/www/a.com/htdocs"
</VirtualHost>
<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>
基于端口的虚拟主机:
listen 808
listen 8080
<VirtualHost 172.16.100.6:80>
ServerName www.a.com
DocumentRoot "/www/a.com/htdocs"
</VirtualHost>
<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虚拟主机
centos7基于FQDN的虚拟主机不再需要NameVirutalHost指令
<VirtualHost *:80>
ServerName www.b.net
DocumentRoot "/apps/b.net/htdocs"
<Directory "/apps/b.net/htdocs">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
注意:任意目录下的页面只有显式授权才能被访问
三种方式的虚拟主机可以混和使用
实验五:实现基于FQDN的虚拟主机
/etc/httpd/conf.d/virtualhost.conf <VirtualHost *:80> ServerName www.test.net DocumentRoot "/apps/test.net/htdocs" <Directory "/apps/test.net/htdocs"> options None AllowOverride None Require all granted </Directory> </VirtualHost>
mkdir -p /apps/test.net/htdocs
echo virtual_host > /apps/test.net/htdocs/index.html
vim /etc/hosts
192.168.206.15 www.test.net
18、mod_deflate模块
使用mod_deflate模块压缩页面优化传输速度
适用场景:
(1) 节约带宽,额外消耗CPU;同时,可能有些较老浏览器不支持
(2) 压缩适于压缩的资源,例如文本文件
LoadModule deflate_module modules/mod_deflate.so
SetOutputFilter DEFLATE # 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
Level of compression (Highest 9 - Lowest 1)
DeflateCompressionLevel 9
排除特定旧版本的浏览器,不支持压缩
Netscape 4.x 只压缩text/html
BrowserMatch ^Mozilla/4 gzip-only-text/html
Netscape 4.06-08三个版本 不压缩
BrowserMatch ^Mozilla/4\.0[678] no-gzip
Internet Explorer标识本身为“Mozilla / 4”,但实际上是能够处理请求的压缩。如果用户代理首部匹配字符串“MSIE”(“B”为单词边界”),就关闭之前定义的限制
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
posted on 2020-08-22 17:09 HowOldAreYou 阅读(151) 评论(0) 编辑 收藏 举报