您的位置:首页 > 理论基础 > 计算机网络

Linux学习之路--http-2基础设置及访问限制【15】---20180108

2018-01-28 15:57 281 查看
一、HTTP软件

1、http服务器应用
http服务器程序
httpd apache
nginx
lighttpd

应用程序服务器
IIS .asp
tomcat .jsp
jetty 开源的servlet容器,基于Java的web容器
Resin CAUCHO公司,支持servlets和jsp的引擎
webshpere(IBM), weblogic(BEA), jboss,oc4j(Oracle)

市场占有率统计
www.netcraft.

2、httpd介绍
httpd(主程序名字)
20世纪90年代初,国家超级计算机应用中心NCSA开发
1995年开源社区发布apache(名字简称 a patchy server)
ASF: Apache Software Foundation(特意成立的基金会)
FSF:Free Software Foundation(自由软件基金会)

特性:
高度模块化:core + modules (模块开发模式,内核加模块)
DSO: Dynamic Shared Object 动态加/卸载
MPM:multi-processing module多路处理模块

3、MPM工作模式
prefork:多进程I/O模型,每个进程响应一个请求,默认模型
一个主进程:生成和回收n个子进程,创建套接字,不响应请求
多个子进程:工作work进程,每个子进程处理一个请求;系统初始时,预先生成多个空闲进程,等待请求,最大不超过1024个

worker:复用的多进程I/O模型,多进程多线程,IIS使用此模型
一个主进程:生成m个子进程,每个子进程负责生个n个线程,每个线程响应一个请求,并发响应请求:m*n

event:事件驱动模型(worker模型的变种)
一个主进程:生成m个子进程,每个进程直接响应n个请求,并发响应请求:m*n,有专门的线程来管理这些keep-alive类型的线程,当有真实请求时,将请求传递给服务线程,执行完毕后,又允许释放。这样增强了高并发场景下的请求处理能力
httpd-2.2: event 测试版,centos6默认
httpd-2.4:event 稳定版,centos7默认

[root@Centos6-serverhtml]#ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3808
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 3808
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited








4、进程角色
监听 --> 工作 --> 队列空闲




5、httpd功能特性
虚拟主机(通常一个网站在一个服务器上,但如果网络访问量不大,可以在一个服务器上运行多个网站,就叫虚拟主机)
具体实现通过修改下列三项进行建设虚拟主机
IP、Port、FQDN

CGI:Common Gateway Interface,通用网关接口
httpd处理不了的事物可以转发给能处理的机器上去,就是通过通用网关接口完成的

反向代理
根据用户的请求,把用户调度到相应的服务器上

负载均衡

路径别名

丰富的用户认证机制
basic -->不加密认证
digest -->加密认证,但兼容性较差,有部分浏览器不支持

支持第三方模块

二、Httpd安装

1、安装方法
版本
CentOS 6: 2.2
CentOS 7: 2.4

安装方式:
rpm:centos发行版,稳定,建议使用
编译:定制或特殊需求

CentOS 6程序信息:
httpd-2.2
配置文件位置:
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
检查配置语法:
httpd –t
service httpd configtest

2、CentOS 6 httpd程序环境
服务脚本(启动脚本):/etc/rc.d/init.d/httpd
脚本配置文件:/etc/sysconfig/httpd

服务控制和启动:
chkconfig httpd on|off
service {start|stop|restart|status|configtest|reload} httpd

站点网页文档根目录:
/var/www/html

模块文件路径:
/etc/httpd/modules
/usr/lib64/httpd/modules

主程序文件(MPM模式文件):
/usr/sbin/httpd(默认模式)
/usr/sbin/httpd.worker
/usr/sbin/httpd.event

主进程文件:
/etc/httpd/run/httpd.pid

日志文件目录:
/var/log/httpd
access_log: 访问日志
error_log:错误日志

帮助文档包:
httpd-manual

[root@Centos6-serverhtml]#ll /etc/httpd/
total 8
drwxr-xr-x 2 root root 4096 Jan 23 22:56 conf
drwxr-xr-x 2 root root 4096 Jan 23 22:56 conf.d
lrwxrwxrwx 1 root root   19 Jan 23 22:56 logs -> ../../var/log/httpd
lrwxrwxrwx 1 root root   29 Jan 23 22:56 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx 1 root root   19 Jan 23 22:56 run -> ../../var/run/httpd

