服务器程序
- Linux:apache/nginx/lighttpd
-
apache
介绍
- 高度模块化:核心模块+扩展模块
- DSO:Dynamic Shared Object 动态加载/卸载
- MPM:多路处理模块
- 虚拟主机:/ip/port/fdqn
- CGI:同用网关接口
- 反向代理
- 负载均衡
- 路径别名
- 双向认证
- 支持第三方模块
- MPM工作模式
- prefork:多进程IO模型,一个主进程,管理多个子进程,一个子进程处理一个请求
- worker:服用的多进程IO模型,多进程多线程,一个主进程管理多个子进程。一个子进程管理多个线程,每个线程处理一个请求。
- event:事件驱动模型,一个主进程,管理多个子进程,一个进程处理多个请求。
- 配置文件说明
- /etc/httpd/:主配置文件目录
- /etc/httpd/conf/httpd.conf:服务配置文件
- /etc/httpd/conf.d/:服务配置目录(模块化)
- /etc/httpd/conf.modules.d/:模块配置目录
- /etc/sysconfig/httpd:守护进程配置文件
- /usr/lib64/httpd/modules/:可用模块
- /usr/sbin/:相关命令目录
- /var/log/httpd/:日志目录
- /var/www/:站点目录
服务配置文件
主配置说明
[root@node3 ~]# grep “^[^ #]” /etc/httpd/conf/httpd.conf
ServerRoot “/etc/httpd” # 服务器的根
Listen 80 # 监听的端口
Include conf.modules.d/.conf # 包含模块
User apache # 用户
Group apache # 属组
ServerAdmin root@localhost # 服务器管理员
DocumentRoot “/var/www/html”
ErrorLog “logs/error_log” # 错误日志
LogLevel warn # 日志等级
EnableSendfile on # 开启
IncludeOptional conf.d/.conf # 虚拟服务器配置文件
说明:<></>此类称之为容器,针对某个容器做配置多端口支持
[root@node3 conf.d]# echo "Listen 8080" > /etc/httpd/conf.d/Listen.conf[root@node3 conf.d]# systemctl restart httpd[root@node1 ~]# curl ‐I 192.168.0.142:8080HTTP/1.1 200 OK Date: Thu, 21 Mar 2019 14:53:16 GMTServer: Apache/2.4.6 (CentOS)Last‐Modified: Thu, 21 Mar 2019 13:56:08 GMTETag: "c‐5849b199cf733"
持久连接
- KeepAlive默认是on,时间是5秒。
- KeepAliveTimeout # 连接超时
- MaxKeepAliveRequests # 最大保持连接请求
[root@node3 conf.d]# cat /etc/httpd/conf.d/keeplive.confKeepAlive onKeepAliveTimeout 30MaxKeepAliveRequests 100[root@node3 conf.d]# systemctl restart httpd测试:[root@node1 ~]# telnet 192.168.0.142 80
多路处理模块
[root@node1 ~]# cat /etc/httpd/conf.modules.d/00‐mpm.conf | grep ‐Ev "^#|^$"LoadModule mpm_prefork_module modules/mod_mpm_prefork.so默认参数:StartServers 5 # 服务启动时的进程数MaxSpareServers 10 # 最大空闲服务进程数MinSpareServers 5 # 最小空闲进程数MaxRequestWorkers 256 # 单个进程最多接受的进程数[root@node3 conf.d]# ps aux | grep httpd压力测试:[root@node1 ~]# ab ‐c 100 ‐n 1000 http://192.168.0.142/index.html若要使用worker和event工作模型,只需要在/etc/httpd/conf.modules.d/00-mpm.conf中取消注释即可
访问控制机制 ```bash 定义服务器的文档的页面路径: DocumentRoot “/data/www/html”
[root@node3 ~]# echo “this path /data/wwww/html” > /data/www/html/index.html
[root@node3 ~]# systemctl restart httpd
[root@node1 ~]# curl ‐I 192.168.0.142 HTTP/1.1 403 Forbidden
必须开发此文件夹的权限才可以访问:
Require all granted
[root@node1 ~]# curl ‐I 192.168.0.142
HTTP/1.1 200 OK
Require all granted 给所有授权
Require all denied 给所有拒绝
Require ip IP 允许某个IP访问(RequireAll)
Require no ip IP 拒绝某个IP访问(RequireAll)
Require user user1 允许某个用户访问
Require group group1 拒绝某个用户访问
Options配置参数:
Indexes:如果不存在index.html是显示索引
FollowSymLinks:允许软链接访问
AllowOverride None
- 用户访问控制认证方式有:basic和digest```bash创建用户认证文件:[root@node3 ~]# htpasswd ‐c ‐m /etc/httpd/conf.d/.htpassword zhaohaoNew password:Re‐type new password:Adding password for user zhaohao[root@node3 ~]# htpasswd ‐b ‐m /etc/httpd/conf.d/.htpassword zhangsan zhangsanAdding password for user zhangsan通过认证用户文件:<Directory "/data/www/html">AuthType BasicAuthName "Restricted Resource"AuthBasicProvider fileAuthUserFile /etc/httpd/conf.d/.htpasswordRequire user zhaohao</Directory通过认证组文件:<Directory "/data/www/html">AuthType BasicAuthName "Restricted Resource"AuthBasicProvider fileAuthUserFile /etc/httpd/conf.d/.htpasswordAuthGroupFile /etc/httpd/conf.d/.htgroupRequire group group1</Directory>使用浏览器访问测试即可!
- 日志设定
```bash
ErrorLog “logs/error_log”
LogLevel warn
LogFormat “%h %l %u %t \”%r\” %>s %b” commonLogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User‐Agent}i\"" combined
combinedioLogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User‐Agent}i\" %I %O"
CustomLog “logs/access_log” combined</IfModule>
参数: %h Remote hostname 客户端主机名 %l Remote logname 远程日志名 %u Remote user 远程用户名 %t Time the request was received, in the format [18/Sep/2011:19:18:28 ‐0400] 访问时间戳 %r First line of request 请求报文第一行 %s Status 记录状态码 %b Size of response in bytes, excluding HTTP headers 响应报文的长度 Referer 有利于分析用户是通过哪个网站转发的如通过baidu转发的,也可以监控网站盗链的发生。
User‐Agent 记录浏览器的类型。防止爬虫一定程度上,爬虫可以伪造浏览器类型。curl ‐A “evan”
http://I(伪造名字叫evan的浏览器)
- 路径别名
```bash
Alias /image /ftp/pub/image
<Directory /ftp/pub/image>
Require all granted
</Directory>
- 虚拟主机
- 基于IP地址
- 基于端口
- 基于FQDN
```bash
基于IP地址:
[root@node3 data]# cat /etc/httpd/conf.d/site.conf
Require all granted
Servername www.site1.com
DocumentRoot “/data/site1/“
Servername www.site2.com
DocumentRoot “/data/site2/“
[root@node1 ~]# curl 192.168.0.140This is site1
[root@node1 ~]# curl 192.168.0.145This is site2
基于端口: [root@node3 data]# cat /etc/httpd/conf.d/site.conf
Require all granted
Servername www.site1.com
DocumentRoot “/data/site1/“ [root@node1 ~]# curl 192.168.0.142:80
Servername www.site2.com
DocumentRoot “/data/site2/“This is site1
[root@node1 ~]# curl 192.168.0.142:8080This is site2
基于RQDN:
[root@node3 data]# cat /etc/httpd/conf.d/site.conf
Require all granted
Servername www.site1.com
DocumentRoot “/data/site1/“ [root@node1 ~]# cat /etc/hosts
Servername www.site2.com
DocumentRoot “/data/site2/“
192.168.0.142 www.site1.com
192.168.0.142 www.site2.com
[root@node1 ~]# curl www.site1.comThis is site1
[root@node1 ~]# curl www.site2.comThis is site2
```
