美文网首页APACHE
3.HTTPD实现多虚拟主机

3.HTTPD实现多虚拟主机

作者: Stone_説 | 来源:发表于2020-08-04 00:47 被阅读0次

目录:
1.统计日志访问量
2.添加加密模块
3.添加压缩功能
4.多虚拟主机

1.统计日志访问量

[root@node03 logs]# awk '{print $1}' access_log|sort|uniq -c|sort -nr
  29934 192.168.177.131
   2000 192.168.177.138
    654 ::1
    253 192.168.177.1
      4 127.0.0.1
      2 192.168.177.154
      1 192.168.177.153
[root@node03 logs]# awk '{print $1}' access_log|sort|uniq -c|sort -nr|head -3
  29934 192.168.177.131
   2000 192.168.177.138
    654 ::1

2.添加加密模块

[root@node03 ~]# httpd -M|grep ssl
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::6042:9b26:de9e:d6f4. Set the 'ServerName' directive globally to suppress this message
[root@node03 ~]# yum install  mod_ssl
[root@node03 ~]# rpm -ql mod_ssl
/etc/httpd/conf.d/ssl.conf
/etc/httpd/conf.modules.d/00-ssl.conf
/usr/lib64/httpd/modules/mod_ssl.so
/usr/libexec/httpd-ssl-pass-dialog
/var/cache/httpd/ssl
[root@node03 ~]# cat /etc/httpd/conf.modules.d/00-ssl.conf 
LoadModule ssl_module modules/mod_ssl.so
[root@node03 ~]# httpd -M|grep ssl
 ssl_module (shared)
[root@node03 ~]# ss -ntl
State       Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN      0      128                                                        *:22                                                                     *:*                  
LISTEN      0      100                                                127.0.0.1:25                                                                     *:*                  
LISTEN      0      128                                                       :::80                                                                    :::*                  
LISTEN      0      128                                                       :::22                                                                    :::*                  
LISTEN      0      100                                                      ::1:25                                                                    :::*                  
[root@node03 ~]# systemctl restart httpd
[root@node03 ~]# ss -ntl
State       Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN      0      128                                                        *:22                                                                     *:*                  
LISTEN      0      100                                                127.0.0.1:25                                                                     *:*                  
LISTEN      0      128                                                       :::80                                                                    :::*                  
LISTEN      0      128                                                       :::22                                                                    :::*                  
LISTEN      0      100                                                      ::1:25                                                                    :::*                  
LISTEN      0      128                                                       :::443                                                                   :::*

3.添加压缩功能

DocumentRoot "/data/html"
<Directory "/data/html">
    Require all granted
    #Options Indexes FollowSymLinks
    AllowOverride All
</Directory>

<virtualhost *:80>
Documentroot /data/asite
servername www.a.com
<Directory "/data/asite">
    Require all granted
</Directory>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
DeflateCompressionLevel 9
</virtualhost>
[root@centos6 ~]# curl -I 192.168.177.153/m.txt
HTTP/1.1 200 OK
Date: Sun, 02 Aug 2020 08:54:52 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Sun, 02 Aug 2020 08:51:13 GMT
ETag: "768d0-5abe11fd1b541"
Accept-Ranges: bytes
Content-Length: 485584
Vary: Accept-Encoding
Content-Type: text/plain; charset=UTF-8

[root@centos6 ~]# curl -I --compressed 192.168.177.153/m.txt
HTTP/1.1 200 OK
Date: Sun, 02 Aug 2020 08:54:47 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Sun, 02 Aug 2020 08:51:13 GMT
ETag: "768d0-5abe11fd1b541-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 1100
Content-Type: text/plain; charset=UTF-8

4.多虚拟主机

4.1介绍

站点标识: 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虚拟主机

基于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>
注意:任意目录下的页面只有显式授权才能被访问
三种方式的虚拟主机可以混和使用

4.2基于IP的多虚拟主机

DocumentRoot "/data/html"
<Directory "/data/html">
    Require all granted
    #Options Indexes FollowSymLinks
    AllowOverride All
</Directory>
<virtualhost 192.168.177.101:80>
Documentroot /data/asite
<Directory "/data/asite">
    Require all granted
</Directory>
</virtualhost>

<virtualhost 192.168.177.102:80>
Documentroot /data/bsite
<Directory "/data/bsite">
    Require all granted
</Directory>
</virtualhost>

<virtualhost 192.168.177.103:80>
Documentroot /data/csite
<Directory "/data/csite">
    Require all granted
</Directory>
</virtualhost>

测试