[root@Centos6-serverhtml]#httpd -t
httpd: apr_sockaddr_info_get() failed for Centos6-server
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK

[root@Centos6-serverhtml]#cat /etc/httpd/run/httpd.pid
26226

[root@Centos6-serverhtml]#pstree -p
init(1)─┬─。。。。。
├─httpd(26226)─┬─httpd(26245)
│              ├─httpd(26246)
│              ├─httpd(26247)
│              ├─httpd(26248)
│              ├─httpd(26249)
│              ├─httpd(26250)
│              ├─httpd(26251)
│              └─httpd(26252)
。。。。。

[root@Centos6-serverhtml]#ps aux | grep httpd
root      26226  0.0  0.3 177464  3856 ?        Ss   Jan23   0:02 /usr/sbin/httpd
apache    26245  0.0  0.3 177600  3112 ?        S    Jan23   0:00 /usr/sbin/httpd
apache    26246  0.0  0.3 177600  3100 ?        S    Jan23   0:00 /usr/sbin/httpd
apache    26247  0.0  0.3 177600  3112 ?        S    Jan23   0:00 /usr/sbin/httpd
apache    26248  0.0  0.3 177600  3080 ?        S    Jan23   0:00 /usr/sbin/httpd
apache    26249  0.0  0.3 177600  3160 ?        S    Jan23   0:00 /usr/sbin/httpd
apache    26250  0.0  0.3 177600  3076 ?        S    Jan23   0:00 /usr/sbin/httpd
apache    26251  0.0  0.3 177600  3076 ?        S    Jan23   0:00 /usr/sbin/httpd
apache    26252  0.0  0.3 177600  3076 ?        S    Jan23   0:00 /usr/sbin/httpd
root      40387  0.0  0.0 103332   844 pts/0    S+   04:59   0:00 grep httpd

[root@Centos6-serverhtml]#yum install httpd-manual
[root@Centos6-serverhtml]#service httpd restart
#然后就可以登陆http://192.168.1.100/manual/ 查看帮助文档


三、Httpd 2.2常见配置

1、httpd配置文件的组成
# grep "Section" /etc/httpd/conf/httpd.conf
配置文件的主要内容分成三个部分
### Section 1: Global Environment --->全局环境
### Section 2: 'Main' server configuration --->主服务器设置
### Section 3: Virtual Hosts --->虚拟主机

配置格式:directive value --->类似于键值对或者前面是关键字|变量|指令后面是一个值
directive: 不区分字符大小写
value: 为路径时,是否区分大小写,取决于文件系统

[root@Centos6-serverhtml]#grep "Section" /etc/httpd/conf/httpd.conf
### Section 1: Global Environment
### Section 2: 'Main' server configuration
### Section 3: Virtual Hosts


2、显示服务器版本信息
ServerTokens Major|Minor|Min[imal]|Prod[uctOnly]|OS|Full

ServerTokens Prod[uctOnly]: Server: Apache

ServerTokens Major: Server: Apache/2

ServerTokens Minor: Server: Apache/2.0

ServerTokens Min[imal]: Server: Apache/2.0.41

ServerTokens OS: Server: Apache/2.0.41 (Unix)

ServerTokens Full (or not specified): Server: Apache/2.0.41 (Unix) PHP/4.2.2 MyMod/1.2

This setting applies to the entire server and cannot be enabled or disabled on a virtualhost-by-virtualhost basis.
#此设置适用于整个服务器,并且不能基于虚拟主机虚拟主机启用或禁用该设置

After version 2.0.44, this directive also controls the information presented by the ServerSignature directive.
#在版本2.0.44之后,该指令还控制由ServerSignature指令提供的信息。

建议使用:ServerTokens Prod

[root@Centos6-serverhtml]#vim /etc/httpd/conf/httpd.conf
ServerTokens Prod

[root@Centos6-serverhtml]#curl -I HTTP/1.1 200 OK
Date: Tue, 23 Jan 2018 21:32:02 GMT
Server: Apache

3、修改监听的IP和Port
Listen [IP:]PORT
(1) 省略IP表示为本机所有IP
(2) Listen指令至少一个,可重复出现多次
Listen 80
Listen 8080

