[关闭]
@mrz1 2018-02-01T12:32:17.000000Z 字数 4228 阅读 1179

Apache主配置文件httpd.conf 详解

配置文件


显示服务器版本信息 44行

  1. ServerTokens OS //默认
  1. [root@centos7 ~]#curl -I 172.18.103.167
  2. HTTP/1.1 403 Forbidden
  3. Date: Sat, 27 Jan 2018 06:06:14 GMT
  4. Server: Apache/2.2.15 (CentOS) //服务器版本非常仔细
  5. Accept-Ranges: bytes
  6. Content-Length: 4961
  7. Connection: close
  8. Content-Type: text/html; charset=UTF-8
  1. ServerTokens prod //修改后
  1. [root@centos7 ~]#curl -I 172.18.103.167
  2. HTTP/1.1 403 Forbidden
  3. Date: Sat, 27 Jan 2018 06:07:06 GMT
  4. Server: Apache //这里显示用什么服务器不会显示版本
  5. Accept-Ranges: bytes
  6. Content-Length: 4961
  7. Connection: close
  8. Content-Type: text/html; charset=UTF-8

修改监听的IP和Port 136行

  1. 绑定到特定的IP地址和/或端口
  2. (1) 省略IP表示为本机所有IP
  3. (2) Listen指令至少一个,可重复出现多次
  4. Listen 80
  5. Listen 8080
  6. 示例:
  7. Listen 192.168.1.100:8080
  8. Listen 80

持久连接

  1. 连接建立,每个资源获取完成后不会断开连接,而是继续等待其它的请求完成,默认关闭持久连接
  2. 断开条件:数量限制:100
  3. 时间限制:以秒为单位,
  4. httpd-2.4 支持毫秒级
  5. 副作用:对并发访问量较大的服务器,持久连接功能会使用有些请求得不到响应
  6. 折衷:使用较短的持久连接
  7. 设置:
  8. KeepAlive On|Off //开关
  9. KeepAliveTimeout 15 //超时是时间15秒
  10. MaxKeepAliveRequests 100 //最大连接请求
  11. 测试:
  12. telnet IP port(端口)
  13. GET /URL(路径) HTTP/1.1
  14. host: ip(随便写)

MPM(Multi-Processing Module)多路处理模块

  1. prefork, worker, event(测试阶段)
  2. httpd-2.2不支持同时编译多个模块,所以只能编译时选定一个;rpm安装的包提供三个二进制程序文件,分别用于实现对不同MPM机制的支持
  3. 确认方法:ps aux | grep httpd
  4. 默认为/usr/sbin/httpd, prefork模式
  5. 查看模块列表
  6. 查看静态编译的模块
  7. httpd -l
  8. 查看静态编译及动态装载的模块
  9. httpd M
  10. 动态模块加载:不需重启即生效
  11. 动态模块路径/usr/lib64/httpd/modules/
  12. 更换使用的httpd程序:
  13. /etc/sysconfig/httpd
  14. HTTPD=/usr/sbin/httpd.worker
  15. 重启服务生效
  16. pstree -p|grep httpd查看进程和线程
  17. Httpd2.4与之不同
  18. 以动态模块方式提供
  19. 配置文件:/etc/httpd/conf.modules.d/00-mpm.conf
  20. httpd M |grep mpm 重启服务生效
  21. pstree -p|grep httpd 查看进程和线程

prefork的默认配置

  1. <IfModule prefork.c>
  2. StartServers 8
  3. MinSpareServers 5
  4. MaxSpareServers 20
  5. ServerLimit 256 最多进程数,最大20000
  6. MaxClients 256 最大并发
  7. MaxRequestsPerChild 4000 子进程最多能处理的请求数量。在处理MaxRequestsPerChild 个请求之后,子进程将会被父进程终止,这时候子进程占用的内存就会释放(为0时永远不释放)
  8. </IfModule>

worker的默认配置

  1. <IfModule worker.c>
  2. StartServers 4 //进程数
  3. MaxClients 300 //最大连接数
  4. MinSpareThreads 25 //最小的空闲线程
  5. MaxSpareThreads 75 //最大的空闲线程
  6. ThreadsPerChild 25 //每个进程带有的线程数
  7. MaxRequestsPerChild 0 //无限制
  8. </IfModule>

