Bootstrap

Linux Web发布 限制用户、客户端与基于IP、端口、域名访问

httpd服务的访问控制

发布web,dns正向解析(参考Linux web发布一)

1、客户端地址限制

vim /usr/local/httpd/conf/httpd.conf

用 / 命令查找 htdocs
DocumentRoot "/usr/local/httpd/htdocs"
<Directory "/usr/local/httpd/htdocs">     (在下列编辑)



<RequireAll>             (开始)
Require all granted      (允许所有,删除自带,重新添加)
Require not ip 192.168.1.250 192.168.2.0/24   (限制1.250主机和2.0网段ip)
</RequireAll>            (结束)

Require all granted:表示允许所有主机访问。
Require all denied:表示拒绝所有主机访问。
Require local:表示仅允许本地主机访问。
Require【not】host<主机名或域名列表>:表示允许或拒绝指定主机或域访问。
Require【not】ip<ip地址或网段列表>:表示允许或拒绝指定P地址或网段访问。 

重启Apache服务

/usr/local/httpd/bin/apachectl restart

 客户机1.250登录失败,限制成功。

2、用户授权设置 

         (1)创建用户认证数据文件

cd /usr/local/httpd/     (对/usr/local/.httpd/htdocs网页目录下的网页内容添加用户授权限制)

bin/htpasswd -c /usr/local/httpd/conf/.awspwd zhangsan (添加用户zhangsan)
New password:            (设置密码,不显示)
Re-type new password:    (确认密码)


bin/htpasswd  /usr/local/httpd/conf/.awspwd lisi (添加用户lisi)
New password:            (设置密码,不显示)
Re-type new password:    (确认密码)


cat /usr/local/httpd/conf/.awspwd   (确认用户文件数据)

httpd服务器支持使用摘要认证(Digest)和基本认证(Basic)两种方式,使用摘要认证需要在编译httpd之前添加"--enable--auth一digest”选项,但并不是所有的浏览器都支持摘要认证,而基本认证是httpd服务的基本功能,不需要预先配置特别的选项。
 添加 "-C" 选项表示新建立此文件

       (2)添加用户授权配置

需要修改httpd.cof配置文件,在特定的目录区域中添加授权配置以启用基本认证并设置允许哪些用户访问。例如,若只允许:aws即wd数据文件中的任一用户访问网页,可以执行以下操作,

vim  /usr/local/httpd/conf/httpd.conf

用 / 命令查找 htdocs

(以下为编辑内容)

DocumentRoot "/usr/local/httpd/htdocs"
<Directory "/usr/local/httpd/htdocs">     (在下列编辑)


AuthName "DocumentRoot"                   (描述)
AuthType Basic                            (基本认证)
AuthUserFile /usr/local/httpd/conf/.awspwd(用户文件)
Require  valid-user

<RequireAll>
Require all denied
Require not ip 192.168.1.250  192.168.2.0/24
</RequireAll>

        重启httpd服务 

/usr/local/httpd/bin/apachectl restart

 客户机访问192.168.1.10弹出用户登录界面完成

 3、基于域名的虚拟主机

         (1)为虚拟主机提供域名解析

        named服务主配置

vim /etc/named.conf

zone     "aaa.com."     in      {
        type master; 
        file "aaa.com.zone";
};

zone    "bbb.com."      in      {
        type master; 
        file "bbb.com.zone";
};

         aaa.com区域配置

vim /var/named/aaa.com.zone
    (以下为编辑内容)

$ttl    86400
@    in     soa     aaa.com.    admin.aaa.com. (
    20220424
    3h
    15m
    1w
    1d
)
        in     ns       ns1.aaa.com.
ns1     in     a        192.168.1.10
www     in     a        192.168.1.10

         bbb.com区域配置

vim /var/named/bbb.com.zone
    (以下为编辑内容)

$ttl    86400
@    in     soa     bbb.com.    admin.bbb.com. (
    20220424
    3h
    15m
    1w
    1d
)
        in     ns       ns1.bbb.com.
