跳到主要内容

Apache 速查表:HTTP Server 配置、VirtualHost、.htaccess、Rewrite、SSL、反代

Apache HTTP Server 速查,覆盖 httpd.conf、VirtualHost、.htaccess、mod_rewrite、SSL、反向代理、认证、Header 和日志。

  • 本地处理
  • 分类 开发运维
  • 适合 格式化、校验、压缩或检查和代码相关的文本。
分类:
88

粘贴 Apache 配置片段后可检测已识别指令和常见风险。不粘贴也可以直接使用上方搜索。

命令 (7)
apachectl configtest

重载前检查 Apache 配置语法。Debian 也可用 `apache2ctl configtest`,RHEL 常见是 `apachectl` 或 `httpd -t`。

注意: 每次重载前都先跑。配置坏了再 restart,服务可能直接起不来。

示例
sudo apachectl configtest
sudo apache2ctl configtest
sudo httpd -t
apachectl graceful

优雅重载配置,让正在处理的请求先完成。

注意: 只有 graceful 拿不到变更时才考虑 restart。

示例
sudo apachectl graceful
sudo apache2ctl graceful
systemctl reload apache2|httpd

systemd 重载命令,Debian 服务名是 `apache2`,RHEL 服务名通常是 `httpd`。

示例
sudo systemctl reload apache2
sudo systemctl reload httpd
sudo systemctl status apache2
apachectl -M

列出已加载模块。确认 mod_rewrite、mod_proxy、mod_ssl、mod_headers 是否启用最快。

示例
apachectl -M | grep rewrite
apache2ctl -M | grep headers
apachectl -S

打印解析后的虚拟主机、端口、域名、别名和默认 vhost。

注意: 某个域名命中了错误站点时,这条通常能直接看出原因。

示例
sudo apachectl -S
sudo apache2ctl -S
a2ensite / a2dissite

Debian 系列启用或停用站点配置的助手,会维护 sites-enabled 软链。

示例
sudo a2ensite example.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
a2enmod / a2dismod

Debian 系列启用或停用 Apache 模块的助手。

示例
sudo a2enmod rewrite headers ssl proxy_http
sudo a2dismod autoindex
核心 (7)
ServerRoot "/etc/apache2"

Apache 相对配置路径的基准目录。Debian 和 RHEL 的目录布局不同。

示例
ServerRoot "/etc/apache2"
ServerRoot "/etc/httpd"
Listen 80

让 Apache 监听端口或地址。多条 Listen 可同时开 IPv4、IPv6、HTTP、HTTPS。

注意: 端口占用错误通常来自 Listen 冲突或另一个服务占了同一端口。