DSO:Dynamic Shared Object

  1. 加载动态模块配置
  2. /etc/httpd/conf/httpd.conf
  3. 配置指定实现模块加载格式:
  4. LoadModule <mod_name> <mod_path>
  5. 模块文件路径可使用相对路径:
  6. 相对于ServerRoot(默认/etc/httpd
  7. 示例:
  8. [root@centos6 ~]#cat //etc/httpd/conf/httpd.conf
  9. // 在150行后.so后缀的都是动态模块
  10. LoadModule auth_basic_module modules/mod_auth_basic.so
  11. LoadModule auth_digest_module modules/mod_auth_digest.so
  12. ··········

定义'Main' server的文档页面路径

  1. DocumentRoot "/path" //页面路径 292行
  2. 文档路径映射:
  3. DocumentRoot指向的路径为URL路径的起始位置
  4. 默认:
  5. DocumentRoot "/var/www/html" 网站默认路径
  6. http://HOST:PORT/index.html
  7. --> /var/www/html/index.html
  8. 示例:
  9. DocumentRoot "/app/data"
  10. http://HOST:PORT/index.html
  11. --> /app/data/index.html
  12. 注意:
  13. SELinuxiptables的状态
  14. 不写路径只有ip默认寻找的是index.html文件,如果不存在会调用/etc/httpd/conf.d/welcome.conf 这里面配置的默认文件(默认的文件会暴露服务器版本 所以welcome.conf修改名字任意 找不到此文件就会403
  15. 定义站点主页面
  16. DirectoryIndex index.html index.html.var //402行
  17. 定义了默认找index.html文件或者添加别的文件找不到前面就访问后面文件

站点访问控制常见机制

  1. 可基于两种机制指明对哪些资源进行何种访问控制
  2. 访问控制机制有两种:客户端来源地址,用户账号
  3. 文件系统路径
  4. <Directory "/path">
  5. ...文件夹
  6. </Directory>
  7. <File "/path/file">
  8. ...文件
  9. </File>
  10. <FileMatch "PATTERN">
  11. ...正则
  12. </FileMatch>
  13. URL路径:
  14. <Location "">
  15. ...
  16. </Location>
  17. <LocationMatch "">
  18. ...
  19. </LocationMatch>
  20. 示例:
  21. <FilesMatch "\.(gif|jpe?g|png)$">
  22. <Files "?at.*"> 通配符
  23. <Location /status>
  24. <LocationMatch "/(extra|special)/data">

压力测试

  1. 安装: yum install httpd-tools
  2. 命令:
  3. ab -c 2000 -n 10000 http://172.18.103.167/index.html
  4. -c 并发2000
  5. -n 最多10000
  6. ulimit -a 显示当前的各种用户进程限制
  7. [root@centos6 html]#ab -c 2000 -n 10000 http://172.18.103.167/index.html
  8. This is ApacheBench, Version 2.3 <$Revision: 655654 $>
  9. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  10. Licensed to The Apache Software Foundation, http://www.apache.org/
  11. Benchmarking 172.18.103.167 (be patient)
  12. Completed 1000 requests
  13. Completed 2000 requests
  14. Completed 3000 requests
  15. Completed 4000 requests
  16. Completed 5000 requests
  17. Completed 6000 requests
  18. Completed 7000 requests
  19. Completed 8000 requests
  20. Completed 9000 requests
  21. Completed 10000 requests
  22. Finished 10000 requests
  23. Server Software: Apache
  24. Server Hostname: 172.18.103.167
  25. Server Port: 80
  26. Document Path: /index.html
  27. Document Length: 11 bytes
  28. Concurrency Level: 2000
  29. Time taken for tests: 3.059 seconds
  30. Complete requests: 10000
  31. Failed requests: 0
  32. Write errors: 0
  33. Total transferred: 2621834 bytes
  34. HTML transferred: 110077 bytes
  35. Requests per second: 3269.55 [#/sec] (mean)
  36. Time per request: 611.705 [ms] (mean)
  37. Time per request: 0.306 [ms] (mean, across all concurrent requests)
  38. Transfer rate: 837.13 [Kbytes/sec] received
  39. Connection Times (ms)
  40. min mean[+/-sd] median max
  41. Connect: 0 80 356.2 1 3014
  42. Processing: 0 127 422.8 16 1946
  43. Waiting: 0 126 422.8 16 1946
  44. Total: 12 207 676.6 17 3044
  45. Percentage of the requests served within a certain time (ms)
  46. 50% 17
  47. 66% 17
  48. 75% 18
  49. 80% 21
  50. 90% 59
  51. 95% 2865
  52. 98% 2923
  53. 99% 2947
  54. 100% 3044 (longest request)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注