ns1     in     a        192.168.1.10
www     in     a        192.168.1.10

        重启服务

systemctl restart named

         (2)为虚拟主机准备网页文档

mkdir -p /var/www/html/aaacom    (为www.aaa.com创建目录)
mkdir -p /var/www/html/bbbcom    (为www.bbb.com创建目录)

echo "<h1>www.aaa.com</h1>" >/var/www/html/aaacom/index.html    (网站跟目录)
echo "<h1>www.bbb.com</h1>" >/var/www/html/bbbcom/index.html    (网站跟目录)

        (3)添加虚拟主机配置 

vim /usr/local/httpd/conf/extra/httpd-vhosts.conf

(以下为修改内容)

<VirtualHost *:80>
    ServerAdmin [email protected]                (创建独立配置文件)
    DocumentRoot "/var/www/html/aaacom"          (设置aaa虚拟站点区域)
    ServerName www.aaa.com
    ServerAlias www.aaa.com                       (别名)
    ErrorLog "logs/www.aaa.com.error_log"
    CustomLog "logs/www.aaa.com.access_log" common

<Directory "/var/www/html">                       (设置访问目录权限)
        Require all granted

</Directory>

</VirtualHost>



<VirtualHost *:80>
    ServerAdmin [email protected]                  (创建独立配置文件)
    DocumentRoot "/var/www/html/bbbcom"            (设置aaa虚拟站点区域)
    ServerName www.bbb.com
    ErrorLog "logs/www.bbb.com.error_log"
    CustomLog "logs/www.bbb.com.access_log" common

<Directory "/var/www/html">                        (设置访问目录权限)

        Require all granted
</Directory>
</VirtualHost>
vim /usr/local/httpd/conf/httpd.conf

Include conf/extra/httpd-vhosts.conf  去掉#(去注释,加载独立的配置文件)

        重启服务

/usr/local/httpd/bin/apachectl restart

 客户机访问虚拟web,ww.aaa.com和www.bbb.com,显示aaa和bbb结束

4、不同IP地址,相同域名

 (1)配置多个IP地址(临时)
 

ifconfig    ens33:2  192.168.1.20

  (2)设置虚拟站点区域 

分别指定各自域名对应的IP地址

vim /usr/local/httpd/conf/extra/httpd-vhosts.conf


<VirtualHost 192.168.1.10:80> 
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/aaacom"
    ServerName www.aaa.com
    ErrorLog "logs/www.aaa.com.error_log"
    CustomLog "logs/www.aaa.com.access_log" common 
    
<Directory "/var/www/html">
        Require all granted
    
</Directory>
    
</VirtualHost>

<VirtualHost 192.168.1.20:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/bbbcom"
    ServerName www.bbb.com
    ErrorLog "logs/www.bbb.com.error_log"
    CustomLog "logs/www.bbb.com.access_log" common

        重启服务

/usr/local/httpd/bin/apachectl restart

客户端分别访问192.168.1.10和192.168.1.20完成

 5、不同端口,相同IP地址的虚拟主机

        基于80端口和8353端口

vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
(以下修改内容)

<VirtualHost 192.168.1.10:80>        (设置端口)
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/aaacom"
    ServerName www.aaa.com
    ErrorLog "logs/www.aaa.com.error_log"
    CustomLog "logs/www.aaa.com.access_log" common

<Directory "/var/www/html">
        Require all granted

</Directory>

</VirtualHost>

<VirtualHost 192.168.1.10:8353>        (设置端口)
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/bbbcom"
    ServerName www.bbb.com
    ErrorLog "logs/www.bbb.com.error_log"
    CustomLog "logs/www.bbb.com.access_log" common
<Directory "/var/www/html">

        Require all granted
</Directory>

</VirtualHost>
Listen  80        (监听端口)
Listen  8353      (监听端口)

        重启服务 

/usr/local/httpd/bin/apachectl restart

        客户端访问192.168.1.10:80 和192.168.1.10:8353成功

;