示例:
Listen 192.168.1.100:8080 ---> 8080这个端口,只能用这个ip段访问
Lsten 80

[root@Centos6-serverhtml]#vim /etc/httpd/conf/httpd.conf
Listen 172.18.1.100:80

[root@Centos6-serverhtml]#curl -I curl: (7) couldn't connect to host

[root@Centos6-serverhtml]#curl -I http://172.18.1.100/test.html HTTP/1.1 200 OK
Date: Tue, 23 Jan 2018 21:38:22 GMT
Server: Apache
Last-Modified: Tue, 23 Jan 2018 14:58:06 GMT
ETag: "121db5-a9-56372c63490d1"
Accept-Ranges: bytes
Content-Length: 169
Connection: close
Content-Type: text/html; charset=UTF-8


4、持久连接
Persistent Connection:连接建立,每个资源获取完成后不会断开连接,而是继续等待其它的请求完成,默认关闭持久连接

断开条件:数量限制:100
时间限制:以秒为单位, httpd-2.4 支持毫秒级

副作用:对并发访问量较大的服务器,持久连接功能会使用有些请求得不到响应

折衷:使用较短的持久连接时间

设置:
KeepAlive On|Off
KeepAliveTimeout 15
MaxKeepAliveRequests 100

测试:
telnet WEB_SERVER_IP PORT
GET /URL HTTP/1.1
Host: WEB_SERVER_IP

[root@centos7mini~]#telnet 192.168.1.100 80                             --->默认是不支持持久连接的
Trying 192.168.1.100...
Connected to 192.168.1.100.
Escape character is '^]'.
GET /test.html HTTP/1.1
HOST: 192.168.1.101
HTTP/1.1 200 OK
Date: Tue, 23 Jan 2018 21:53:31 GMT
Server: Apache
。。。
</body>
</html>
Connection closed by foreign host.

[root@Centos6-serverhtml]#vim /etc/httpd/conf/httpd.conf
KeepAlive On

MaxKeepAliveRequests 100
KeepAliveTimeout 150

[root@centos7mini~]#telnet 192.168.1.100 80
Trying 192.168.1.100...
Connected to 192.168.1.100.
Escape character is '^]'.
GET /test.html HTTP/1.1
HOST:192.168.1.101
HTTP/1.1 200 OK
Date: Tue, 23 Jan 2018 21:56:19 GMT
。。。
</body>
</html>
GET /test.txt HTTP/1.1
HOST:192.168.1.101
HTTP/1.1 200 OK
Date: Tue, 23 Jan 2018 21:56:52 GMT
。。。
</body>
</html>
q


5、MPM( Multi-Processing Module)多路处理模块
prefork, worker, event(试验阶段)

httpd-2.2不支持同时编译多个模块,所以只能编译时选定一个
rpm安装的包提供三个二进制程序文件,分别用于实现对不同MPM机制的支持

确认方法:
ps aux | grep httpd
默认为/usr/sbin/httpd, 即prefork模式

查看模块列表

查看静态编译的模块
httpd -l

查看静态编译及动态装载的模块
httpd –M

动态模块加载:不需重启即生效

动态模块路径
/usr/lib64/httpd/modules/

更换使用的httpd程序:

/etc/sysconfig/httpd
HTTPD=/usr/sbin/httpd.worker
重启服务生效
pstree -p|grep httpd 查看进程和线程

Httpd 2.4 与之不同
以动态模块方式提供
配置文件:/etc/httpd/conf.modules.d/00-mpm.conf
httpd –M |grep mpm
重启服务生效
pstree -p|grep httpd 查看进程和线程

prefork的默认配置:
<IfModule prefork.c>
StartServers 8 --->默认启动服务的子进程数量
MinSpareServers 5 --->最小的空闲线程
MaxSpareServers 20 --->最大的空闲线程
ServerLimit 256 --->最多进程数,最大20000
MaxClients 256 --->最大并发
MaxRequestsPerChild 4000 --->服务器进程提供的最大请求数
</IfModule>
#4000是子进程最多能处理的请求数量。在处理MaxRequestsPerChild 个请求之后,子进程将会被父进程终止,这时候子进程占用的内存就会释放(为0时永远不释放)

