HTTP 协议工作模型和 httpd 基础配置与应用

1380 字
7 分钟
HTTP 协议工作模型和 httpd 基础配置与应用

HTTP 协议工作模型和 httpd 基础配置与应用#

所属:第三篇 · HTTP 与高性能 Web 应用 | 建议时长:85 min | 系列:Linux 运维全栈学习(第 15 章)

← HTTP 与高性能 Web 应用:并发模型与性能全景 · 返回课程总目录 · Nginx 基础 →


一、这一章解决什么问题#

HTTP 是运维每天看的东西:日志里的状态码、curl -v 的输出、浏览器的 Network 面板。看懂报文结构,才能把「打不开」拆成「请求没发出 / 服务端不收 / 服务端出错 / 响应没回来」。

httpd(Apache)是理解这些概念的好载体:配置显式、模块清晰、日志规范。

1.1 学习目标#

  • 能手工读一条 HTTP 请求与响应报文,指出方法、路径、头、体
  • 能按状态码判断问题归属,而不是只说「500 了」
  • 能配置 Keep-Alive、压缩、缓存头、虚拟主机、目录访问控制
  • 能根据业务选 MPM 模式并调参
  • 能用访问日志做 TOP 分析

1.2 验收标准#

  • 一台 httpd 上跑两个域名,互不干扰
  • 关掉目录浏览、限制某目录只允许内网访问
  • 用 curl -I 验证压缩与缓存头生效

二、核心概念与原理#

2.1 报文结构#

请求:

GET /index.html HTTP/1.1
Host: ops.local
User-Agent: curl/8.5.0
Accept: */*
Connection: keep-alive

响应:

HTTP/1.1 200 OK
Server: Apache/2.4.57
Content-Type: text/html; charset=UTF-8
Content-Length: 1024
Cache-Control: max-age=3600
Connection: keep-alive
<!DOCTYPE html>...

方法:GET 取、HEAD 只要头、POST 提交、PUT 覆盖、DELETE 删除、OPTIONS 预检。

2.2 状态码分族#

段含义典型
1xx信息101 Switching Protocols(WebSocket)
2xx成功200、204 无内容、206 断点续传
3xx重定向301 永久、302 临时、304 未修改
4xx客户端错400、401 未认证、403 禁止、404 不存在、429 限流
5xx服务端错500、502、503、504

304 不是错误,它表示协商缓存命中,是性能优化的结果。

2.3 连接管理#

版本连接方式
HTTP/1.0默认短连接,需 Connection: keep-alive 才复用
HTTP/1.1默认长连接,需 Connection: close 才关闭
HTTP/2多路复用,一个连接跑多个请求

Keep-Alive 的收益:省掉 TCP + TLS 握手。代价:连接长期占用 worker,需配 KeepAliveTimeout 平衡。

2.4 MPM 三种模型#

MPM并发单元适用
prefork进程需要非线程安全模块(老 PHP)
worker线程(多进程多线程)通用
event事件驱动 + 线程现代默认,长连接场景最优
Terminal window
httpd -V | grep -i mpm # 查看当前编译默认
httpd -M | grep mpm # 查看已加载

2.5 缓存头语义#

头作用
Cache-Control: max-age=N强缓存 N 秒,期间不发请求
Cache-Control: no-cache可缓存但每次要协商
Cache-Control: no-store完全不缓存
ETag / Last-Modified协商缓存依据,命中返回 304
Expires老式绝对时间,被 Cache-Control 取代

三、命令与实操(跟做)#

3.1 安装与基本运行#

Terminal window
dnf install -y httpd
systemctl enable --now httpd
firewall-cmd --permanent --add-service=http && firewall-cmd --reload
curl -I http://127.0.0.1/

主要路径:

/etc/httpd/conf/httpd.conf 主配置
/etc/httpd/conf.d/*.conf 扩展配置(虚拟主机放这里)
/var/www/html/ 默认站点根
/var/log/httpd/access_log 访问日志
/var/log/httpd/error_log 错误日志

3.2 虚拟主机#

/etc/httpd/conf.d/vhost-ops.conf
<VirtualHost *:80>
ServerName ops.local
DocumentRoot /var/www/ops
ErrorLog /var/log/httpd/ops-error.log
CustomLog /var/log/httpd/ops-access.log combined
<Directory "/var/www/ops">
Options -Indexes +FollowSymLinks # 关闭目录浏览
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName blog.ops.local
DocumentRoot /var/www/blog
ErrorLog /var/log/httpd/blog-error.log
CustomLog /var/log/httpd/blog-access.log combined
</VirtualHost>
Terminal window
mkdir -p /var/www/ops /var/www/blog
echo '<h1>ops</h1>' > /var/www/ops/index.html
echo '<h1>blog</h1>' > /var/www/blog/index.html
chown -R apache:apache /var/www/ops /var/www/blog
apachectl configtest && systemctl reload httpd
curl -H 'Host: ops.local' http://127.0.0.1/
curl -H 'Host: blog.ops.local' http://127.0.0.1/

虚拟主机的本质是按 Host 头分流,所以没有 DNS 也能用 -H Host: 测试。

3.3 目录访问控制#

<Directory "/var/www/ops/admin">
Require ip 10.0.0.0/24
</Directory>
# 单文件保护
<Files ".htaccess">
Require all denied
</Files>

3.4 压缩与缓存#

# 压缩
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json
</IfModule>
# 强缓存静态资源
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/png "access plus 30 days"
ExpiresByType text/css "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
</IfModule>

验证:

Terminal window
curl -I -H 'Accept-Encoding: gzip' http://127.0.0.1/style.css | grep -i content-encoding
curl -I http://127.0.0.1/logo.png | grep -i -E 'cache-control|expires'

3.5 MPM 调优#

/etc/httpd/conf.modules.d/00-mpm.conf 选定模块后,在 httpd.conf 调参:

<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0 # 0 表示不回收;内存泄漏时可设 10000
</IfModule>

MaxRequestWorkers 是最关键的上限:它决定了能同时处理多少请求,超出的会排队,直到 ListenBacklog 溢出。

3.6 访问日志分析#

Terminal window
# 独立 IP 数
awk '{print $1}' /var/log/httpd/ops-access.log | sort -u | wc -l
# TOP 10 访问 IP
awk '{print $1}' /var/log/httpd/ops-access.log | sort | uniq -c | sort -rn | head
# 状态码分布
awk '{print $9}' /var/log/httpd/ops-access.log | sort | uniq -c | sort -rn
# 最慢的请求(需 %D 记录耗时,combined 不含,要自定义 LogFormat)
awk '$NF > 1000000 {print}' /var/log/httpd/ops-access.log | head

四、常见故障与排错#

现象排查结论
403 Forbiddenerror_log、目录权限、SELinux文件权限不足或 Require 拒绝
404 但文件存在DocumentRoot 路径、Options -Indexes路径拼错 / 虚拟主机未匹配 Host
500error_log通常是 CGI/PHP 或配置语法
端口占用启动失败ss -lntup | grep :80Nginx 或旧进程占用
改配置不生效apachectl configtest、是否 reload语法错误被静默忽略 / 未 reload
页面一直转圈access_log 里 %D 耗时后端慢,不是 httpd 问题

SELinux 常见坑:把站点放到 /data/www,权限全对仍然 403,需 chcon -R -t httpd_sys_content_t /data/www 或 semanage fcontext。

五、本章小结与验收#

  • 报文、状态码、连接管理是 HTTP 排障的三块基石
  • 虚拟主机靠 Host 分流;MaxRequestWorkers 决定并发上限
  • 缓存与压缩必须用 curl -I 验证,不要「配了就算完」

验收题:

  1. 一机跑两个域名,用 curl -H Host: 验证互不干扰
  2. 关闭目录浏览并限制 /admin 只允许 10.0.0.0/24,验证 403 行为
  3. 用访问日志统计 TOP 10 IP 与状态码分布,写出一段分析结论

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

HTTP 协议工作模型和 httpd 基础配置与应用
https://aiyw.top/posts/linux-15-httpd/
作者
LiPeng
发布于
2024-04-15
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
LiPeng
运维工程师 · 可观测性 / 系统迁移 / 容器编排
公告
记录可观测体系、系统迁移、容器编排的实战复盘;「学习」里是 K8s 四周课程笔记。
分类
标签
站点统计
文章
31
分类
5
标签
58
总字数
59,760
运行时长
0 天
最后活动
0 天前
文章目录