zoukankan      html  css  js  c++  java
  • Linux学习77 基于apache实现linux平台web服务配置

    一、http相应配置(续)  

      1、站点访问控制常见机制

        我们URL路径和文件系统路径彼此之间是需要建立映射关系的, 而这里面的页面资源是不允许用户随便访问的。

        a、可基于两种机制指明对哪些资源进行何种访问控制

          (1)、文件系统路径

            1)、基于某个目录下的所有资源做控制

            <Directory "">

            ...

            </Directory>

            2)、只针对于一个文件或一组文件

            <File "">

            ...

            </File>

            3)、只针对于一个文件或一组文件,用正则匹配

            <FileMatch "PATTERN">

            ...

            </FileMatch>

          (2)、URL路径

            1)、Location中给定的都应该是URL

              <Location "">

              ...

              </Location>

            2)、LocationMatch 中给定的是单个Location或通配符或正则表达式匹配的路径

              <LocationMatch "PATTERN">

              ...

              </LocationMatch>

        b、<Directory>中“基于源地址”实现访问控制

          (1)、相应格式

            1)、httpd-2.2中定义:

              AllowOverride

                i、与访问控制相关的指令可以放在 .htaccess文件(每个目录下都可以有一个)中;

                  ALL:

                  None:

              order和allow、deny

                i、order:定义生效次序,写在后面的表示默认法则

                ii、Allow from,Deny from

                  来源地址:

                  IP

                  NetAddr:(下面几种方式的写法都可以)

                    192.168(表示允许192.168网段地址的主机访问)

                    192.168.0.0

                    192.168.0.0/16

                    192.168.0.0/255.255.0.0

            2)、httpd-2.4

              i、基于IP控制

                Require ip IP地址或网络地址

                Require not ip IP地址或网络地址

              ii、基于主机名控制

                Require host 主机名或域名

                Require not host 主机名或域名

          (2)、控制页面资源允许所有来源的主机可访问:

            1)、httpd-2.2:

                <Directory "/data/web/www">

                ...
                Order allow,deny

                Allow from all
                </Directory>

            2)、httpd-2.4:

                <Directory "/data/web/www">

                ...    

                Require all granted

                </Directory>

          (3)、控制页面资源拒绝所有来源的主机可访问:

            1)、httpd-2.2:

                <Directory "/data/web/www">

                ...
                Order allow,deny

                Deny from all
                </Directory>

            2)、httpd-2.4:

                <Directory "/data/web/www">

                ...    

                Require all denied

                </Directory>

          (4)、控制页面资源允许部分来源的主机可访问:

            1)、httpd-2.2:

                <Directory "/data/web/www"> (记得把匹配范围小的写前面,下面表示拒绝192.168.10.14主机的访问允许192.168.10.0网段的主机访问)

                ...
                Order allow,deny

                Deny from 192.168.10.14

                Allow from 192.168.10

                Deny from all (可以省略)
                </Directory>

            2)、httpd-2.4

                <Directory "/data/web/www">
                  <Requireall>
                    Require not ip 192.168.10.13
                    Require ip 192.168.10
                  </Requireall>
                </Directory>

          (5)、Options,我们网页资源被访问时万一不存在主页文件会怎么办呢?他其实默认会把所有索引文件给你列出来,比如我们想使用一个web服务器做一个下载站,我们能看到所有资源点击即可下载

            1)、后跟一个或多个以空白字符分隔的“选项”列表

              Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户。

              FollowSymLinks:允许跟踪符号链接文件所指向的源文件

              None:

              ALL:启用除了MultiViews之外所有选项 

            2)、Indexes示例1

              我们首先修改相应配置文件后然后创建/data/web/www/images目录并复制几个图片至该目录下。我们可以看到配置文件中我们有Options Indexes FollowSymLinks这一行,当这一行存在的时候我们就可以通过访问链接http://192.168.10.13/images/访问到我们的图片

    [root@www www]#  cat /etc/httpd/conf/httpd.conf |grep -A3 'Directory "/data/web/www/"'
    <Directory "/data/web/www/">
        Options Indexes FollowSymLinks
        Require all granted
    </Directory>
    [root@www ~]# mkdir /data/web/www/images
    [root@www ~]# find /usr/share/ -iname "*.jpg" -exec cp {} /data/web/www/images/ ;
    [root@www ~]# ls /data/web/www/images/
    2560x1600.jpg  background.jpg  day.jpg  default.jpg  morning.jpg  night.jpg

              然后我们在配置文件中干掉Indexes这个关键词然后重启服务,然后我们再次访问http://192.168.10.13/images/时会显示没有权限,但是可以指定到某一个具体的文件进行访问,比如http://192.168.10.13/images/index.html

    [root@www www]#  cat /etc/httpd/conf/httpd.conf |grep -A3 'Directory "/data/web/www/"'
    <Directory "/data/web/www/">
        Options  FollowSymLinks
        Require all granted
    </Directory>
    [root@www www]# systemctl restart httpd

            3)、FollowSymLinks示例

              首先我们创建/etc/fstab文件的软连接至/data/web/www/fstab.html

    [root@www www]# ln -sv /etc/fstab /data/web/www/fstab.html
    "/data/web/www/fstab.html" -> "/etc/fstab"

              我们fstab.html是一个软连接,也就是说其内容没有在我们的网站内。如果允许的话就使用FollowSymLinks这个选项,不允许的话就干掉这个选项。现在我们干掉我们Options中的FollowSymLinks这个关键词。然后将后面替换为None,重启服务后发现这个文件由原来的可访问变为不可访问了。

    [root@www images]# cat /etc/httpd/conf/httpd.conf |grep -A3 'Directory "/data/web/www/"'
    <Directory "/data/web/www/">
        Options None 
        Require all granted
    </Directory>
    [root@www images]# systemctl restart httpd

        c、httpd-2.4示例

          (1)、我们前面已经修改了对应的DocumentRoot内容

    [root@www ~]# cat /etc/httpd/conf/httpd.conf |grep -E "^Docu"
    DocumentRoot "/data/web/www"

          (2)、现在我们来定义访问权限

    [root@www ~]# cat /etc/httpd/conf/httpd.conf |grep -A2 '<Directory "/data/web/www">'
    <Directory "/data/web/www">
        Require all granted  #表示允许所有人访问
    </Directory>

          (3)、我们重启服务后访问,可以看到能够访问了(httpd-2.2不定义权限就可以被访问,而httpd-2.4访问权限是严格限制的,不定义权限就无法访问)

    [root@www ~]# systemctl restart httpd
    [root@www ~]# curl 192.168.10.13
    Main Server

          (4)、现在我们重新定义为允许192.168.10.0网段进行访问,拒绝192.168.10.13这个IP访问。

              1)、我们修改相应的配置文件并重启服务,然后通过192.168.10.13和192.168.10.14进行访问,可以发现13访问被拒绝而14访问就可以

    [root@www ~]# cat /etc/httpd/conf/httpd.conf |grep -A5 'Directory "/data/web/www"'
    <Directory "/data/web/www">
        <Requireall>
            Require not ip 192.168.10.13
            Require ip 192.168.10
        </Requireall>
    </Directory>
    [root@www ~]# systemctl restart httpd
    [root@www ~]# curl 192.168.10.13
    ....#显示拒绝访问

              2)、通过192.168.10.14访问发现可以

    [root@node2 ~]# curl 192.168.10.13
    Main Server

        d、定义站点主页面

          (1)、我们访问一个URL时如果没给路径那么就会根据DirectoryIndex这个指令先找index.html文件

            DirectoryIndex  index.html 

    [root@www images]# cat /etc/httpd/conf/httpd.conf |grep -C1 
    <IfModule dir_module>
        DirectoryIndex index.html
    </IfModule>

        e、定义路径别名

          (1)、格式:

            Alias  /URL/  "/PATH/TO/SOMEDIR/"

          (2)、DocumentRoot  "/www/htdocs"

                http://www.wohaoshuai.com/download/bash-4.4.2-3.el6.x86_64.rpm

                  /www/htdocs/download/bash-4.4.2-3.el6.x86_64.rpm

          (3)、Alias  /download/  "/rpms/pub/"

                http://www.wohaoshuai.com/download/bash-4.4.2-3.el6.x86_64.rpm

                  /rpms/pub/bash-4.4.2-3.el6.x86_64.rpm

                http://www.wohaoshuai.com/images/logo.png

                  /www/htdocs/images/logo.png

          (4)、示例

            我们在配置文件中添加相应的Alias内容,访问 /images/时就定向到/usr/share/backgrounds/路径下。此处我们的Options参数为Options Indexes FollowSymLinks。

    [root@www images]# find /usr/share/ -iname "*.jpg"
    /usr/share/backgrounds/morning.jpg
    /usr/share/backgrounds/night.jpg
    /usr/share/backgrounds/day.jpg
    /usr/share/backgrounds/default.jpg
    [root@www images]# cat /etc/httpd/conf/httpd.conf |grep -C1 "Alias"
    <IfModule alias_module>
        Alias /images/  "/usr/share/backgrounds/"
    </IfModule>

            httpd-2.4中我们还需要配置/usr/share/backgrounds/的访问授权。整个配置文件如下

    [root@www images]# cat /etc/httpd/conf/httpd.conf |grep -EA15 "^DocumentRoot"
    DocumentRoot "/data/web/www/"
    <Directory "/data/web/www/">
        AllowOverride None
        Options Indexes FollowSymLinks
        Require all granted
    </Directory>
    
    <Directory "/usr/share/backgrounds/">
        AllowOverride None
        Options Indexes FollowSymLinks
        Require all granted
    </Directory>
    
    <IfModule alias_module>
        Alias /images/  "/usr/share/backgrounds/"
    </IfModule>

            然后我们访问192.168.10.13/images/访问的资源就在服务器的/usr/share/backgrounds/目录下了。我们注释掉Alias行的话再次访问192.168.10.13/images/就不会被重定向了。

        f、设定默认字符集

          (1)、AddDefaultCharset UTF-8

          (2)、中文字符集:GBK,GB2312,GB1830

        g、日志设定

          (1)、日志类型:访问日志和错误日志

          (2)、错误日志:

            1)、ErrorLog  logs/error_log :日志路径,这个logs目录是相对于ServerRoot(/etc/httpd)而言的路径,其实/etc/httpd/logs这个路径是软连接到/var/log/httpd的

    [root@www images]# ls -ld /etc/httpd/*
    drwxr-xr-x. 2 root root  76 5月  14 15:37 /etc/httpd/conf
    drwxr-xr-x. 2 root root 178 5月  13 15:06 /etc/httpd/conf.d
    drwxr-xr-x. 2 root root 186 5月  13 17:27 /etc/httpd/conf.modules.d
    lrwxrwxrwx  1 root root  19 10月 21 2019 /etc/httpd/logs -> ../../var/log/httpd
    lrwxrwxrwx  1 root root  29 10月 21 2019 /etc/httpd/modules -> ../../usr/lib64/httpd/modules
    lrwxrwxrwx  1 root root  10 10月 21 2019 /etc/httpd/run -> /run/httpd
    [root@www images]# cat /etc/httpd/conf/httpd.conf |grep -E "^Error"
    ErrorLog "logs/error_log"

            2)、LogLevel warn:日志等级

              Possible values include:debug,info,notice,warn,error,crit,alert,emerg

    [root@www images]# cat /etc/httpd/conf/httpd.conf |grep -E "^LogLevel"
    LogLevel warn

          (3)、访问日志:宏(变量)

    [root@www images]# cat /etc/httpd/conf/httpd.conf |grep -A28 "log_config_module" |grep -Ev "^$|[[:space:]]*#"
    <IfModule log_config_module>
        LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined #定义了combined格式
        LogFormat "%h %l %u %t "%r" %>s %b" common #定义了common格式
        <IfModule logio_module>
          LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedio
        </IfModule>
        CustomLog "logs/access_log" combined #指定日志文件格式为combined,即上面的combined格式,如果要用上面定义的common就写成common即可。
    </IfModule>

            1)、LogFormat  "%h   %l  %u    %t    "%r"   %b    "%{Referer}i"   "%{User-Agent}i" " commbined。日志是靠LogFormat定义的

            2)、CustomLog   logs/access_log   combined:指定日志文件格式,格式由1)中LogFormat定义。

            3)、LogFormat   format   strings:

              http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats

              %h:客户端IP地址。

              %l:Remote User,即远程登录的用户的名字,通常为一个减号("-")

              %u:如果我们远程服务器允许客户端登录,要求登录的时候使用用户名,如果登录了那么记录登录的用户名。这种登陆是认证登陆。如果没有做认证的话,也记录为横线。

              %t:登陆时的时间

               "%r":转义表示显示引号自身。r表示请求报文的首行,包含请求的方法,请求的URL和请求的协议版本

                

               %s:状态码,%>s记录的是最后一次的响应码,意思是我们去请求一个资源告诉服务器然后服务器说这个资源搬到别处去了,你去请求新的。因此记录下来的就是客户端向新资源发送请求的响应码。而不是第一次发请求的响应码。

              % b:响应报文的大小,单位是字节,但不包括响应报文的http首部,如果大小为0时也用减号表示"-"

              %{Referer}i:请求报文中首部“referer”的值,即从哪个页面中的超链接跳转至当前页面的。

              %{User-Agent}i:请求报文中首部“User-Agent”的值,即发出请求的应用程序。即获取浏览器类型。

    [root@www images]# tail -2 /var/log/httpd/access_log
    192.168.10.200 - - [14/May/2020:15:19:33 +0800] "GET /images/ HTTP/1.1" 200 2388 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Sa
    fari/537.36"192.168.10.200 - - [14/May/2020:15:20:50 +0800] "GET /images/ HTTP/1.1" 200 17 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safa
    ri/537.36"
  • 相关阅读:
    软件开发过程中应当遵守的原则
    企业架构是什么?
    软件工程的精髓
    PHP获取自然周列表,周开始结束日期,月开始结束时间方法类
    JS替换textarea里的回车换行
    PHP连接PGSQL
    PT与PX的区别
    JQuery 翻页小记
    PHP中替换textarea时的回车换行
    HTML5 CanvasOreilly.Canvas.Pocket.Reference (4)
  • 原文地址:https://www.cnblogs.com/Presley-lpc/p/12886439.html
Copyright © 2011-2022 走看看