worker的默认配置:
<IfModule worker.c>
StartServers 4 --->默认启动服务的子进程数量
MaxClients 300 --->最大访问客户数量(最多支持300个客户端并发访问)
MinSpareThreads 25 --->最小的空闲线程
MaxSpareThreads 75 --->最大的空闲线程
ThreadsPerChild 25 --->每个子进程带的线程数量
MaxRequestsPerChild 0 无限制 --->服务器进程提供的最大请求数
</IfModule>

[root@Centos6-serverhtml]#httpd -M                      --->查看动态模块,可加载的模块
httpd: apr_sockaddr_info_get() failed for Centos6-server
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Loaded Modules:
core_module (static)
mpm_prefork_module (static)                            --->默认的就是Prefork模块
。。。。。

[root@Centos6-serverhtml]#httpd -l                      --->查看静态模块,编译进去的静态模块
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c

[root@Centos6-serverhtml]#vim /etc/sysconfig/httpd
HTTPD=/usr/sbin/httpd.worker
[root@Centos6-serverhtml]#service httpd restart
[root@Centos6-serverhtml]#ps aux
root      41486  0.0  0.4 177736  4068 ?        Ss   06:10   0:00 /usr/sbin/httpd.worker
apache    41488  0.0  0.5 521996  5348 ?        Sl   06:10   0:00 /usr/sbin/httpd.worker
apache    41489  0.0  0.5 587532  5368 ?        Sl   06:10   0:00 /usr/sbin/httpd.worker
apache    41490  0.0  0.5 521996  5368 ?        Sl   06:10   0:00 /usr/sbin/httpd.worker
apache    41491  0.0  0.5 521996  5352 ?        Sl   06:10   0:00 /usr/sbin/httpd.worker
。。。。

[root@Centos6-serverhtml]#pstree -p
init(1)─┬─NetworkManager(1649)─┬─dhclient(1690)
├─httpd.worker(41679)─┬─httpd.worker(41681)─┬─{httpd.worker}(41765)
。。。。。。
│                     │                     └─{httpd.worker}(41790)
│                     ├─httpd.worker(41682)─┬─{httpd.worker}(41713)
│                     │                     ├─{httpd.worker}(41714)
。。。。。。
│                     │                     └─{httpd.worker}(41738)
│                     ├─httpd.worker(41683)─┬─{httpd.worker}(41689)
。。。。。。
│                     │                     └─{httpd.worker}(41792)
│                     └─httpd.worker(41684)─┬─{httpd.worker}(41739)
。。。。。。
│                                           └─{httpd.worker}(41764)


压力测试r软件包

[root@Centos6-serverhtml]#yum install httpd-tools
[root@Centos6-serverhtml]#rpm -ql httpd-tools
/usr/bin/ab
/usr/bin/htdbm
/usr/bin/htdigest
/usr/bin/htpasswd
/usr/bin/logresolve
[root@Centos6-serverhtml]#ulimit -n 10240

[root@Centos6-serverhtml]#ulimit -a
。。。。。
open files                      (-n) 10240

[root@Centos6-serverhtml]#ab -c 2000 -n 10000          --->压力测试
\This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.1.100 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests

