服务器程序

  • Linux:apache/nginx/lighttpd
  • windows:IIS

    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 # 虚拟服务器配置文件
    说明:<></>此类称之为容器,针对某个容器做配置

  • 多端口支持

  1. [root@node3 conf.d]# echo "Listen 8080" > /etc/httpd/conf.d/Listen.conf
  2. [root@node3 conf.d]# systemctl restart httpd
  3. [root@node1 ~]# curl I 192.168.0.142:8080
  4. HTTP/1.1 200 OK Date: Thu, 21 Mar 2019 14:53:16 GMT
  5. Server: Apache/2.4.6 (CentOS)
  6. LastModified: Thu, 21 Mar 2019 13:56:08 GMT
  7. ETag: "c‐5849b199cf733"
  • 持久连接

    • KeepAlive默认是on,时间是5秒。
    • KeepAliveTimeout # 连接超时
    • MaxKeepAliveRequests # 最大保持连接请求
      1. [root@node3 conf.d]# cat /etc/httpd/conf.d/keeplive.conf
      2. KeepAlive on
      3. KeepAliveTimeout 30
      4. MaxKeepAliveRequests 100
      5. [root@node3 conf.d]# systemctl restart httpd
      6. 测试:
      7. [root@node1 ~]# telnet 192.168.0.142 80
  • 多路处理模块

    1. [root@node1 ~]# cat /etc/httpd/conf.modules.d/00mpm.conf | grep Ev "^#|^$"
    2. LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
    3. 默认参数:
    4. StartServers 5 # 服务启动时的进程数
    5. MaxSpareServers 10 # 最大空闲服务进程数
    6. MinSpareServers 5 # 最小空闲进程数
    7. MaxRequestWorkers 256 # 单个进程最多接受的进程数
    8. [root@node3 conf.d]# ps aux | grep httpd
    9. 压力测试:
    10. [root@node1 ~]# ab c 100 n 1000 http://192.168.0.142/index.html
    11. 若要使用workerevent工作模型,只需要在/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

  1. - 用户访问控制
  2. 认证方式有:basicdigest
  3. ```bash
  4. 创建用户认证文件:
  5. [root@node3 ~]# htpasswd ‐c ‐m /etc/httpd/conf.d/.htpassword zhaohao
  6. New password:
  7. Re‐type new password:
  8. Adding password for user zhaohao
  9. [root@node3 ~]# htpasswd ‐b ‐m /etc/httpd/conf.d/.htpassword zhangsan zhangsan
  10. Adding password for user zhangsan
  11. 通过认证用户文件:
  12. <Directory "/data/www/html">
  13. AuthType Basic
  14. AuthName "Restricted Resource"
  15. AuthBasicProvider file
  16. AuthUserFile /etc/httpd/conf.d/.htpassword
  17. Require user zhaohao
  18. </Directory
  19. 通过认证组文件:
  20. <Directory "/data/www/html">
  21. AuthType Basic
  22. AuthName "Restricted Resource"
  23. AuthBasicProvider file
  24. AuthUserFile /etc/httpd/conf.d/.htpassword
  25. AuthGroupFile /etc/httpd/conf.d/.htgroup
  26. Require group group1
  27. </Directory>
  28. 使用浏览器访问测试即可!
  • 日志设定 ```bash ErrorLog “logs/error_log”
    LogLevel warn
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User‐Agent}i\"" combined      
    
    LogFormat “%h %l %u %t \”%r\” %>s %b” common
          LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User‐Agent}i\" %I %O"  
    
    combinedio
      </IfModule>      
    
    CustomLog “logs/access_log” combined

    参数: %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.140

      This is site1


      [root@node1 ~]# curl 192.168.0.145

      This is site2

      基于端口: [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.142:80

      This is site1


      [root@node1 ~]# curl 192.168.0.142:8080

      This is site2

      基于RQDN:
      [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 ~]# cat /etc/hosts
      192.168.0.142 www.site1.com
      192.168.0.142 www.site2.com
      [root@node1 ~]# curl www.site1.com

      This is site1


      [root@node1 ~]# curl www.site2.com

      This is site2

```