apachectl configtest重载前检查 Apache 配置语法。Debian 也可用 `apache2ctl configtest`,RHEL 常见是 `apachectl` 或 `httpd -t`。
注意: 每次重载前都先跑。配置坏了再 restart,服务可能直接起不来。
sudo apachectl configtest
sudo apache2ctl configtest
sudo httpd -t
Apache HTTP Server 速查,覆盖 httpd.conf、VirtualHost、.htaccess、mod_rewrite、SSL、反向代理、认证、Header 和日志。
粘贴 Apache 配置片段后可检测已识别指令和常见风险。不粘贴也可以直接使用上方搜索。
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|httpdsystemd 重载命令,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 / a2dissiteDebian 系列启用或停用站点配置的助手,会维护 sites-enabled 软链。
sudo a2ensite example.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
a2enmod / a2dismodDebian 系列启用或停用 Apache 模块的助手。
sudo a2enmod rewrite headers ssl proxy_http
sudo a2dismod autoindex
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-dataworker 进程运行时使用的系统用户和用户组。
注意: 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 *:80>定义某个地址和端口上的站点。基于域名的 vhost 在块内用 ServerName 和 ServerAlias 匹配。
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
</VirtualHost>ServerName example.comvhost 的主域名。写在全局时也能修复 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"
<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
RewriteEngine On在当前作用域启用 mod_rewrite 规则。
注意: RewriteEngine 没开或 mod_rewrite 没加载时,RewriteRule 不会生效。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.只作用于紧随其后的下一条 RewriteRule。
RewriteCond %{HTTPS} !=onRewriteCond %{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 /newmod_alias 的简单前缀跳转。没有条件判断时比 mod_rewrite 清楚。
Redirect 301 /old https://example.com/new
Redirect gone /removed
RedirectMatch 301 ^/docs/(.*)$ /manual/$1mod_alias 提供的正则跳转。
RedirectMatch 301 ^/old/(.*)$ https://example.com/new/$1
RewriteBase /app/目录级 rewrite 替换路径的基准。主要用于子目录里的 .htaccess。
RewriteBase /app/
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:8080mod_proxy_balancer 上游池里的一个成员。
<Proxy "balancer://app">
BalancerMember http://app1:8080
BalancerMember http://app2:8080
</Proxy>
ProxyPass / balancer://app/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)
AuthType Basic给目录或 Location 启用 HTTP Basic 认证。
AuthType Basic
AuthName "Restricted"浏览器登录弹窗里显示的 realm 名称。
AuthName "Restricted area"
AuthUserFile /etc/apache2/.htpasswdhtpasswd 生成的密码文件路径。
注意: 把文件放在 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 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
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 6mod_deflate 的压缩等级。CPU 紧张时不是越高越好。
DeflateCompressionLevel 6
ErrorLog ${APACHE_LOG_DIR}/error.log主错误日志路径。配置和权限问题优先从这里查。
ErrorLog ${APACHE_LOG_DIR}/error.logErrorLog /var/log/httpd/example-error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined访问日志路径和使用的 LogFormat 名称。
CustomLog ${APACHE_LOG_DIR}/access.log combinedLogFormat "%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
.htaccessAllowOverride 允许时,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
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
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 ErrorApache 进入了代理处理,但上游拒绝、关闭或超时。
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
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 vhostApache 接在本地应用服务前面的反向代理 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。
把内容粘贴或拖入工具面板。
点击按钮,在浏览器内本地处理,文件不上传。
一键复制结果或下载到本地。
适合穿插在写代码、查问题、做 Review、上线前的小任务里。
这些入口会把当前任务接到更完整的工具链里。
把 vhost 和 Directory 片段粘进去,工具会识别 DocumentRoot、 Directory、Require、Options,再跳到 403 和目录权限条目。检查点会提醒你看 `Require all granted`、Apache 用户对文件系统的读取权限,以及 error log 里的真实路径。
搜 ProxyPass,直接复制 ProxyPassReverse 配套示例。片段里如果只有 ProxyPass 没有反向映射,分析区会提醒你;这正是重定向暴露上游主机名或路径的常见原因。
共享主机工单要加跳转规则时,切到 Rewrite 和 .htaccess 分类。速查会区分 Redirect、RewriteRule、RewriteCond、AllowOverride,避免为了一个规则把所有 .htaccess 能力都打开。
到处写 `AllowOverride All` 会让每个请求都查 .htaccess,并允许目录规则覆盖审过的 vhost。能收窄就收窄,能放 vhost 就放 vhost。
只写 `ProxyPass` 不写 `ProxyPassReverse`,上游重定向很容易把内网主机名漏给用户。除非非常明确,否则成对写。
公共目录打开 `Options Indexes`,没有 index 文件时会直接列目录。不是故意开放下载目录,就用 `Options -Indexes`。
Apache 配置可能包含私有路径、内网域名和上游 URL,所以配置片段文本框只在本地处理,不写进 URL 状态。搜索词和分类可以分享,粘贴的配置不会离开当前标签页。
做你这行的人, 还会一起用这些。