示例
Listen 80
Listen 443 https
Listen 127.0.0.1:8080
IncludeOptional conf-enabled/*.conf

加载可选配置文件或通配符,匹配为空时不会报错。

示例
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
LoadModule rewrite_module modules/mod_rewrite.so

加载共享模块。包管理安装的服务器通常由 a2enmod 或 conf.modules.d 管。

示例
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule ssl_module modules/mod_ssl.so
User www-data / Group www-data

worker 进程运行时使用的系统用户和用户组。

注意: DocumentRoot 下的文件要让这个用户能读,不是只让部署用户能读。

示例
User www-data
Group www-data
User apache
Group apache
ServerTokens Prod

减少响应头里暴露的服务器版本信息。

示例
ServerTokens Prod
ServerSignature Off
KeepAlive On

复用 TCP 连接处理多个请求。浏览器流量通常应该开启。

示例
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
VirtualHost (6)
<VirtualHost *:80>

定义某个地址和端口上的站点。基于域名的 vhost 在块内用 ServerName 和 ServerAlias 匹配。

示例
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example
</VirtualHost>
ServerName example.com

vhost 的主域名。写在全局时也能修复 AH00558 警告。

示例
ServerName example.com
ServerName localhost
ServerAlias www.example.com

同一个 vhost 还要接收的其他域名。

示例
ServerAlias www.example.com
ServerAlias *.example.net
DocumentRoot "/var/www/site"

这个 vhost 对外服务静态文件的文件系统根目录。

注意: 只写 DocumentRoot 不够,还要配匹配的 Directory 块放行。

示例
DocumentRoot "/var/www/site/public"
DirectoryIndex index.html index.php

请求映射到目录时 Apache 依次尝试的默认文件。

示例
DirectoryIndex index.html
DirectoryIndex index.php index.html
ErrorDocument 404 /404.html

给某个状态码配置自定义错误页或响应。

示例
ErrorDocument 404 /404.html
ErrorDocument 503 "Maintenance window"
目录权限 (7)
<Directory "/var/www/site">

给文件系统目录绑定访问控制、Options 和覆盖规则。

示例
<Directory "/var/www/site">
    Require all granted
    Options -Indexes +FollowSymLinks
</Directory>
Require all granted

允许所有客户端访问。公开 DocumentRoot 的 Directory 块里很常见。

示例
Require all granted
Require all denied

拒绝所有客户端访问。适合私有目录和默认拒绝块。

示例
Require all denied
Options -Indexes +FollowSymLinks

启用或关闭目录特性,如目录列表、符号链接、CGI、includes。

注意: `Options Indexes` 会在没有 index 文件时暴露目录列表。

示例
Options -Indexes +FollowSymLinks
Options None
AllowOverride None

忽略此目录下的 .htaccess。性能和可审查性最好的默认值。

示例
AllowOverride None
AllowOverride FileInfo AuthConfig

只允许选定类型的 .htaccess 覆盖。

注意: 优先收窄分类,不要默认 `AllowOverride All`。

示例
AllowOverride FileInfo AuthConfig
AllowOverride None
DirectorySlash On

把目录 URL 重定向到带末尾斜杠,避免相对链接解析错。

示例
DirectorySlash On
重写跳转 (6)
RewriteEngine On

在当前作用域启用 mod_rewrite 规则。

注意: RewriteEngine 没开或 mod_rewrite 没加载时,RewriteRule 不会生效。

示例
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.

只作用于紧随其后的下一条 RewriteRule。

示例
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

重写或重定向 URL 路径。在目录和 .htaccess 上下文里,pattern 匹配不带开头斜杠的路径。

注意: 做跳转时加 `[R=301,L]` 或 `[R=302,L]`,让规则在跳转后停止。

示例
RewriteRule ^old/(.*)$ /new/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
Redirect 301 /old /new

mod_alias 的简单前缀跳转。没有条件判断时比 mod_rewrite 清楚。

示例
Redirect 301 /old https://example.com/new
Redirect gone /removed
RedirectMatch 301 ^/docs/(.*)$ /manual/$1

mod_alias 提供的正则跳转。

示例
RedirectMatch 301 ^/old/(.*)$ https://example.com/new/$1
RewriteBase /app/

目录级 rewrite 替换路径的基准。主要用于子目录里的 .htaccess。

示例
RewriteBase /app/
反代 (6)
ProxyPass / http://127.0.0.1:3000/

把匹配的请求转发到上游服务器。

注意: 末尾斜杠会影响路径拼接。大多数 HTTP 应用都要配 ProxyPassReverse。

示例
ProxyPass / http://127.0.0.1:3000/
ProxyPass /api/ http://app:8080/api/
ProxyPassReverse / http://127.0.0.1:3000/

把上游返回的 Location 头改回公开 URL。

示例
ProxyPassReverse / http://127.0.0.1:3000/
ProxyPreserveHost On

把原始 Host 头传给上游应用。

示例
ProxyPreserveHost On
RequestHeader set X-Forwarded-Proto "https"

告诉上游应用原始请求在 TLS 终止前是 https。

示例
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"
ProxyTimeout 60

反向代理请求的超时时间。

注意: 调大超时只是掩盖慢上游,能修应用延迟就先修应用。

示例
ProxyTimeout 60
BalancerMember http://app1:8080

mod_proxy_balancer 上游池里的一个成员。

示例
<Proxy "balancer://app">
    BalancerMember http://app1:8080
    BalancerMember http://app2:8080
</Proxy>
ProxyPass / balancer://app/
SSL (5)
SSLEngine on

给当前 vhost 启用 TLS。

示例
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem

给客户端展示的证书文件。Let us Encrypt 部署通常用 fullchain.pem。

注意: 只用叶证书可能让需要中间证书链的客户端失败。

示例
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

与证书匹配的私钥。

示例
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1

限制协议版本。现代公网站点通常保留 TLS 1.2 和 1.3。

示例
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLUseStapling on

在 mod_ssl 和证书链支持时启用 OCSP stapling。

示例
SSLUseStapling on
SSLStaplingCache shmcb:/var/run/ocsp(128000)
认证 (6)
AuthType Basic

给目录或 Location 启用 HTTP Basic 认证。

示例
AuthType Basic
AuthName "Restricted"

浏览器登录弹窗里显示的 realm 名称。

示例
AuthName "Restricted area"
AuthUserFile /etc/apache2/.htpasswd

htpasswd 生成的密码文件路径。

注意: 把文件放在 DocumentRoot 外面,避免用户下载到哈希。

示例
AuthUserFile /etc/apache2/.htpasswd
Require valid-user

允许 AuthUserFile 里任意认证通过的用户访问。

示例
Require valid-user
Require user alice bob

只允许指定的已认证用户访问。

示例
Require user alice bob
AuthGroupFile /etc/apache2/.htgroups

`Require group` 规则使用的用户组文件。

示例
AuthGroupFile /etc/apache2/.htgroups
Require group admins
Header (6)
Header set Cache-Control "public, max-age=31536000"

用 mod_headers 设置响应头。

示例
Header set Cache-Control "public, max-age=31536000"
Header set X-Content-Type-Options "nosniff"
Header always set Strict-Transport-Security "max-age=31536000"

即使错误响应和重定向也设置这个响应头。

注意: 只有所有域名 HTTPS 都稳定后再上 HSTS。

示例
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
RequestHeader set X-Forwarded-Proto "https"

在 Apache 反代到上游前设置请求头。

示例
RequestHeader set X-Forwarded-Proto "https"
SetEnvIfNoCase User-Agent "bot" is_bot

根据请求元数据设置环境变量。

示例
SetEnvIfNoCase User-Agent "bot" is_bot
CustomLog logs/access.log combined env=!is_bot
AddType application/wasm .wasm

把文件扩展名映射到 MIME 类型。

示例
AddType application/wasm .wasm
AddType image/svg+xml .svg
AddOutputFilterByType DEFLATE text/html text/css application/javascript

用 mod_deflate 压缩选定类型的响应。

示例
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript
缓存 (6)
ExpiresActive On

在当前作用域启用 mod_expires 规则。

示例
ExpiresActive On
ExpiresByType image/png "access plus 1 year"

按 MIME 类型设置浏览器缓存过期时间。

示例
ExpiresByType text/css "access plus 1 month"
ExpiresByType image/png "access plus 1 year"
CacheQuickHandler off

让认证和 rewrite 正常执行后,再由 mod_cache 返回缓存内容。

示例
CacheQuickHandler off
CacheRoot "/var/cache/apache2/mod_cache_disk"

mod_cache_disk 使用的磁盘缓存目录。

示例
CacheRoot "/var/cache/apache2/mod_cache_disk"
CacheEnable disk /

为某个 URL 路径启用缓存 provider。

示例
CacheEnable disk /assets/
CacheDisable /api/
DeflateCompressionLevel 6

mod_deflate 的压缩等级。CPU 紧张时不是越高越好。

示例
DeflateCompressionLevel 6
日志 (5)
ErrorLog ${APACHE_LOG_DIR}/error.log

主错误日志路径。配置和权限问题优先从这里查。

示例
ErrorLog ${APACHE_LOG_DIR}/error.log
ErrorLog /var/log/httpd/example-error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

访问日志路径和使用的 LogFormat 名称。

示例
CustomLog ${APACHE_LOG_DIR}/access.log combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common

定义具名访问日志格式。

示例
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%v %h %>s %D \"%r\"" vhost_timing
LogLevel warn

错误日志详细程度。调试模块时可以临时调高。

示例
LogLevel warn
LogLevel rewrite:trace3 proxy:debug
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access.%Y%m%d 86400" combined

把日志管给 rotatelogs,按时间轮转。

示例
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access.%Y%m%d 86400" combined
.htaccess (4)
.htaccess

AllowOverride 允许时,Apache 在请求时读取的目录级配置文件。

注意: 它比 vhost 配置慢,也更难审查。只有需要时才用。

示例
# .htaccess
RewriteEngine On
RewriteRule ^old$ /new [R=301,L]
AllowOverride All

允许 .htaccess 覆盖所有类别的指令。

注意: 不要当默认值。它扩大攻击面,也让行为依赖 vhost 审查之外的文件。

示例
AllowOverride FileInfo AuthConfig
# Avoid broad default:
AllowOverride All
<FilesMatch "\.php$">

对文件名匹配正则的文件应用规则。

示例
<FilesMatch "\.inc$">
    Require all denied
</FilesMatch>
AddHandler application/x-httpd-php .php

把处理器映射到扩展名。共享主机常见,误用风险很高。

示例
AddHandler application/x-httpd-php .php
安全 (6)
TraceEnable Off

禁用 HTTP TRACE。

示例
TraceEnable Off
ServerSignature Off

隐藏自动生成错误页里的服务器签名。

示例
ServerSignature Off
Options -Indexes

关闭目录列表。

示例
Options -Indexes
Require ip 10.0.0.0/8

只允许选定客户端 IP 段访问。

示例
Require ip 10.0.0.0/8
Require ip 192.168.1.0/24
<LimitExcept GET POST>

对列出方法之外的其他 HTTP 方法应用访问控制。

示例
<LimitExcept GET POST>
    Require all denied
</LimitExcept>
FileETag None

担心 inode/path 泄露或多节点不一致时关闭文件 ETag。

示例
FileETag None
排查 (6)
403 Forbidden

检查 Directory 里的 Require、文件系统权限、SELinux 和 .htaccess 覆盖。

示例
sudo tail -f /var/log/apache2/error.log
namei -l /var/www/site/index.html
404 Not Found

检查 apachectl -S 命中的 vhost、DocumentRoot、Alias 和 rewrite 目标。

示例
sudo apachectl -S
curl -I http://example.com/path
500 Internal Server Error

常见原因是 .htaccess 语法错、CGI/PHP handler 问题、或某指令不允许出现在当前上下文。

示例
tail -f /var/log/apache2/error.log
apachectl configtest
502 Proxy Error

Apache 进入了代理处理,但上游拒绝、关闭或超时。

示例
curl -v http://127.0.0.1:3000/
LogLevel proxy:debug
AH00558 could not reliably determine the server name

设置全局 ServerName 可消除启动警告。

示例
ServerName localhost
echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/servername.conf
AH00072 make_sock could not bind

端口已被占用,或 Listen 指令冲突。

示例
sudo ss -ltnp | grep ":80"
sudo apachectl -S
模板 (5)
Static site vhost

静态站点的最小 HTTP vhost。

示例
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example/public
    <Directory "/var/www/example/public">
        Require all granted
        Options -Indexes +FollowSymLinks
        AllowOverride None
    </Directory>
</VirtualHost>
HTTPS vhost

使用 Let us Encrypt 路径的 TLS vhost。

示例
<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example/public
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
Reverse proxy vhost

Apache 接在本地应用服务前面的反向代理 vhost。

示例
<VirtualHost *:80>
    ServerName app.example.com
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
.htaccess HTTPS redirect

共享主机 .htaccess 的规范 HTTPS 跳转。

示例
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Basic auth directory

用 htpasswd 用户保护目录。

示例
<Directory "/var/www/private">
    AuthType Basic
    AuthName "Restricted"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Directory>

这个工具能做什么

面向 Apache HTTP Server 日常运维的可搜索速查表,覆盖真正会反复查的配置: apachectl configtest、优雅重载、VirtualHost、ServerName、DocumentRoot、 Directory 权限、AllowOverride、Options、Require、mod_rewrite 跳转、 .htaccess、ProxyPass / ProxyPassReverse、SSL 证书、Header、缓存、 压缩、基础认证、日志格式,以及 403 / 404 / 500 / 502 的排查路径。 工具完全在浏览器本地运行:可以搜指令、按分类过滤、粘一小段 Apache 配置检测已识别指令和常见风险,再复制需要的指令或示例。内容按 Debian 的 apache2、RHEL 的 httpd、共享主机 .htaccess、反向代理 vhost 这些真实场景来写,不是只列几个 hello world。

工具细节

输入
文件 + 文本
页面会根据工具类型展示文本框、数值控件、文件选择或结构化输入。
输出
即时结果 + 复制 + 预览
结果区优先给出可操作结果,支持项会显示复制、下载或可视化预览。
隐私
浏览器本地处理
主工具逻辑未发现外部 API 调用,输入通常留在当前标签页内处理。
保存 / 分享
可分享链接状态
关键设置会进入 URL,复制链接后别人能复现同一组参数。
性能预算
首屏 JS ≤ 34 KB
没有声明 WASM 依赖,适合快速打开和移动端使用。
适用场景
开发运维 · 程序员
分类和职业标签用于推荐相关工具、组织内链,并帮助用户快速判断是否适合当前任务。

怎么用

  1. 1. 输入

    把内容粘贴或拖入工具面板。

  2. 2. 处理

    点击按钮,在浏览器内本地处理,文件不上传。

  3. 3. 复制 / 下载

    一键复制结果或下载到本地。

Apache 速查表 适合怎么用

适合穿插在写代码、查问题、做 Review、上线前的小任务里。

适合开发场景

  • 格式化、校验、压缩或检查和代码相关的文本。
  • 把片段整理好再放进文档、工单、提交或交接材料。
  • 不切换工具,快速检查一个小 payload。

开发检查项

  • 压缩、混淆这类不可逆处理,先对副本操作。
  • 除非确认工具本地处理,不要粘贴密钥和敏感片段。
  • 转换后的代码上线前,仍要跑自己的测试或 lint。

下一步可以接着做

这些入口会把当前任务接到更完整的工具链里。

  1. 1 Nginx 速查表 Nginx 速查表,常见配置、location/server 块、SSL、反代、gzip,真实例子和常见坑。 打开
  2. 2 .htaccess 生成器 Apache .htaccess 生成器:HTTPS 跳转、www 切换、Gzip、缓存头、防盗链。 打开
  3. 3 .htpasswd 生成器 生成 Apache/Nginx 基本认证 .htpasswd 行:bcrypt / apr1(MD5)/ SHA-1,多用户 + 复制 + 下载,100% 浏览器本地 打开

真实使用场景

  • 迁移站点后排查 403

    把 vhost 和 Directory 片段粘进去,工具会识别 DocumentRoot、 Directory、Require、Options,再跳到 403 和目录权限条目。检查点会提醒你看 `Require all granted`、Apache 用户对文件系统的读取权限,以及 error log 里的真实路径。

  • 把应用服务接到 Apache 反向代理后面

    搜 ProxyPass,直接复制 ProxyPassReverse 配套示例。片段里如果只有 ProxyPass 没有反向映射,分析区会提醒你;这正是重定向暴露上游主机名或路径的常见原因。

  • 判断 rewrite 该不该放进 .htaccess

    共享主机工单要加跳转规则时,切到 Rewrite 和 .htaccess 分类。速查会区分 Redirect、RewriteRule、RewriteCond、AllowOverride,避免为了一个规则把所有 .htaccess 能力都打开。

常见踩坑

  • 到处写 `AllowOverride All` 会让每个请求都查 .htaccess,并允许目录规则覆盖审过的 vhost。能收窄就收窄,能放 vhost 就放 vhost。

  • 只写 `ProxyPass` 不写 `ProxyPassReverse`,上游重定向很容易把内网主机名漏给用户。除非非常明确,否则成对写。

  • 公共目录打开 `Options Indexes`,没有 index 文件时会直接列目录。不是故意开放下载目录,就用 `Options -Indexes`。

隐私说明

Apache 配置可能包含私有路径、内网域名和上游 URL,所以配置片段文本框只在本地处理,不写进 URL 状态。搜索词和分类可以分享,粘贴的配置不会离开当前标签页。

常见问题

类似工具组合

做你这行的人, 还会一起用这些。

Made by Toolora · 100% client-side · Updated 2026-06-13