Server Software:        Apache
Server Hostname:        192.168.1.100
Server Port:            80
Document Path:          /test.txt
Document Length:        169 bytes
Concurrency Level:      2000
Time taken for tests:   5.609 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      4288374 bytes
HTML transferred:       1713322 bytes
Requests per second:    1782.80 [#/sec] (mean)
Time per request:       1121.832 [ms] (mean)
Time per request:       0.561 [ms] (mean, across all concurrent requests)
Transfer rate:          746.61 [Kbytes/sec] received
Connection Times (ms)
min  mean[+/-sd] median   max
Connect:        0  153 441.0     22    3082
Processing:     0  414 974.1     58    4209
Waiting:        0  401 977.3     45    4207
Total:         44  567 1300.4     84    5569
Percentage of the requests served within a certain time (ms)
50%     84
66%    114
75%    140
80%    165
90%   2418
95%   4411
98%   5054
99%   5377
100%   5569 (longest request)


6、DSO: Dynamic Shared Object
动态共享对象

加载动态模块配置
/etc/httpd/conf/httpd.conf
配置指定实现模块加载格式:
LoadModule <mod_name> <mod_path>
模块文件路径可使用相对路径:
相对于ServerRoot(默认/etc/httpd)

示例:
LoadModule auth_basic_module modules/mod_auth_basic.so

[root@Centos6-serverhtml]#vim /etc/httpd/conf/httpd.conf
#LoadModule auth_basic_module modules/mod_auth_basic.so
#LoadModule auth_digest_module modules/mod_auth_digest.so
。。。。。
#想不加载的模块就在前面加#注释掉即可


7、定义'Main' server的文档页面路径(主目录)

DocumentRoot “/path”
文档路径映射:
DocumentRoot指向的路径为URL路径的起始位置

示例:
DocumentRoot "/app/data“ http://HOST:PORT/test/index.html --> /app/data/test/index.html

注意:SELinux和iptables的状态

[root@Centos6-serverhtml]#mkdir /app/website

[root@Centos6-serverhtml]#vim /etc/httpd/conf/httpd.conf
DocumentRoot "/app/website"

[root@Centos6-serverhtml]#service httpd restart

[root@Centos6-serverhtml]#curl /app/website


8、定义站点主页面
DirectoryIndex index.html index.html.var

服务默认找文档页面路径下这两个格式的文件

[root@Centos6-serverconf.d]#mv welcome.conf welcome.conf.bak
这样默认找不到主页面,也不回去找默认的apache信息页面,就会报403错误页面

[root@Centos6-serverconf.d]#vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.html index.html.var


9、站点访问控制常见机制
可基于两种机制指明对哪些资源进行何种访问控制,访问控制机制有两种:
客户端来源地址
用户账号

文件系统路径:
<Directory “/path"> --->针对特定的目录
... --->需要是这里制定的ip或者用户才能访问
</Directory>
<File “/path/file”> --->或者针对文件控制
...
</File>
<FileMatch "PATTERN"> --->正则表达式控制
...
</FileMatch>

URL路径:
<Location ""> --->特定的URL路径,进行控制
...
</Location>
<LocationMatch "">
...
</LocationMatch>

示例:
<FilesMatch "\.(gif|jpe?g|png)$"> --->后缀为gif、jpeg、jpg、png的文件为匹配条件
<Files “?at.*”> 通配符
<Location /status>
<LocationMatch "/(extra|special)/data">

10、<Directory>中“基于源地址”实现访问控制
(1) Options:后跟1个或多个以空白字符分隔的选项列表
在选项前的+,- 表示增加或删除指定选项

常见选项:
Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户
FollowSymLinks:允许访问符号链接文件所指向的源文件
None:全部禁用
All: 全部允许

示例:
<Directory /web/docs>
Options Indexes FollowSymLinks --->在这个目录下支持索引列表
</Directory>
<Directory /web/docs/spec>
Options FollowSymLinks
</Directory>

<Directory /web/docs>
Options Indexes FollowSymLinks
</Directory>
<Directory /web/docs/spec>
Options +Includes -Indexes
</Directory>
(2) AllowOverride
与访问控制相关的哪些指令可以放在指定目录下的.htaccess(由AccessFileName指定)文件中,覆盖之前的配置指令
只对<directory>语句有效
AllowOverride All: 所有指令都有效
AllowOverride None:.htaccess 文件无效
AllowOverride AuthConfig Indexes 除了AuthConfig 和Indexes的其它指令都无法覆盖

(3) order和allow、deny
放在directory, .htaccess中
order:定义生效次序;写在后面的表示默认法则
Order allow,deny
Order deny,allow
Allow from和Deny from:定义客户端地址
客户端地址:
IP --->IP地址
网络: 172.16 --->网段模式
172.16.0.0
172.16.0.0/16
172.16.0.0/255.255.0.0
如果拒绝和允许冲突,以最后面的条件优先级高。

判断条件
allow、deny
deny、allow
allow only
allow
allow
deny only
deny
deny
allow deny
deny
allow
none
deny
allow
注意:设置条件多的,放在后面
示例:
<files "*.txt">
order deny,allow
deny from 172.16. 100.100
allow from 172.16
</files>

<files "*.txt">
order allow,deny
deny from 172.16.100.100
allow from 172.16
</files>

#实验 基于源地址目录访问控制
[root@Centos6-serverwebsite]#ls
index.html.bak
[root@Centos6-serverwebsite]#mv  index.html.bak index.html
[root@Centos6-serverwebsite]#mkdir optionsdir
[root@Centos6-serverwebsite]#echo /app/website/optionsdir > /app/website/optionsdir/index.html
[root@Centos6-serverwebsite]#curl /app/website/optionsdir

[root@Centos6-serveroptionsdir]#pwd
/app/website/optionsdir
[root@Centos6-serveroptionsdir]#ln -s /etc/fstab fstab
[root@Centos6-serveroptionsdir]#curl 。。。。。
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@Centos6-serveroptionsdir]#vim /etc/httpd/conf/httpd.conf
。。。。。
DirectoryIndex index.html index.html.var
<directory /app/website/optionsdir>
options -FollowSymLinks
</directory>