[root@node03 data]# mkdir {a,b,c}site
[root@node03 data]# ls
asite  bsite  csite  html
[root@node03 data]# echo www.a.com > asite/index.html
[root@node03 data]# echo www.b.com > bsite/index.html
[root@node03 data]# echo www.c.com > csite/index.html
[root@node03 ~]# ip a a 192.168.177.101/24 dev ens33
[root@node03 ~]# ip a a 192.168.177.102/24 dev ens33
[root@node03 ~]# ip a a 192.168.177.103/24 dev ens33
[root@node03 ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:b2:1f:21 brd ff:ff:ff:ff:ff:ff
    inet 192.168.177.153/24 brd 192.168.177.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.177.101/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet 192.168.177.102/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet 192.168.177.103/24 scope global secondary ens33
       valid_lft forever preferred_lft forever

[root@centos6 ~]# vim /etc/hosts
192.168.177.101 www.a.com
192.168.177.102 www.b.com
192.168.177.103 www.c.com
[root@centos6 ~]# curl www.a.com
www.a.com
[root@centos6 ~]# curl www.b.com
www.b.com
[root@centos6 ~]# curl www.c.com
www.c.com

4.3基于PORT的多虚拟主机

DocumentRoot "/data/html"
<Directory "/data/html">
    Require all granted
    #Options Indexes FollowSymLinks
    AllowOverride All
</Directory>

Listen 81
Listen 82
Listen 83

<virtualhost *:81>
Documentroot /data/asite
<Directory "/data/asite">
    Require all granted
</Directory>
</virtualhost>

<virtualhost *:82>
Documentroot /data/bsite
<Directory "/data/bsite">
    Require all granted
</Directory>
</virtualhost>

<virtualhost *:83>
Documentroot /data/csite
<Directory "/data/csite">
    Require all granted
</Directory>
</virtualhost>

测试

[root@centos6 ~]# vim /etc/hosts
192.168.177.153 www.a.com www.b.com www.c.com
[root@centos6 ~]# curl 192.168.177.153:81
www.a.com
[root@centos6 ~]# curl 192.168.177.153:83
www.c.com
[root@centos6 ~]# curl 192.168.177.153:82
www.b.com
[root@centos6 ~]#
[root@centos6 ~]# curl www.a.com:81
www.a.com
[root@centos6 ~]# curl www.a.com:82
www.b.com
[root@centos6 ~]# curl www.a.com:83
www.c.com

4.4基于FQDN多虚拟主机

DocumentRoot "/data/html"
<Directory "/data/html">
    Require all granted
    #Options Indexes FollowSymLinks
    AllowOverride All
</Directory>

<virtualhost *:80>
Documentroot /data/asite
servername www.a.com
<Directory "/data/asite">
    Require all granted
</Directory>
</virtualhost>

<virtualhost *:80>
Documentroot /data/bsite
servername www.b.com
<Directory "/data/bsite">
    Require all granted
</Directory>
</virtualhost>

<virtualhost *:80>
Documentroot /data/csite
servername www.c.com
<Directory "/data/csite">
    Require all granted
</Directory>
</virtualhost>

测试

[root@centos6 ~]# cat /etc/hosts
192.168.177.153 www.a.com www.b.com www.c.com
[root@centos6 ~]# curl www.a.com
www.a.com
[root@centos6 ~]# curl www.b.com
www.b.com
[root@centos6 ~]# curl www.c.com
www.c.com

加载模块

[root@node03 logs]# cd /etc/httpd/conf.modules.d/
[root@node03 conf.modules.d]# pwd
/etc/httpd/conf.modules.d
[root@node03 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@node03 conf.modules.d]# cat 00-base.conf |grep deflate
LoadModule deflate_module modules/mod_deflate.so

相关文章

  • 3.HTTPD实现多虚拟主机

    目录:1.统计日志访问量2.添加加密模块3.添加压缩功能4.多虚拟主机 1.统计日志访问量 2.添加加密模块 3....

  • 2. Apache - 高级配置

    21. 多虚拟主机 多虚拟主机应用场景: 网站的唯一标识: 多虚拟主机有三种实现方案: 准备环境: 1. 基于IP...

  • apache

    源码编译安装httpd-2.4.39 实现basic用户验证 实现家目录的共享 实现状态页面 基于IP的多虚拟主机...

  • apache创建虚拟主机实现多站点服务

    Apache配置虚拟主机,单主机多主机配置,apache配置多个域名 一、虚拟主机的配置 所谓的虚拟主机就是使用一...

  • wampserver多站点配置

    困扰我好久的wampserver多站点配置总算解决了,实现在本地localhost环境下用多个不用的虚拟主机的方式...

  • Apache中多个HTTPS虚拟主机的实现

    点击:申请SSL证书 要实现一个Apache服务器上提供多个SSL虚拟主机,可以: * 使用多域名SSL证书,可以...

  • 如何实现多台虚拟主机

    基于三种方式:实现多台虚拟主机。一:基于端口号实现三台虚拟主机 IP相同但端口不同 1):创建三个网站页面的目录...

  • Linux_253_Nginx基于多域名的虚拟主机

    Nginx基于多域名的虚拟主机配置基于多IP的虚拟主机,用的还是不多的,还可能造成IP不足等问题,一般如果没有特殊...

  • Linux_254_Nginx基于域名&端口的多虚拟主机

    Nginx基于多域名的虚拟主机配置基于多IP的虚拟主机,用的还是不多的,还可能造成IP不足等问题,一般如果没有特殊...

  • 图解HTTP-第5章-与HTTP协作的Web服务器

    用单台虚拟主机实现多个域名 HTTP/1.1规范允许一台HTTP服务器搭建多个Web站点.这是因为利用了虚拟主机(...

网友评论

    本文标题:3.HTTPD实现多虚拟主机

    本文链接:https://www.haomeiwen.com/subject/pciprktx.html