• 使用Apache服务部署静态网站


    Apache:目前拥有很高市场占有率的Web服务程序之一,其跨平台和安全性广泛被认可且拥有快速、可靠、简单的API扩展。可以运行在Linux系统、UNIX系统甚至是Windows系统中,支持基于IP、域名及端口号的虚拟主机功能,支持多种认证方式,集成有代理服务器模块、安全Socket层(SSL),能够实时监视服务状态与定制日志消息,并有着各类丰富的模块支持

    Nginx: 一款由俄罗斯的程序设计师 Igor Sysoev 所开发高性能的Web和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。在高连接并发的情况下,Nginx是Apache服务器不错的替代品。

    实验环境

    Windows10系统、VMware Workstation、Red Hat 7、Xshell 6、Chrome浏览器、Firefox浏览器

    Apache安装

    解释 命令
    安装 yum install httpd
    启动 service httpd startsystemctl start httpd
    停止 service httpd stopsystemctl stop httpd
    查询httpd进程是否存在 `ps -ef
    网络统计,查看80端口是否启用 `netstat -anpl
    关闭防火墙(开启80端口) service iptables stop(sytemctl stop iptables)
    加入开机启动项 chkconfig httpd onsystemctl enable httpd
    查看启动情况 chkconfig --list httpd(systemctl is-enabled httpd)

    注:括号内为RedHat7的新命令

    配置服务文件参数

    配置文件 解释
    /etc/httpd 服务目录
    /etc/httpd/conf/httpd.conf 主配置文件
    /var/www/html 网站数据目录
    /var/log/httpd/access_log 访问日志
    /var/log/httpd/error_log 错误日志
    主配置文件的参数 解释
    ServerRoot 服务目录
    ServerAdmin 管理员邮箱
    User 运行服务的用户
    Group 运行服务的用户组
    ServerName 网站服务器的域名
    DocumentRoot 网站数据目录
    Listen 监听的IP地址与端口号
    DirectoryIndex 默认的索引页页面
    ErrorLog 错误日志文件
    CustomLog 访问日志文件
    Timeout 网页超时时间

    1. 建立网站数据的保存目录并创建首页文件

    1
    2
     mkdir -p /home/wwwroot
    echo "The New Web Directory" >> /home/wwwroot/index.html

    2. 修改httpd服务程序的主配置文件
    将DocumentRoot修改为/home/wwwroot
    将用于定义目录权限的参数Directory修改为/home/wwwroot

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     vim /etc/httpd/conf/httpd.conf
    ......
    119 DocumentRoot "/home/wwwroot"
    120
    121 #
    122 # Relax access to content within /var/www.
    123 #
    124 <Directory "/home/wwwroot">
    125 AllowOverride None
    126 # Allow open access:
    127 Require all granted
    128 </Directory>
    ......

    3. 修改SELinux当前的运行模式为禁用(0为禁用,1为启用),临时修改,系统重启后失效,永久的方法看步骤5、6、7
    如果不修改则会报错:Forbidden,You don't have permission to access /index.html on this server.

    1
     setenforce 0

    4. 刷新网页

    正确的网页

    SELinux安全子系统

    • SELinux(Security-Enhanced Linux)
    • 美国国家安全局在Linux开源社区帮助下开发的一个强制访问控制的安全子系统
    • 目的是为了让各个服务进程都受到约束,使其仅获取到本应获取的资源
    • 对服务程序的功能进行限制(SELinux域限制可以确保服务程序做不了出格的事情)
    • 对文件资源的访问限制(SELinux安全上下文确保文件资源只能被其所属的服务程序进行访问)
    • SELinux服务主配置文件是/etc/selinux/config

    SELinux服务的三种配置模式

    • enforcing:强制启用安全策略模式,将拦截服务的不合法请求
    • permissive:遇到服务越权访问时,只发出警告而不强制拦截
    • disabled:对于越权的行为不警告也不拦截

    管理SELinux策略的命令semanage

    ​ SELinux服务极大地提升了Linux系统的安全性,将用户权限牢牢地锁在笼子里。semanage命令不仅能够像传统chcon命令那样——设置文件、目录的策略,还可以管理网络端口、消息接口。其格式为“semanage [选项] [文件]“:

    semanage参数 解释
    -l 用于查询
    -a 用于添加
    -m 用于修改
    -d 用于删除

    getenforce命令获取当前SELinux服务的运行模式

    5. 查看原始网站数据的保存目录与当前网站数据的保存目录是否拥有不同的SELinux安全上下文值

    1
    2
    3
    4
    5
     setenforce 1
    # ls -Zd /var/www/html
    drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
    # ls -Zd /home/wwwroot
    drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot

    注:
    用户段system_u代表系统进程的身份
    角色段object_r代表文件目录的角色
    类型段httpd_sys_content_t代表网站服务的系统文件

    6. 向/home/wwwroot目录新添加一条SELinux安全上下文,使目录本身及目录里的所有文件都能被httpd访问

    1
    2
    # semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
    # semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*

    7. 使用restorecon命令将设置好的SELinux安全上下文立即生效,-Rv参数对指定的目录进行递归操作,并显示SELinux安全上下文的修改过程

    1
    2
    3
    # restorecon -Rv /home/wwwroot/
    restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
    restorecon reset /home/wwwroot/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

    个人用户主页功能

    简单的个人用户主页

    1. 开启个人用户主页功能
    将第17行的UserDir disabled参数注释(加#),表示让httpd服务程序开启个人用户主页功能
    将第24行的UserDir public_html参数的注释去掉(UserDir参数表示网站数据在用户家目录中的保存目录名称,即public_html目录)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    # vim /etc/httpd/conf.d/userdir.conf
    1 #
    2 # UserDir: The name of the directory that is appended onto a user's home
    3 # directory if a ~user request is received.
    4 #
    5 # The path to the end user account 'public_html' directory must be
    6 # accessible to the webserver userid. This usually means that ~userid
    7 # must have permissions of 711, ~userid/public_html must have permissions
    8 # of 755, and documents contained therein must be world-readable.
    9 # Otherwise, the client will only receive a "403 Forbidden" message.
    10 #
    11 <IfModule mod_userdir.c>
    12 #
    13 # UserDir is disabled by default since it can confirm the presence
    14 # of a username on the system (depending on home directory
    15 # permissions).
    16 #
    17 #UserDir disabled
    18
    19 #
    20 # To enable requests to /~user/ to serve the user's public_html
    21 # directory, remove the "UserDir disabled" line above, and uncomment
    22 # the following line instead:
    23 #
    24 UserDir public_html
    25 </IfModule>
    26
    27 #
    28 # Control access to UserDir directories. The following is an example
    29 # for a site where these directories are restricted to read-only.
    30 #
    31 <Directory "/home/*/public_html">
    32 AllowOverride FileInfo AuthConfig Limit Indexes
    33 Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    34 Require method GET POST OPTIONS
    35 </Directory>

    2. 在用户家目录中建立用于保存网站数据的目录及首页面文件并将家目录的权限修改为755,保证其他人也有权限读取里面的内容

    1
    2
    3
    4
    5

    Last login: Tue Apr 16 18:17:42 CST 2019 from 192.168.40.1 on pts/1
    $ mkdir public_html
    $ echo "This is yanji's website" > public_html/index.html
    $ chmod -Rf 755 /home/yanji

    3. 重启httpd服务,浏览器访问,地址为:网址/~用户名,出现Forbidden页面

    禁止访问用户的个人网站

    4. 查询并过滤出所有与HTTP协议相关的安全策略。其中,off为禁止状态,on为允许状态

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    # getsebool -a | grep http
    httpd_anon_write --> off
    httpd_builtin_scripting --> on
    httpd_can_check_spam --> off
    httpd_can_connect_ftp --> off
    httpd_can_connect_ldap --> off
    httpd_can_connect_mythtv --> off
    httpd_can_connect_zabbix --> off
    httpd_can_network_connect --> off
    httpd_can_network_connect_cobbler --> off
    httpd_can_network_connect_db --> off
    httpd_can_network_memcache --> off
    httpd_can_network_relay --> off
    httpd_can_sendmail --> off
    httpd_dbus_avahi --> off
    httpd_dbus_sssd --> off
    httpd_dontaudit_search_dirs --> off
    httpd_enable_cgi --> on
    httpd_enable_ftp_server --> off
    httpd_enable_homedirs --> off
    httpd_execmem --> off
    httpd_graceful_shutdown --> on
    httpd_manage_ipa --> off
    httpd_mod_auth_ntlm_winbind --> off
    httpd_mod_auth_pam --> off
    httpd_read_user_content --> off
    httpd_run_stickshift --> off
    httpd_serve_cobbler_files --> off
    httpd_setrlimit --> off
    httpd_ssi_exec --> off
    httpd_sys_script_anon_write --> off
    httpd_tmp_exec --> off
    httpd_tty_comm --> off
    httpd_unified --> off
    httpd_use_cifs --> off
    httpd_use_fusefs --> off
    httpd_use_gpg --> off
    httpd_use_nfs --> off
    httpd_use_openstack --> off
    httpd_use_sasl --> off
    httpd_verify_dns --> off
    named_tcp_bind_http_port --> off
    prosody_bind_http_port --> off

    5. 修改SELinux策略中与个人用户主页功能相关的规则布尔值为on,刷新网页
    参数-P让修改后的SELinux策略规则永久生效且立即生效

    1
    # setsebool -P httpd_enable_homedirs=on

    添加口令功能

    1. htpasswd生成密码数据库
    -c参数表示第一次生成;后面的是密码数据库的存放文件,以及验证要用到的用户名称(该用户不必是系统中已有的本地账户)

    1
    2
    3
    4
    # htpasswd -c /etc/httpd/passwd yanji
    New password:
    Re-type new password:
    Adding password for user yanji

    2. 编辑个人用户主页功能的配置文件,然后重启httpd服务,刷新网页后需输入口令才能访问
    (RedHat7之前的个人用户主页功能是在主配置文件中的)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # vim /etc/httpd/conf.d/userdir.conf
    ......
    31 <Directory "/home/*/public_html">
    32 AllowOverride all
    #密码验证文件保存路径
    33 authuserfile "/etc/httpd/passwd"
    #当用户尝试访问个人用户网站时的提示信息
    34 authname "My privately website"
    35 authtype basic
    #用户进行账户密码登录时需要验证的用户名称
    36 require user yanji
    37 </Directory>
    # systemctl restart httpd

    网站提示需要输入账户和密码才能访问

    虚拟网站主机功能

    基于IP地址

    1. 配置IP地址

    nmtui命令打开网络配置页→选中Edit a connection→Edit→【把网络IPv4的配置方式改成Manual(手动)→show】→填写IP地址→OK

    Edit a connection

    Edit Ethernet

    Add IPv4 Addresses

    2. 将网卡配置文件中的ONBOOT参数修改成yes,重启网卡

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # vim /etc/sysconfig/network-scripts/ifcfg-eno16777736
    TYPE=Ethernet #设备类型
    BOOTPROTO=none #地址分配模式
    DEFROUTE=yes
    DNS1=10.11.160.1 #DNS地址
    IPV4_FAILURE_FATAL=no
    NAME=eno16777728 #网卡名称
    UUID=fb861ba7-9b2e-4fe4-81e4-6e940581c86e
    ONBOOT=yes #是否启动
    IPADDR0=192.168.40.131 #IP地址
    PREFIX0=24 #子网掩码的网络号
    GATEWAY0=192.168.40.1 #网关地址
    IPADDR1=192.168.40.141
    PREFIX1=24
    IPADDR2=192.168.40.151
    PREFIX2=24
    IPV6INIT=no
    HWADDR=00:0C:29:4A:20:E2
    # systemctl restart network

    3. 测试网络连通性

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    # ping -c 2 192.168.40.131
    PING 192.168.40.131 (192.168.40.131) 56(84) bytes of data.
    64 bytes from 192.168.40.131: icmp_seq=1 ttl=64 time=0.122 ms
    64 bytes from 192.168.40.131: icmp_seq=2 ttl=64 time=0.048 ms

    --- 192.168.40.131 ping statistics ---
    2 packets 大专栏  使用Apache服务部署静态网站 transmitted, 2 received, 0% packet loss, time 1000ms
    rtt min/avg/max/mdev = 0.048/0.085/0.122/0.037 ms
    # ping -c 2 192.168.40.141
    PING 192.168.40.141 (192.168.40.141) 56(84) bytes of data.
    64 bytes from 192.168.40.141: icmp_seq=1 ttl=64 time=0.030 ms
    64 bytes from 192.168.40.141: icmp_seq=2 ttl=64 time=0.035 ms

    --- 192.168.40.141 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 999ms
    rtt min/avg/max/mdev = 0.030/0.032/0.035/0.006 ms
    # ping -c 2 192.168.40.151
    PING 192.168.40.151 (192.168.40.151) 56(84) bytes of data.
    64 bytes from 192.168.40.151: icmp_seq=1 ttl=64 time=0.133 ms
    64 bytes from 192.168.40.151: icmp_seq=2 ttl=64 time=0.043 ms

    --- 192.168.40.151 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1000ms
    rtt min/avg/max/mdev = 0.043/0.088/0.133/0.045 ms

    4. 分别在/home/wwwroot中创建用于保存不同网站数据的3个目录,并分别写入网站的首页文件

    1
    2
    3
    4
    # mkdir -p /home/wwwroot/{131,141,151}
    # echo "IP:192.168.40.131" > /home/wwwroot/131/index.html
    # echo "IP:192.168.40.141" > /home/wwwroot/141/index.html
    # echo "IP:192.168.40.151" > /home/wwwroot/151/index.html

    5. 在httpd配置文件中添加三个基于IP地址的虚拟主机网站参数,重启httpd服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # vim /etc/httpd/conf/httpd.conf
    ......
    113 <VirtualHost 192.168.40.131>
    114 DocumentRoot /home/wwwroot/131
    115 ServerName www.zengzhilai.com
    116 <Directory /home/wwwroot/131 >
    117 AllowOverride None
    118 Require all granted
    119 </Directory>
    120 </VirtualHost>
    121 <VirtualHost 192.168.40.141>
    122 DocumentRoot /home/wwwroot/141
    123 ServerName bbs.zengzhilai.com
    124 <Directory /home/wwwroot/141 >
    125 AllowOverride None
    126 Require all granted
    127 </Directory>
    128 </VirtualHost>
    129 <VirtualHost 192.168.40.151>
    130 DocumentRoot /home/wwwroot/151
    131 ServerName mail.zengzhilai.com
    132 <Directory /home/wwwroot/151 >
    133 AllowOverride None
    134 Require all granted
    135 </Directory>
    136 </VirtualHost>
    ......
    # systemctl restart httpd

    6. 访问网站会看到编写的网页,如果显示的是默认首页,则可能是SElinux安全上下文与网站服务不符,参考配置服务文件参数的步骤6和7

    基于主机域名

    1. 定义IP地址与域名之间对应关系(本来应该用DNS解析服务的,但为演示方便直接修改hosts文件了)

    由于是在Windows系统Chrome浏览器上测试,所以应修改C:WindowsSystem32driversetcHOSTS文件,而Linux的是/etc/hosts文件,添加下面一行即可

    1
    192.168.40.131 www.zengzhilai.com bbs.zengzhilai.com mail.zengzhilai.com

    测试域名是否成功解析

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    C:Userszengzhilai>ping www.zengzhilai.com

    正在 Ping www.zengzhilai.com [192.168.40.131] 具有 32 字节的数据:
    来自 192.168.40.131 的回复: 字节=32 时间<1ms TTL=64
    来自 192.168.40.131 的回复: 字节=32 时间<1ms TTL=64
    来自 192.168.40.131 的回复: 字节=32 时间<1ms TTL=64
    来自 192.168.40.131 的回复: 字节=32 时间<1ms TTL=64

    192.168.40.131 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
    往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

    2. 在/home/wwwroot中创建用于保存不同网站数据的三个目录,并分别写入网站的首页文件

    1
    2
    3
    4
    # mkdir -p /home/wwwroot/{www,bbs,mail}
    # echo "www.zengzhilai.com" > /home/wwwroot/www/index.html
    # echo "bbs.zengzhilai.com" > /home/wwwroot/bbs/index.html
    # echo "mail.zengzhilai.com" > /home/wwwroot/mail/index.html

    3. 在配置文件写入三个基于主机名的虚拟主机网站参数,重启httpd服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # vim /etc/httpd/conf/httpd.conf
    ......
    113 <VirtualHost 192.168.40.131>
    114 DocumentRoot "/home/wwwroot/www"
    115 ServerName "www.zengzhilai.com"
    116 <Directory "/home/wwwroot/www" >
    117 AllowOverride None
    118 Require all granted
    119 </Directory>
    120 </VirtualHost>
    121 <VirtualHost 192.168.40.131>
    122 DocumentRoot "/home/wwwroot/bbs"
    123 ServerName "bbs.zengzhilai.com"
    124 <Directory "/home/wwwroot/bbs" >
    125 AllowOverride None
    126 Require all granted
    127 </Directory>
    128 </VirtualHost>
    129 <VirtualHost 192.168.40.131>
    130 DocumentRoot "/home/wwwroot/mail"
    131 ServerName "mail.zengzhilai.com"
    132 <Directory "/home/wwwroot/mail" >
    133 AllowOverride None
    134 Require all granted
    135 </Directory>
    136 </VirtualHost>
    ......
    # systemctl restart httpd

    4. 正确设置网站数据目录文件的SELinux安全上下文,使其与网站服务功能相吻合
    (由于在前面的实验中已经设置/home/wwwroot目录及其下的所有文件符合SELinux安全上下文了,所以这一步可省略)

    5. 访问虚拟主机网站

    www.zengzhilai.com

    bbs.zengzhilai.com

    mail.zengzhilai.com

    基于端口号

    • 可以让用户通过指定的端口号来访问服务器上的网站资源
    • 需考虑httpd服务程序的配置因素以及SELinux服务对新开设端口的监控
    • 一般使用80、443、8080等端口号来提供网站访问服务是比较合理的

    1. 在/home/wwwroot中创建用于保存不同网站数据的两个目录,并分别写入网站的首页文件

    1
    2
    3
    # mkdir -p /home/wwwroot/{6111,6222}
    # echo "port:6111" > /home/wwwroot/6111/index.html
    # echo "port:6222" > /home/wwwroot/6222/index.html

    2. 在httpd服务配置文件中添加用于监听6111和6222端口的参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # vim /etc/httpd/conf/httpd.conf
    ......
    33 #
    34 # Listen: Allows you to bind Apache to specific IP addresses and/or
    35 # ports, instead of the default. See also the <VirtualHost>
    36 # directive.
    37 #
    38 # Change this to Listen on specific IP addresses as shown below to
    39 # prevent Apache from glomming onto all bound IP addresses.
    40 #
    41 #Listen 12.34.56.78:80
    42 Listen 80
    43 Listen 6111
    44 Listen 6222
    ......

    3. 在httpd服务配置文件中写入两个基于端口号的虚拟主机网站参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # vim /etc/httpd/conf/httpd.conf
    ......
    114 <VirtualHost 192.168.40.131:6111>
    115 DocumentRoot "/home/wwwroot/6111"
    116 ServerName www.zengzhilai.com
    117 <Directory "/home/wwwroot/6111" >
    118 AllowOverride None
    119 Require all granted
    120 </Directory>
    121 </VirtualHost>
    122 <VirtualHost 192.168.40.131:6222>
    123 DocumentRoot "/home/wwwroot/6222"
    124 ServerName bbs.zengzhilai.com
    125 <Directory "/home/wwwroot/6222" >
    126 AllowOverride None
    127 Require all granted
    128 </Directory>
    129 </VirtualHost>
    ......

    修改完配置文件后重启httpd服务会出现错误:
    Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.

    这是因为SELinux服务检测到6111和6222端口原本不属于Apache服务应该需要的资源,但现在却以httpd服务程序的名义监听使用了,所以SELinux会拒绝使用Apache服务使用这两个端口

    4. 正确设置网站数据目录文件的SELinux安全上下文,使其与网站服务功能相吻合

    1
    2
    3
    4
    5
    6
    # semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
    # semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111
    # semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111/*
    # semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222
    # semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222/*
    # restorecon -Rv /home/wwwroot/

    5. 添加6111和6222两端口号到SELinux允许的与HTTP协议相关的默认端口号中,重启httpd服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # semanage port -a -t http_port_t -p tcp 6111
    # semanage port -a -t http_port_t -p tcp 6222
    # semanage port -l | grep http
    http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
    http_cache_port_t udp 3130
    http_port_t tcp 6111, 6222, 80, 81, 443, 488, 8008, 8009, 8443, 9000
    pegasus_http_port_t tcp 5988
    pegasus_https_port_t tcp 5989
    # systemctl restart httpd

    6. 防火墙开放6111和6222端口(关闭防火墙systemctl stop iptables),重启防火墙,访问网站

    1
    2
    3
    4
    5
    6
    # vim /etc/sysconfig/iptables
    ....
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 6111 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 6222 -j ACCEPT
    ....
    # systemctl restart iptables

    port6111

    port6222

    注意:

    原本端口号是随手打的6666和6667,但是实验后发现chrome浏览器对这两个端口进行了限制,查资料后给出的解释是这样的:

    6666-6669是IRC协议使用的缺省端口,有安全风险,容易被木马程序利用,所以webkit内核的chrome缺省状态下禁止访问

    FireFox访问给出的解释是这样的(感谢火狐浏览器☺):

    Port restricted

    Apache的访问控制

    • Apache可以基于源主机名、源IP地址或源主机上的浏览器特征等信息对网站上的资源进行访问控制
    • 通过Allow指令允许某个主机访问服务器上的网站资源,通过Deny指令实现禁止访问
    • Order指令用来定义Allow或Deny指令起作用的顺序,其匹配原则是按照顺序进行匹配,若匹配成功则执行后面的默认指令
    • 如“Order Allow, Deny”表示先将源主机与允许规则进行匹配,若匹配成功则允许访问请求,反之则拒绝访问请求

    在服务器上的网站数据目录中新建一个子目录,并创建首页文件
    在前面已设置/home/wwwroot替代/var/www/html了,所以默认目录变了

    1
    2
    # mkdir /home/wwwroot/server
    # echo "Successful" > /var/www/html/server/index.html

    匹配源主机的浏览器进行访问控制

    在httpd服务的配置文件中添加规则来限制源主机的访问

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # vim /etc/httpd/conf/httpd.conf
    ......
    # 允许使用Firefox浏览器的主机访问服务器上的首页文件,除此之外的所有请求都将被拒绝
    129 <Directory "/home/wwwroot/server">
    130 SetEnvIf User-Agent "Firefox" ff=1
    131 Order allow,deny
    132 Allow from env=ff
    133 </Directory>
    ......
    # systemctl restart httpd

    火狐浏览器访问成功页面:

    谷歌浏览器访问被禁止页面:

    匹配源主机的IP地址进行访问控制

    在httpd服务的配置文件中添加规则:只允许IP地址为192.168.40.131的主机访问网站资源

    1
    2
    3
    4
    5
    6
    7
    # vim /etc/httpd/conf/httpd.conf
    ......
    129 <Directory "/home/wwwroot/server">
    130 Order allow,deny
    131 Allow from 192.168.40.131
    132 </Directory>
    ......

    Windows 10 IP地址不符合,被拒绝访问:

    服务器的本身的IP地址192.168.40.131符合,允许访问

  • 相关阅读:
    MySQL面试题-基础
    IDEA 创建Spring项目后org.springframework.boot报错
    IDEA上传项目到SVN
    解决:idea中右键项目找不到subversion
    关于fromdata的上传文件问题
    PHP清除数组中为0的元素
    PHP删除数组中重复的元素
    PHP代码篇(六)--如何根据邀请人id查询满足条件的会员上级
    PHP对URL进行字符串编码
    PHP时间戳相互转换
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12037801.html
Copyright © 2020-2023  润新知