[root@Centos6-serveroptionsdir]#curl
<title>403 Forbidden</title>
。。。。。

[root@Centos6-serveroptionsdir]#vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.html index.html.var
<directory /app/website/optionsdir>
options Indexes -FollowSymLinks                         --->支持索引模式(不安全),但不支持软连接
</directory>

<directory /app/website/optionsdir>
options all
</directory>

[root@Centos6-serveroptionsdir]#vim /etc/httpd/conf/httpd.conf
ServerName www.lanyangyang.com:80                                   --->把名字启用,重启服务不会报乱码

#实验 基于.htaccess 文件控制
#需要先修改主配置文件,相当于授权
[root@Centos6-serveroptionsdir]#vim /etc/httpd/conf/httpd.conf
<directory /app/website/optionsdir>
AllowOverride All
</directory>

[root@Centos6-serveroptionsdir]#touch .htaccess
[root@Centos6-serveroptionsdir]#vim .htaccess
options -Followsymlinks Indexes
#实验 基于客户端的控制
[root@Centos6-serveroptionsdir]#vim /etc/httpd/conf/httpd.conf
<Directory /app/website/optionsdir>
order deny,allow
deny from 192.168.1.101
</Directory>
[root@centos7mini~]#curl http://192.168.1.100/optionsdir/            --->101的ip无法访问,403
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>

[root@centos6-mini~]#curl -I http://192.168.1.100/optionsdir/         --->其他的ip可以访问
HTTP/1.1 200 OK

#如果拒绝和允许冲突,以最后面的条件优先级高
[root@Centos6-serveroptionsdir]#vim /etc/httpd/conf/httpd.conf
<Directory /app/website/optionsdir>
order deny,allow
deny from 192.168.1.101
allow from 192.168.1.101
</Directory>

[root@centos7mini~]#curl http://192.168.1.100/optionsdir/ -I
HTTP/1.1 200 OK
#基于文件名后缀控制
[root@Centos6-serveroptionsdir]#vim /etc/httpd/conf/httpd.conf
<Files ~ "\.conf$">
order allow,deny                                              --->拒绝查看所有以.conf结尾的文件
deny from all
</Files>
[root@centos7mini~]#curl <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
。。。。

<Files ~ "\.conf$">                                                   --->拒绝查看所有以.conf结尾的文件
order allow,deny
allow from 192.168.1.101                                      --->之允许这个ip访问
</Files>








11、日志设定
日志类型:
访问日志
错误日志

错误日志:
ErrorLog logs/error_log
LogLevel warn
LogLevel 可选值:
debug, info, notice, warn,error
crit, alert, emerg

访问日志:
定义日志格式:LogFormat format strings
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
使用日志格式:
CustomLog logs/access_log combined
参考帮助:http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats
 %h 客户端IP地址
 %l 远程用户,启用mod_ident才有效,通常为减号“-”
%u 验证(basic,digest)远程用户,非登录访问时,为一个减号“-”
%t 服务器收到请求时的时间
%r First line of request,即表示请求报文的首行;记录了此次请求的“方法”,“URL”以及协议版本
%>s 响应状态码
%b 响应报文的大小,单位是字节;不包括响应报文http首部
%{Referer}i 请求报文中首部“referer”的值;即从哪个页面中的超链接跳转至当前页面的
%{User-Agent}i 请求报文中首部“User-Agent”的值;即发出请求的应用程序

