- Put the six
Header always setlines in one file loaded in the server configuration. Apache applies it to every virtual host, and condition the HSTS line on%{HTTPS}so plain HTTP does not carry it. - Unlike Nginx, a
<Location>or virtual host that sets another header keeps the six. Setting one of the six again replaces it for that scope, which is how you give a path its own value. - Keep
alwayson every line, and never add aHeader setfor the same header elsewhere: the two tables combine, and the response carries the header twice. - Behind
mod_proxy_http, addHeader unsetfor the six headers and forServer. Otherwise the application's values, and its version, reach the browser too. - With all six passing, WebSec0 gives the headers grade 100 out of 100. Add
ServerTokens Prodto avoid the 5-point penalty for the version in theServerheader.
The short answer
Create one file with the six headers:
# /etc/apache2/conf-available/security-headers.conf
Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" "expr=%{HTTPS} == 'on'"
Header always set X-Frame-Options "DENY"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=(), serial=(), hid=(), bluetooth=(), midi=(), display-capture=(), accelerometer=(), gyroscope=(), magnetometer=(), browsing-topics=()"
Header always set X-Content-Type-Options "nosniff"Enable it, check the configuration and reload. On Debian and Ubuntu:
sudo a2enmod headers
sudo a2enconf security-headers
sudo apachectl configtest && sudo apachectl gracefulOn RHEL, Rocky Linux and AlmaLinux, save the file as
/etc/httpd/conf.d/security-headers.conf instead: httpd.conf loads every
.conf file in that directory. With the official Docker image, add
Include conf/extra/security-headers.conf to httpd.conf.
Every configuration on this page was run on Apache httpd 2.4.58, 2.4.62 and
2.4.69 and checked with curl, on a page, a 404, a static file, a redirect, a
proxied application and paths with their own values.
Two of these lines need a rollout before they go live in this form. The CSP
blocks every script, style and connection that is not on your own origin:
start it in report-only mode, as the
Content-Security-Policy guide for Apache
explains. The HSTS value is a final one: start with a short max-age, as the
HSTS guide for Apache explains.
What each line does
| Header | Value in the file | WebSec0 points | Details |
|---|---|---|---|
| Content-Security-Policy | Only your own origin, no plugins, no framing | 25 | CSP on Apache |
| Strict-Transport-Security | HTTPS only for one year, subdomains included | 20 | HSTS on Apache |
| X-Frame-Options | No framing, for browsers without frame-ancestors |
15 | X-Frame-Options on Apache |
| Referrer-Policy | Full URL on your origin, origin only elsewhere | 15 | Referrer-Policy on Apache |
| Permissions-Policy | Camera, microphone, location, devices and sensors off | 15 | Permissions-Policy on Apache |
| X-Content-Type-Options | No MIME sniffing | 10 | X-Content-Type-Options on Apache |
always on every line keeps the headers on 404 pages and redirects. Apache
keeps two tables of response headers: Header set writes to the one used for
successful responses only, Header always set to the one that is also sent on
errors and redirects.
The expr condition on the HSTS line keeps it off plain HTTP responses, where
browsers ignore it. In our test, the port 80 redirect carried the five other
headers, which is harmless, and no HSTS.
Before you start
Check that mod_headers is loaded, then list your virtual hosts and every
Header line already in the configuration:
apachectl -M | grep headers
apachectl -S
grep -rnE "Header|ServerTokens|ProxyPass" /etc/apache2/ /etc/httpd/ 2>/dev/nullThen check three things the file can break:
- Scripts and styles your CSP does not allow. Run the policy in report-only mode first.
- Subdomains without HTTPS.
includeSubDomainsmakes browsers refuse them. - Files served with the wrong type.
nosniffblocks a script or stylesheet whoseContent-Typedoes not match. CheckAddTypelines andmime.typesfor.mjs,.wasmand other recent extensions.
Also remove the features your site uses from the Permissions-Policy line,
such as camera for a video call page, or give those pages their own value
(step 4).
Step 1: one file for every virtual host
Apache merges the server configuration into each virtual host, then
<Directory>, <Files> and <Location> sections on top of it. A file loaded
outside any VirtualHost therefore reaches every site, including virtual
hosts that only redirect.
This differs from Nginx, where one add_header in a block drops every
inherited one. In Apache, a section that sets another header keeps the
six:
<VirtualHost *:443>
ServerName example.com
# SSLEngine and certificate lines...
<Location "/assets/">
# The six security headers are still sent for /assets/.
Header always set Cache-Control "public, max-age=31536000, immutable"
</Location>
</VirtualHost>Our test returned Cache-Control and the six headers for /assets/app.js.
A virtual host or section that sets one of the six again replaces it for
that scope. In our test, a virtual host with
Header always set Content-Security-Policy "default-src 'none'" sent only
that policy, on pages and 404s. Step 4 uses this to give paths their own
values.
Step 2: never mix Header set and Header always set
The two tables combine. A Header set X-Frame-Options "SAMEORIGIN" left in a
virtual host, from an old tutorial or a CMS, adds a second header next to the
file’s:
HTTP/1.1 200 OK
X-Frame-Options: DENY
X-Frame-Options: SAMEORIGINThat was our test’s result on pages; the 404 carried only DENY, from the
always table. Browsers block framing everywhere when the values conflict,
and WebSec0 gives such a header half its points. Search for every Header set
and Header append line for the six headers, and change them to
Header always set or remove them. The Debian and Ubuntu
/etc/apache2/conf-available/security.conf has commented Header set lines
for X-Content-Type-Options and Content-Security-Policy: leave them
commented.
Step 3: hide the application’s headers behind a proxy
With mod_proxy_http, the application’s headers land in the default table and
yours in the always table, so the response carries both. Our test, with an
application that sent weak values for all six headers:
HTTP/1.1 200 OK
Server: nginx/1.30.6
Content-Security-Policy: default-src 'self'; script-src 'self'; ...
Content-Security-Policy: default-src *
Referrer-Policy: strict-origin-when-cross-origin
Referrer-Policy: unsafe-urlBrowsers enforce both CSPs and apply the last Referrer-Policy, here
unsafe-url. The application’s Server header also replaced Apache’s, with
its version. Add these lines at the top of the file:
# /etc/apache2/conf-available/security-headers.conf, before the six lines
Header unset Server
Header unset Content-Security-Policy
Header unset Strict-Transport-Security
Header unset X-Frame-Options
Header unset Referrer-Policy
Header unset Permissions-Policy
Header unset X-Content-Type-OptionsOur test then returned each header once, with the file’s value, on the
application’s pages and on its 404s, and Server: Apache once ServerTokens
was set (step 5). The lines only touch the default table, so they change
nothing for static files. Apache’s documentation notes that CGI scripts and
mod_proxy_fcgi, which runs PHP-FPM, put their headers in the always table,
where Header always set replaces them. If the application must own one
header, such as a CSP with per-request nonces, remove that header’s two lines
instead.
Step 4: different values for some paths
Some pages need a different value: an embeddable widget, a password reset
page, a video call. Set the header again in a <Location> inside the
virtual host:
<VirtualHost *:443>
ServerName example.com
# SSLEngine and certificate lines...
<Location "/embed/">
Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self' https://partner.example; upgrade-insecure-requests"
Header always unset X-Frame-Options
</Location>
<Location "/reset/">
Header always set Referrer-Policy "no-referrer"
</Location>
<Location "/meet/">
Header always set Permissions-Policy "camera=(self), microphone=(self), geolocation=(), payment=(), usb=(), serial=(), hid=(), bluetooth=(), midi=(), display-capture=(), accelerometer=(), gyroscope=(), magnetometer=(), browsing-topics=()"
</Location>
</VirtualHost>What our test returned:
| Path | What changes |
|---|---|
/ |
The default values |
/embed/ |
frame-ancestors 'self' https://partner.example, and no X-Frame-Options |
/reset/ |
Referrer-Policy: no-referrer |
/meet/ |
camera=(self), microphone=(self) |
The 404 pages under /embed/ and /reset/ got the same values as the pages.
The other five headers stayed in place on each path. Use always in the
unset line too: a plain Header unset only clears the default table, and
the file’s X-Frame-Options is in the always one.
Step 5: hide the Apache version
WebSec0 removes 5 points from the headers grade when the Server header
contains a version (see the Server header guide). Our tests got these values by default:
| Installation | Server header |
|---|---|
| Official Docker image | Apache/2.4.69 (Unix) OpenSSL/3.5.7 |
| Debian package | Apache/2.4.69 (Debian) |
Set ServerTokens Prod in the server configuration. Inside a virtual host,
our test failed with ServerTokens cannot occur within <VirtualHost> section.
On Debian and Ubuntu, edit the line that already exists in
/etc/apache2/conf-available/security.conf, which ships with
ServerTokens OS and ServerSignature On:
ServerTokens Prod
ServerSignature OffOur test then returned Server: Apache on every response, error pages and
redirects included. ServerSignature Off removes the footer of Apache’s own
error pages, which repeats the Server value, such as
Apache/2.4.69 (Debian) Server at localhost Port 80 on Debian. Neither line changes the
header of a proxied application: that needs Header unset Server (step 3).
On shared hosting: .htaccess
Without access to the server configuration, put the same six lines in the
.htaccess file at the root of your site:
# .htaccess
<IfModule mod_headers.c>
Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" "expr=%{HTTPS} == 'on'"
Header always set X-Frame-Options "DENY"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=(), serial=(), hid=(), bluetooth=(), midi=(), display-capture=(), accelerometer=(), gyroscope=(), magnetometer=(), browsing-topics=()"
Header always set X-Content-Type-Options "nosniff"
</IfModule>The directory needs AllowOverride FileInfo (or All). With it, our test
returned the six headers on pages, 404s and static files. A Redirect in the
virtual host, however, answered without them: Apache sends it before it reads
.htaccess. ServerTokens cannot be set in .htaccess; ask your host.
Verify it
Check that each header appears exactly once on a page, an error page and a static file. Use your own URLs:
for url in https://example.com/ https://example.com/does-not-exist https://example.com/assets/app.js; do
echo "$url"
for h in content-security-policy strict-transport-security x-frame-options referrer-policy permissions-policy x-content-type-options; do
printf ' %-26s %s\n' "$h" "$(curl -sI "$url" | grep -ci "^$h:")"
done
doneEvery count should be 1. A 0 points to a file that is not loaded or a
section with Header always unset; a 2 to a proxied application or a
leftover Header set line. Then open your pages with the browser console for
CSP, Permissions-Policy or MIME errors, and scan the site with WebSec0.
How WebSec0 grades this configuration
The six headers make up the 100 points of the WebSec0 headers grade. With the file above, each one passes:
| Header | Points | Passes when |
|---|---|---|
| Content-Security-Policy | 25 | No 'unsafe-inline' in script-src, or in default-src without script-src |
| Strict-Transport-Security | 20 | max-age of at least one year with includeSubDomains |
| X-Frame-Options | 15 | DENY or SAMEORIGIN, or frame-ancestors in the CSP |
| Referrer-Policy | 15 | The applied value is not unsafe-url or no-referrer-when-downgrade |
| Permissions-Policy | 15 | It parses and disables at least one sensitive feature with () |
| X-Content-Type-Options | 10 | The first value is nosniff |
A headers grade of A+ starts at 95. The score can also change with other
headers: −5 for a version in the Server header (step 5), penalties for
cookies without secure attributes (−5 per cookie
without Secure, −10 at most; −3 per cookie without SameSite; −3 per
session cookie without HttpOnly), and −10 for
Access-Control-Allow-Origin: *. Cross-origin isolation headers add bonus
points: +5 for Cross-Origin-Opener-Policy: same-origin, +3 for
Cross-Origin-Embedder-Policy and +2 for Cross-Origin-Resource-Policy.
The HSTS line also counts for the TLS grade: a configuration that would earn
an A+ is held at A unless HSTS has a max-age of at least one year and
includeSubDomains.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Invalid command 'Header' |
mod_headers is not loaded |
a2enmod headers, or uncomment its LoadModule line. |
| Headers missing on 404 pages or redirects | A line without always |
Write Header always set. |
| Headers missing everywhere | The file is not loaded | a2enconf security-headers, or check the Include line and the file name. |
| A header appears twice | A proxied application sends it, or a Header set line coexists with Header always set |
Add the Header unset lines, and use always everywhere. |
| A path override has no effect | The <Location> is in another virtual host, or the path does not match |
Put it in the virtual host that serves the path, and test a URL under it. |
| X-Frame-Options still sent on a path | Header unset without always |
Write Header always unset X-Frame-Options. |
Server shows the application’s version |
mod_proxy_http passes the application’s Server header |
Add Header unset Server. |
ServerTokens cannot occur within <VirtualHost> section |
ServerTokens set in a virtual host |
Move it to the server configuration. |
| WebSec0 still shows the Apache version | ServerTokens missing, or a proxied application sends its own Server |
Set ServerTokens Prod in the server configuration, add Header unset Server, and reload. |
One guide per header
Every Apache guide
Content-Security-Policy
Guide ↗Add a Content-Security-Policy header in Apache with mod_headers, keep it on error pages and redirects, test it in report-only mode and avoid duplicates. Tested configs.
Strict-Transport-Security
Guide ↗Enable HSTS on Apache: send Strict-Transport-Security from the HTTPS virtual host only, with always, on every redirect and error page, without duplicates. Tested configs.
X-Frame-Options
Guide ↗Set X-Frame-Options on Apache with Header always set, pair it with CSP frame-ancestors, keep it on errors and redirects and avoid duplicate headers. Tested configs.
Referrer-Policy
Guide ↗Set Referrer-Policy on Apache with Header always set, keep it on errors and redirects, use no-referrer on sensitive paths and stop a proxied app's value winning. Tested.
Permissions-Policy
Guide ↗Set Permissions-Policy on Apache with Header always set, quote origins correctly, enable a feature on one path and stop a proxied app's policy winning. Tested configs.
X-Content-Type-Options
Guide ↗Set X-Content-Type-Options: nosniff on Apache with Header always set, keep it on errors and redirects, and fix files Apache sends without a Content-Type. Tested configs.
Other web servers
Security headers on other servers
Caddy
Guide ↗All six security headers on Caddy in one tested snippet: CSP, HSTS, X-Frame-Options, Referrer-Policy, Permissions-Policy and nosniff, proxies and error pages included.
Nginx
Guide ↗All six security headers on Nginx in one tested snippet: CSP, HSTS, X-Frame-Options, Referrer-Policy, Permissions-Policy and nosniff, error pages included.
Traefik
Guide ↗All six security headers on Traefik in one tested headers middleware: CSP, HSTS, X-Frame-Options, Referrer-Policy, Permissions-Policy and nosniff, labels or file.
Questions
Frequently asked questions
Which security headers should Apache send?
The six that WebSec0 grades: Content-Security-Policy, Strict-Transport-Security, X-Frame-Options, Referrer-Policy, Permissions-Policy and X-Content-Type-Options. Send them with Header always set so error pages and redirects carry them too.
Where do I put security headers in Apache?
In one file loaded in the server configuration: /etc/apache2/conf-available/security-headers.conf enabled with a2enconf on Debian and Ubuntu, /etc/httpd/conf.d/security-headers.conf on RHEL and its derivatives. Apache merges it into every virtual host.
Do I need to repeat the headers in each VirtualHost or Location?
No. Apache merges the server configuration into every virtual host and section, even one that sets other headers. Repeat a line only to change its value for that scope.
Why does my response have two X-Frame-Options or two CSP headers?
Either the application behind mod_proxy_http sends its own, or a Header set line coexists with your Header always set line. Each writes to a different table and Apache sends both. Use Header unset for proxied applications and always everywhere.
Can I set all security headers in .htaccess?
Yes, when the directory allows it with AllowOverride FileInfo. The six lines work on pages, 404s and static files, but not on redirects made by the virtual host, which Apache sends before it reads .htaccess.
How do I hide the Apache version from the Server header?
Set ServerTokens Prod in the server configuration, and ServerSignature Off for error pages. On Debian and Ubuntu, change both in /etc/apache2/conf-available/security.conf, which ships with ServerTokens OS.
References
Sources
- Apache Module mod_headersApache HTTP Server Project
- Configuration Sections: how the sections are mergedApache HTTP Server Project
- Apache Core Features: ServerTokensApache HTTP Server Project
- Expressions in Apache HTTP ServerApache HTTP Server Project