[root@centos7mini~]#curl -v * About to connect() to 192.168.1.100 port 80 (#0)
*   Trying 192.168.1.100...
* Connected to 192.168.1.100 (192.168.1.100) port 80 (#0)
> GET /optionsdir/a.conf HTTP/1.1
> User-Agent: curl/7.29.0                                            ---> %{User-Agent}i
> Host: 192.168.1.100
> Accept: */*
>
< HTTP/1.1 200 OK                                                    ---> %>s 响应状态码
< Date: Wed, 24 Jan 2018 05:43:54 GMT
< Server: Apache
< Last-Modified: Wed, 24 Jan 2018 05:14:48 GMT
< ETag: "100006-1f-5637ebe0b0886"
< Accept-Ranges: bytes
< Content-Length: 31
< Connection: close
< Content-Type: text/plain; charset=UTF-8
<
/app/website/optionsdir/a.conf
* Closing connection 0

[root@Centos6-serveroptionsdir]#cat /var/log/httpd/access_log
192.168.1.101 - - [24/Jan/2018:13:43:54 +0800] "GET /optionsdir/a.conf HTTP/1.1" 200 31 "-" "curl/7.29.0"

#实验,设置日志抓取host信息
[root@Centos6-serveroptionsdir]#vim /etc/httpd/conf/httpd.conf
LogFormat "%{Host}i" testlog                                         --->设置日志的信息格式
CustomLog logs/access_log testlog                                    --->启用日志格式命令

[root@centos7mini~]#telnet 192.168.1.100 80
Trying 192.168.1.100...
Connected to 192.168.1.100.
Escape character is '^]'.
GET / http/1.1
HOST: 6.6.6.6

[root@Centos6-serveroptionsdir]#tail -f /var/log/httpd/access_log
6.6.6.6


12、设定默认字符集

AddDefaultCharset UTF-8

中文字符集:GBK, GB2312, GB18030

[root@centos7mini~]#curl -I HTTP/1.1 200 OK
Date: Sun, 28 Jan 2018 07:13:30 GMT
Content-Type: text/html; charset=gb2312

[root@Centos6-serveroptionsdir]#vim /etc/httpd/conf/httpd.conf
AddDefaultCharset UTF-8


13、定义路径别名
格式:
Alias /URL/ "/PATH/"
DocumentRoot "/www/htdocs" http://www.magedu.com/download/bash.rpm ==> /www/htdocs/download/bash.rpm http://www.magedu.com/images/logo.png ==> /www/htdocs/images/logo.png
Alias /download/ "/rpms/pub/" http://www.magedu.com/download/bash.rpm ==> /rpms/pub/bash.rpm

#实验: 别名设置
[root@Centos6-serverwebsite]#pwd
/app/website
[root@Centos6-serverwebsite]#mkdir forum
[root@Centos6-serverwebsite]#ls
forum  index.html  optionsdir
[root@Centos6-serverwebsite]#echo /app/website/forum/index.html > /app/website/forum/index.html

[root@centos7mini~]#curl /app/website/forum/index.html

[root@Centos6-serverwebsite]#mkdir /data/bss
[root@Centos6-serverwebsite]#echo /data/bbs/index.html > /data/bbs/index.html
[root@Centos6-serverwebsite]#vim /etc/httpd/conf/httpd.conf
alias /forum /data/bbs/

[root@centos7mini~]#curl /data/bbs/index.html

#实验: 模糊匹配

[root@Centos6-serverwebsite]#mkdir /data/images
[root@Centos6-serverwebsite]#cp /usr/share/wallpapers/The_Rings_of_Saturn/contents/images/1920x1200.jpg /data/images/pica.jpg

[root@Centos6-serverwebsite]#vim /etc/httpd/conf/httpd.conf
aliasmatch ^/imgs/(.*)?$ "/data/images/pic$1"

[root@centos7mini~]#curl -I HTTP/1.1 200 OK
[root@Centos6-serverwebsite]#vim /etc/httpd/conf/httpd.conf
aliasmatch ^(.*\.(jpe?g|gif|png))$ "/data/images/$1"
[root@centos7mini~]#curl -I  HTTP/1.1 200 OK
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息