{"id":1253,"date":"2025-11-03T18:09:29","date_gmt":"2025-11-03T15:09:29","guid":{"rendered":"https:\/\/www.dchost.com\/blog\/the-friendly-guide-to-http-security-headers-how-i-set-up-hsts-csp-x-frame-options-and-x-content-type-options-without-breaking-stuff\/"},"modified":"2026-11-04T05:17:14","modified_gmt":"2026-11-04T02:17:14","slug":"http-security-headers-hsts-csp","status":"publish","type":"post","link":"https:\/\/www.dchost.com\/blog\/en\/http-security-headers-hsts-csp\/","title":{"rendered":"HTTP Security Headers: HSTS and CSP Guide"},"content":{"rendered":"<div class=\"dchost-blog-content-wrapper\"><p>A security-header change can affect script loading, framing or HTTPS access. Start by identifying the response header and rule involved rather than changing several settings at once. HSTS, CSP, X-Frame-Options and X-Content-Type-Options have distinct purposes, and a missing or incorrect setting needs to be assessed in the context of the application.<\/p>\n<p>This guide explains those four headers and provides Nginx, Apache and Express examples for a staged rollout. The starter configurations use short-lived HSTS and CSP Report-Only, with explicit reporting and nonce prerequisites. Replace the example domains and test the public responses and critical user flows before enabling enforcement.<\/p>\n<div id=\"toc_container\" role=\"navigation\" aria-label=\"Table of Contents\" data-nosnippet class=\"toc_transparent no_bullets toc_numbered toc_title_center\"><p class=\"toc_title\">\u0130\u00e7indekiler<\/p><ul class=\"toc_list\"><li><a href=\"#What_HTTP_Security_Headers_Control\"><span class=\"toc_number toc_depth_1\">1.<\/span> What HTTP Security Headers Control<\/a><\/li><li><a href=\"#HSTS_A_Time-Limited_HTTPS_Policy\"><span class=\"toc_number toc_depth_1\">2.<\/span> HSTS: A Time-Limited HTTPS Policy<\/a><ul><li><a href=\"#What_HSTS_does\"><span class=\"toc_number toc_depth_2\">2.1.<\/span> What HSTS does<\/a><\/li><li><a href=\"#How_to_Roll_It_Out_Safely\"><span class=\"toc_number toc_depth_2\">2.2.<\/span> How to Roll It Out Safely<\/a><\/li><li><a href=\"#Server_Config_Examples\"><span class=\"toc_number toc_depth_2\">2.3.<\/span> Server Config Examples<\/a><\/li><li><a href=\"#Subdomains_and_proxy_connections\"><span class=\"toc_number toc_depth_2\">2.4.<\/span> Subdomains and proxy connections<\/a><\/li><\/ul><\/li><li><a href=\"#CSP_Resource_and_Document_Rules\"><span class=\"toc_number toc_depth_1\">3.<\/span> CSP: Resource and Document Rules<\/a><ul><li><a href=\"#What_a_content_security_policy_does\"><span class=\"toc_number toc_depth_2\">3.1.<\/span> What a content security policy does<\/a><\/li><li><a href=\"#A_Complete_Report-Only_Example\"><span class=\"toc_number toc_depth_2\">3.2.<\/span> A Complete Report-Only Example<\/a><\/li><li><a href=\"#Server_Config_Examples-2\"><span class=\"toc_number toc_depth_2\">3.3.<\/span> Server Config Examples<\/a><\/li><li><a href=\"#Handling_Third-Party_Scripts_Without_Losing_Control\"><span class=\"toc_number toc_depth_2\">3.4.<\/span> Handling Third-Party Scripts Without Losing Control<\/a><\/li><\/ul><\/li><li><a href=\"#Framing_Controls_and_Clickjacking\"><span class=\"toc_number toc_depth_1\">4.<\/span> Framing Controls and Clickjacking<\/a><ul><li><a href=\"#X-Frame-Options_and_CSP_frame-ancestors\"><span class=\"toc_number toc_depth_2\">4.1.<\/span> X-Frame-Options and CSP frame-ancestors<\/a><\/li><li><a href=\"#Choose_the_intended_embedding_scope\"><span class=\"toc_number toc_depth_2\">4.2.<\/span> Choose the intended embedding scope<\/a><\/li><li><a href=\"#Server_Config_Examples-3\"><span class=\"toc_number toc_depth_2\">4.3.<\/span> Server Config Examples<\/a><\/li><\/ul><\/li><li><a href=\"#X-Content-Type-Options_and_MIME_Types\"><span class=\"toc_number toc_depth_1\">5.<\/span> X-Content-Type-Options and MIME Types<\/a><ul><li><a href=\"#What_nosniff_changes\"><span class=\"toc_number toc_depth_2\">5.1.<\/span> What nosniff changes<\/a><\/li><li><a href=\"#Server_examples\"><span class=\"toc_number toc_depth_2\">5.2.<\/span> Server examples<\/a><\/li><li><a href=\"#Diagnose_the_actual_response_type\"><span class=\"toc_number toc_depth_2\">5.3.<\/span> Diagnose the actual response type<\/a><\/li><\/ul><\/li><li><a href=\"#A_Staged_Implementation_Plan\"><span class=\"toc_number toc_depth_1\">6.<\/span> A Staged Implementation Plan<\/a><ul><li><a href=\"#Step_1_Map_What_You_Actually_Serve\"><span class=\"toc_number toc_depth_2\">6.1.<\/span> Step 1: Map What You Actually Serve<\/a><\/li><li><a href=\"#Step_2_Start_with_HTTPS_and_HSTS\"><span class=\"toc_number toc_depth_2\">6.2.<\/span> Step 2: Start with HTTPS and HSTS<\/a><\/li><li><a href=\"#Step_3_Deploy_CSP_in_Report-Only_Then_Enforce\"><span class=\"toc_number toc_depth_2\">6.3.<\/span> Step 3: Deploy CSP in Report-Only, Then Enforce<\/a><\/li><li><a href=\"#Step_4_Add_X-Frame-Options_and_X-Content-Type-Options\"><span class=\"toc_number toc_depth_2\">6.4.<\/span> Step 4: Add X-Frame-Options and X-Content-Type-Options<\/a><\/li><li><a href=\"#Step_5_Inspect_Responses_and_Browser_Behavior\"><span class=\"toc_number toc_depth_2\">6.5.<\/span> Step 5: Inspect Responses and Browser Behavior<\/a><\/li><li><a href=\"#Step_6_Dont_Forget_Your_Infrastructure_Basics\"><span class=\"toc_number toc_depth_2\">6.6.<\/span> Step 6: Don\u2019t Forget Your Infrastructure Basics<\/a><\/li><li><a href=\"#Step_7_Consider_the_Edge_CDNs_Caches_and_Multi-Environment_Teams\"><span class=\"toc_number toc_depth_2\">6.7.<\/span> Step 7: Consider the Edge: CDNs, Caches, and Multi-Environment Teams<\/a><\/li><\/ul><\/li><li><a href=\"#Complete_Starter_Examples_to_Adapt\"><span class=\"toc_number toc_depth_1\">7.<\/span> Complete Starter Examples to Adapt<\/a><ul><li><a href=\"#Nginx_Short_HSTS_and_Monitored_CSP\"><span class=\"toc_number toc_depth_2\">7.1.<\/span> Nginx: Short HSTS and Monitored CSP<\/a><\/li><li><a href=\"#Apache_24_with_mod_headers\"><span class=\"toc_number toc_depth_2\">7.2.<\/span> Apache 2.4 with mod_headers<\/a><\/li><li><a href=\"#Express_Dynamic_HTML_and_a_Nonce\"><span class=\"toc_number toc_depth_2\">7.3.<\/span> Express: Dynamic HTML and a Nonce<\/a><\/li><li><a href=\"#EJS_Template_for_the_Express_Examples\"><span class=\"toc_number toc_depth_2\">7.4.<\/span> EJS Template for the Express Examples<\/a><\/li><\/ul><\/li><li><a href=\"#Debugging_and_Monitoring\"><span class=\"toc_number toc_depth_1\">8.<\/span> Debugging and Monitoring<\/a><ul><li><a href=\"#DevTools_First_Logs_Second\"><span class=\"toc_number toc_depth_2\">8.1.<\/span> DevTools First, Logs Second<\/a><\/li><li><a href=\"#Edge_Cases_with_Third-Party_Widgets\"><span class=\"toc_number toc_depth_2\">8.2.<\/span> Edge Cases with Third-Party Widgets<\/a><\/li><li><a href=\"#Dont_Forget_the_Big_Picture\"><span class=\"toc_number toc_depth_2\">8.3.<\/span> Don\u2019t Forget the Big Picture<\/a><\/li><\/ul><\/li><li><a href=\"#Deployment_and_Maintenance_Checklist\"><span class=\"toc_number toc_depth_1\">9.<\/span> Deployment and Maintenance Checklist<\/a><\/li><\/ul><\/div>\n<h2 id=\"section-1\"><span id=\"What_HTTP_Security_Headers_Control\">What HTTP Security Headers Control<\/span><\/h2>\n<p>Security headers are instructions sent with an HTTP response. They influence HTTPS use, resource loading, whether a document can be embedded, and how the browser handles content types. Their scope depends on the header and browser support. They complement authentication, output escaping, access controls and software maintenance.<\/p>\n<p>These controls can reduce particular browser-side risks, but adding them does not prove compliance or block every instance of XSS, mixed content or clickjacking. Verify that each policy expresses the intended behavior and that the application can operate within it. A scanner grade is one observation, not a complete security assessment.<\/p>\n<p>Verify HTTPS and certificate renewal before deploying HSTS. Check the relevant hostname, chain and redirects; a browser icon alone is not sufficient evidence. For certificate basics, see the guide to <a href=\"https:\/\/www.dchost.com\/blog\/en\/what-is-an-ssl-certificate-ways-to-secure-your-website\/\">SSL certificates and website connections<\/a>.<\/p>\n<h2 id=\"section-2\"><span id=\"HSTS_A_Time-Limited_HTTPS_Policy\">HSTS: A Time-Limited HTTPS Policy<\/span><\/h2>\n<h3><span id=\"What_HSTS_does\">What HSTS does<\/span><\/h3>\n<p>HTTP Strict Transport Security uses the Strict-Transport-Security response header. After receiving it over a valid HTTPS connection, a supporting browser upgrades requests for the covered hostname to HTTPS for the specified lifetime. Certificate errors then stop the connection. HSTS does not repair certificates, rewrite every external resource URL or encrypt a CDN-to-origin connection.<\/p>\n<p>A short initial example is <strong>Strict-Transport-Security: max-age=300<\/strong>. The <strong>max-age<\/strong> is measured in seconds; later valid headers refresh the stored policy. <strong>includeSubDomains<\/strong> extends it to all subdomains, including internal or staging names. <strong>Preload<\/strong> is a separate opt-in and submission process; adding the token alone does not enroll a domain in a browser list.<\/p>\n<h3><span id=\"How_to_Roll_It_Out_Safely\">How to Roll It Out Safely<\/span><\/h3>\n<p>After HTTPS and renewal work, begin with a short lifetime and expand only after testing. Audit subdomains before adding coverage. Removing the header does not immediately remove a browser\u2019s stored policy, and preload removal has its own timeline. The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Strict-Transport-Security\" rel=\"nofollow noopener\" target=\"_blank\">MDN HSTS reference<\/a> explains the behavior. Evaluate preload requirements separately from the starter configuration.<\/p>\n<h3><span id=\"Server_Config_Examples\">Server Config Examples<\/span><\/h3>\n<p><strong>Nginx:<\/strong> place this directive in the HTTPS server block. It starts a five-minute policy for the current hostname, without subdomain coverage or preload. The always parameter also covers error responses.<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">add_header Strict-Transport-Security &quot;max-age=300&quot; always;<\/code><\/pre>\n<p><strong>Apache 2.4 with mod_headers:<\/strong> this condition applies the header only when TLS terminates directly at Apache. Use the virtual-host configuration or an .htaccess context permitted by the server. If TLS ends at a trusted reverse proxy, manage HSTS at the HTTPS-facing layer instead of blindly trusting a client-supplied scheme header.<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">Header always set Strict-Transport-Security &quot;max-age=300&quot; &quot;expr=%{HTTPS} == &#039;on&#039;&quot;<\/code><\/pre>\n<p><strong>Express:<\/strong> with an existing app instance, register this middleware before routes. The example checks req.secure and assumes direct TLS or an explicitly configured, trusted proxy path. Do not enable blanket trust of forwarded headers to make this check pass.<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">app.use((req, res, next) =&gt; {\n  if (req.secure) {\n    res.set(&#039;Strict-Transport-Security&#039;, &#039;max-age=300&#039;);\n  }\n  next();\n});<\/code><\/pre>\n<h3><span id=\"Subdomains_and_proxy_connections\">Subdomains and proxy connections<\/span><\/h3>\n<p>A staging hostname under a domain with includeSubDomains inherits that policy even if staging does not send HSTS itself; shared cookies are not the reason. Also distinguish browser-to-CDN HTTPS from CDN-to-origin HTTPS. HSTS governs the browser, while origin encryption and certificate validation need separate proxy settings. For the architecture, see <a href=\"https:\/\/www.dchost.com\/blog\/en\/what-is-content-delivery-network-cdn\/\">the CDN guide<\/a>.<\/p>\n<h2 id=\"section-3\"><span id=\"CSP_Resource_and_Document_Rules\">CSP: Resource and Document Rules<\/span><\/h2>\n<h3><span id=\"What_a_content_security_policy_does\">What a content security policy does<\/span><\/h3>\n<p>CSP defines policies for scripts, styles, images, fonts, connections and other document behavior. Different directives control different operations: frame-ancestors restricts who can embed this document, while frame-src restricts frames loaded inside it. A policy can block a required feature if its origins, inline code or resource types have not been accounted for.<\/p>\n<p>Inventory the resources and important flows, then start with <strong>Report-Only<\/strong>. This monitors a candidate policy without enforcing it. Receiving server-side reports requires a working collector and reporting configuration; normal access logs do not automatically contain CSP violations. Keep testing login, checkout, administration and third-party integrations as the policy evolves.<\/p>\n<h3><span id=\"A_Complete_Report-Only_Example\">A Complete Report-Only Example<\/span><\/h3>\n<p>The following are two HTTP response headers, each sent on one line. They are not Nginx or Apache directives. The example monitors same-origin resources and does not authorize inline scripts or styles. Replace example.com and the reporting path with a configured collector. The report-to name must match Reporting-Endpoints; report-uri provides compatibility for older reporting implementations.<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">Reporting-Endpoints: csp-endpoint=&quot;https:\/\/example.com\/csp-report&quot;\nContent-Security-Policy-Report-Only: default-src &#039;self&#039;; base-uri &#039;self&#039;; object-src &#039;none&#039;; script-src &#039;self&#039;; style-src &#039;self&#039;; img-src &#039;self&#039; data:; font-src &#039;self&#039;; connect-src &#039;self&#039;; frame-ancestors &#039;self&#039;; report-to csp-endpoint; report-uri \/csp-report<\/code><\/pre>\n<p>The collector must accept the relevant Reporting API and legacy CSP report formats. Do not add every reported origin automatically: confirm which resources the application needs. If inline scripts require nonces, generate an unpredictable value for each HTML response and insert it only into trusted script elements, for example through a template\u2019s <code>&lt;script nonce=\"...\"&gt;<\/code> attribute. The static policy above does not generate or include a nonce; the Express example below shows a separate dynamic approach.<\/p>\n<h3><span id=\"Server_Config_Examples-2\">Server Config Examples<\/span><\/h3>\n<p><strong>Nginx, Report-Only:<\/strong> these directives publish the same candidate policy. Keep the reporting endpoint configured separately. Under default Nginx inheritance, a location with its own add_header directives does not inherit the parent set; inspect those responses and any explicit inheritance settings in your installed version.<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">add_header Reporting-Endpoints &#039;csp-endpoint=&quot;https:\/\/example.com\/csp-report&quot;&#039; always;\nadd_header Content-Security-Policy-Report-Only &quot;default-src &#039;self&#039;; base-uri &#039;self&#039;; object-src &#039;none&#039;; script-src &#039;self&#039;; style-src &#039;self&#039;; img-src &#039;self&#039; data:; font-src &#039;self&#039;; connect-src &#039;self&#039;; frame-ancestors &#039;self&#039;; report-to csp-endpoint; report-uri \/csp-report&quot; always;<\/code><\/pre>\n<p><strong>Apache with mod_headers, Report-Only:<\/strong> use an allowed configuration context and adapt the collector address. Set each header in one responsible layer and check for duplicates from the application, Apache or CDN.<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">Header always set Reporting-Endpoints &quot;csp-endpoint=\\&quot;https:\/\/example.com\/csp-report\\&quot;&quot;\nHeader always set Content-Security-Policy-Report-Only &quot;default-src &#039;self&#039;; base-uri &#039;self&#039;; object-src &#039;none&#039;; script-src &#039;self&#039;; style-src &#039;self&#039;; img-src &#039;self&#039; data:; font-src &#039;self&#039;; connect-src &#039;self&#039;; frame-ancestors &#039;self&#039;; report-to csp-endpoint; report-uri \/csp-report&quot;<\/code><\/pre>\n<p><strong>Express with a response-specific nonce:<\/strong> this alternative uses a nonce-only script policy. Register the middleware on an existing app before HTML routes, and use the EJS template shown later. Every script you intend to authorize needs the matching nonce, including external scripts. The no-store header avoids reusing this dynamic HTML through ordinary caches; configure any CDN override consistently. This example still monitors rather than enforces CSP.<\/p>\n<pre class=\"language-python line-numbers\"><code class=\"language-python\">import crypto from &#039;node:crypto&#039;;\n\napp.use((req, res, next) =&gt; {\n  const nonce = crypto.randomBytes(16).toString(&#039;base64&#039;);\n  res.locals.cspNonce = nonce;\n  res.set(&#039;Cache-Control&#039;, &#039;no-store&#039;);\n  res.set(&#039;Reporting-Endpoints&#039;, &#039;csp-endpoint=&quot;https:\/\/example.com\/csp-report&quot;&#039;);\n  res.set(&#039;Content-Security-Policy-Report-Only&#039;,\n    `default-src &#039;self&#039;; base-uri &#039;self&#039;; object-src &#039;none&#039;; script-src &#039;nonce-${nonce}&#039;; style-src &#039;self&#039;; img-src &#039;self&#039; data:; font-src &#039;self&#039;; connect-src &#039;self&#039;; frame-ancestors &#039;self&#039;; report-to csp-endpoint; report-uri \/csp-report`);\n  next();\n});<\/code><\/pre>\n<h3><span id=\"Handling_Third-Party_Scripts_Without_Losing_Control\">Handling Third-Party Scripts Without Losing Control<\/span><\/h3>\n<p>Allow only the external origins required by the application when using a hostname policy. A broad https: source permits any HTTPS host and is not a narrow allowlist. With a nonce-only script policy, a matching nonce authorizes a script regardless of a separate hostname list; adding an allowed host would create another route to permission, not an extra check. Replace inline event handlers with supported script-based listeners rather than adding unsafe-inline by default.<\/p>\n<p>For directive behavior and policy design, use <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Content-Security-Policy\" rel=\"nofollow noopener\" target=\"_blank\">MDN\u2019s CSP documentation<\/a> and the <a href=\"https:\/\/cheatsheetseries.owasp.org\/cheatsheets\/Content_Security_Policy_Cheat_Sheet.html\" rel=\"nofollow noopener\" target=\"_blank\">OWASP CSP Cheat Sheet<\/a>. Match their examples to the resources and framework you actually operate.<\/p>\n<h2 id=\"section-4\"><span id=\"Framing_Controls_and_Clickjacking\">Framing Controls and Clickjacking<\/span><\/h2>\n<h3><span id=\"X-Frame-Options_and_CSP_frame-ancestors\">X-Frame-Options and CSP frame-ancestors<\/span><\/h3>\n<p>X-Frame-Options controls whether a document can be rendered inside a frame. <strong>DENY<\/strong> prohibits framing, and <strong>SAMEORIGIN<\/strong> requires matching origins throughout the ancestor chain. Origin includes scheme, hostname and port. <strong>ALLOW-FROM<\/strong> is obsolete. CSP <strong>frame-ancestors<\/strong> supports explicit external origins and checks every ancestor; it has no default-src fallback and must be delivered as a response header.<\/p>\n<h3><span id=\"Choose_the_intended_embedding_scope\">Choose the intended embedding scope<\/span><\/h3>\n<p>Use DENY with frame-ancestors none when the page must never be framed. The examples here choose SAMEORIGIN with frame-ancestors self to allow same-origin embedding. If a particular page must be embedded by an external application, define its allowed parent origins explicitly and test the response on that page. Loading a payment provider inside your page is a different frame-src question.<\/p>\n<h3><span id=\"Server_Config_Examples-3\">Server Config Examples<\/span><\/h3>\n<p>Nginx:<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">add_header X-Frame-Options &quot;SAMEORIGIN&quot; always;<\/code><\/pre>\n<p>Apache:<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">Header always set X-Frame-Options &quot;SAMEORIGIN&quot;<\/code><\/pre>\n<p>Express:<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">app.use((req, res, next) =&gt; {\n  res.set(&#039;X-Frame-Options&#039;, &#039;SAMEORIGIN&#039;);\n  next();\n});<\/code><\/pre>\n<p>An enforced CSP such as <code>frame-ancestors 'none'<\/code> takes precedence over X-Frame-Options in supporting browsers. Report-Only does not override it. Align DENY with none or SAMEORIGIN with self where those are intended. For external-embedding exceptions, an older client may still block the page under X-Frame-Options; retaining both headers is not automatically harmless for every use case.<\/p>\n<h2 id=\"section-5\"><span id=\"X-Content-Type-Options_and_MIME_Types\">X-Content-Type-Options and MIME Types<\/span><\/h2>\n<h3><span id=\"What_nosniff_changes\">What nosniff changes<\/span><\/h3>\n<p><strong>X-Content-Type-Options: nosniff<\/strong> tells the browser to respect the supplied Content-Type. For script and style destinations, a response with an unsuitable MIME type is blocked. It also restricts MIME sniffing in other contexts. It does not correct a misconfigured type, validate uploaded file contents or provide a general malware filter.<\/p>\n<h3><span id=\"Server_examples\">Server examples<\/span><\/h3>\n<p>Nginx:<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">add_header X-Content-Type-Options &quot;nosniff&quot; always;<\/code><\/pre>\n<p>Apache:<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">Header always set X-Content-Type-Options &quot;nosniff&quot;<\/code><\/pre>\n<p>Express:<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">app.use((req, res, next) =&gt; {\n  res.set(&#039;X-Content-Type-Options&#039;, &#039;nosniff&#039;);\n  next();\n});<\/code><\/pre>\n<h3><span id=\"Diagnose_the_actual_response_type\">Diagnose the actual response type<\/span><\/h3>\n<p>Check that CSS arrives as text\/css and JavaScript as a recognized JavaScript MIME type. A script request that receives <code>text\/plain<\/code> or an HTML error page may be blocked. A missing font or icon also warrants checks for the actual response, CORS and resource URL; do not attribute every font problem directly to nosniff.<\/p>\n<h2 id=\"section-6\"><span id=\"A_Staged_Implementation_Plan\">A Staged Implementation Plan<\/span><\/h2>\n<h3><span id=\"Step_1_Map_What_You_Actually_Serve\">Step 1: Map What You Actually Serve<\/span><\/h3>\n<p>List script, style, image, font and connection origins, inline code and required embedding relationships. Include authenticated pages and failure flows, not just the home page. Record which layer currently sets each header and how HTML responses are cached.<\/p>\n<h3><span id=\"Step_2_Start_with_HTTPS_and_HSTS\">Step 2: Start with HTTPS and HSTS<\/span><\/h3>\n<p>Verify the hostname\u2019s HTTPS endpoint, redirect path and certificate-renewal deployment. Start HSTS with a short max-age and audit subdomains before expanding the scope. Preloading is a separate commitment. See the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Strict-Transport-Security\" rel=\"nofollow noopener\" target=\"_blank\">MDN HSTS reference<\/a> for the policy lifetime and removal behavior.<\/p>\n<h3><span id=\"Step_3_Deploy_CSP_in_Report-Only_Then_Enforce\">Step 3: Deploy CSP in Report-Only, Then Enforce<\/span><\/h3>\n<p>Deploy Report-Only with a working collector and review reports alongside browser tests. Choose hashes, response-specific nonces or a deliberately scoped hostname policy to match the application. When critical flows pass, change the candidate\u2019s header name to Content-Security-Policy. A separate Report-Only policy can continue monitoring a future revision. Existing enforced policies remain active; a second CSP cannot loosen their restrictions.<\/p>\n<h3><span id=\"Step_4_Add_X-Frame-Options_and_X-Content-Type-Options\">Step 4: Add X-Frame-Options and X-Content-Type-Options<\/span><\/h3>\n<p>Apply the intended framing policy and correct Content-Type values with nosniff. Match SAMEORIGIN to frame-ancestors self, or DENY to none. Treat externally embeddable pages as explicit exceptions and account for the browsers that will use them.<\/p>\n<h3><span id=\"Step_5_Inspect_Responses_and_Browser_Behavior\">Step 5: Inspect Responses and Browser Behavior<\/span><\/h3>\n<p>Inspect a real GET response rather than relying only on HEAD requests. Replace the example URL and check relevant success, redirect and error paths:<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\"># Inspect headers for an actual GET response\ncurl -sS -D - -o \/dev\/null https:\/\/example.com\/\n\n# Select the security-header lines\ncurl -sS -D - -o \/dev\/null https:\/\/example.com\/ | grep -Ei &quot;^(strict-transport-security|content-security-policy(-report-only)?|reporting-endpoints|x-frame-options|x-content-type-options):&quot;<\/code><\/pre>\n<p>Inspect public response headers in the browser Network panel, count duplicate fields, and watch the console while exercising key flows. With a CDN, compare the public response to the origin as appropriate. curl can show headers but does not enforce CSP, perform clickjacking tests or prove that a report collector receives browser reports.<\/p>\n<h3><span id=\"Step_6_Dont_Forget_Your_Infrastructure_Basics\">Step 6: Don\u2019t Forget Your Infrastructure Basics<\/span><\/h3>\n<p>Headers complement access controls, patching and network protection. For broader maintenance, consult <a href=\"https:\/\/www.dchost.com\/blog\/en\/vps-security-ssh-firewalls-recovery\/\">VPS server hardening<\/a> and <a href=\"https:\/\/www.dchost.com\/blog\/en\/what-is-ddos-how-to-protect-your-website-from-ddos-attacks\/\">DDoS protection<\/a>. Security headers do not limit incoming traffic volume or repair a compromised application.<\/p>\n<h3><span id=\"Step_7_Consider_the_Edge_CDNs_Caches_and_Multi-Environment_Teams\">Step 7: Consider the Edge: CDNs, Caches, and Multi-Environment Teams<\/span><\/h3>\n<p>Choose which layer owns each header. A CDN set operation can replace a value, while add can create another CSP that is enforced alongside the original. A rule that changes a nonce only in the header will not update the corresponding HTML. For dynamic nonce pages, keep the generated body and header together and configure caching accordingly. Subdomain HSTS inheritance also applies independently of cookies or environment labels.<\/p>\n<p>Record the policy, collector ownership and validation steps in the deployment checklist. Retest after template, plugin, CDN or cache changes. The <a href=\"https:\/\/www.dchost.com\/blog\/en\/what-is-content-delivery-network-cdn\/\">CDN guide<\/a> explains the additional delivery layer these checks must cover.<\/p>\n<h2 id=\"section-7\"><span id=\"Complete_Starter_Examples_to_Adapt\">Complete Starter Examples to Adapt<\/span><\/h2>\n<h3><span id=\"Nginx_Short_HSTS_and_Monitored_CSP\">Nginx: Short HSTS and Monitored CSP<\/span><\/h3>\n<p>Place this set in the HTTPS server block after HTTPS is working. It permits same-origin framing and monitors a static same-origin CSP; it does not generate a nonce or permit inline code. Adapt the reporting address and configure its collector before expecting reports. Test child locations because default add_header inheritance can replace the parent set.<\/p>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">add_header Strict-Transport-Security &quot;max-age=300&quot; always;\nadd_header X-Frame-Options &quot;SAMEORIGIN&quot; always;\nadd_header X-Content-Type-Options &quot;nosniff&quot; always;\nadd_header Referrer-Policy &quot;strict-origin-when-cross-origin&quot; always;\nadd_header Reporting-Endpoints &#039;csp-endpoint=&quot;https:\/\/example.com\/csp-report&quot;&#039; always;\nadd_header Content-Security-Policy-Report-Only &quot;default-src &#039;self&#039;; base-uri &#039;self&#039;; object-src &#039;none&#039;; script-src &#039;self&#039;; style-src &#039;self&#039;; img-src &#039;self&#039; data:; font-src &#039;self&#039;; connect-src &#039;self&#039;; frame-ancestors &#039;self&#039;; report-to csp-endpoint; report-uri \/csp-report&quot; always;<\/code><\/pre>\n<p><strong>Referrer-Policy: strict-origin-when-cross-origin<\/strong> sends the full referrer for same-origin requests, only the origin for secure cross-origin requests, and no referrer on an HTTPS-to-HTTP downgrade. It is an additional policy choice, separate from the four main headers.<\/p>\n<h3><span id=\"Apache_24_with_mod_headers\">Apache 2.4 with mod_headers<\/span><\/h3>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">Header onsuccess unset Strict-Transport-Security\nHeader always set Strict-Transport-Security &quot;max-age=300&quot; &quot;expr=%{HTTPS} == &#039;on&#039;&quot;\nHeader onsuccess unset X-Frame-Options\nHeader always set X-Frame-Options &quot;SAMEORIGIN&quot;\nHeader onsuccess unset X-Content-Type-Options\nHeader always set X-Content-Type-Options &quot;nosniff&quot;\nHeader onsuccess unset Referrer-Policy\nHeader always set Referrer-Policy &quot;strict-origin-when-cross-origin&quot;\nHeader onsuccess unset Reporting-Endpoints\nHeader onsuccess unset Content-Security-Policy-Report-Only\nHeader always set Reporting-Endpoints &quot;csp-endpoint=\\&quot;https:\/\/example.com\/csp-report\\&quot;&quot;\nHeader always set Content-Security-Policy-Report-Only &quot;default-src &#039;self&#039;; base-uri &#039;self&#039;; object-src &#039;none&#039;; script-src &#039;self&#039;; style-src &#039;self&#039;; img-src &#039;self&#039; data:; font-src &#039;self&#039;; connect-src &#039;self&#039;; frame-ancestors &#039;self&#039;; report-to csp-endpoint; report-uri \/csp-report&quot;<\/code><\/pre>\n<h3><span id=\"Express_Dynamic_HTML_and_a_Nonce\">Express: Dynamic HTML and a Nonce<\/span><\/h3>\n<pre class=\"language-python line-numbers\"><code class=\"language-python\">import crypto from &#039;node:crypto&#039;;\n\napp.use((req, res, next) =&gt; {\n  const nonce = crypto.randomBytes(16).toString(&#039;base64&#039;);\n  res.locals.cspNonce = nonce;\n  res.set(&#039;Cache-Control&#039;, &#039;no-store&#039;);\n  if (req.secure) {\n    res.set(&#039;Strict-Transport-Security&#039;, &#039;max-age=300&#039;);\n  }\n  res.set(&#039;X-Frame-Options&#039;, &#039;SAMEORIGIN&#039;);\n  res.set(&#039;X-Content-Type-Options&#039;, &#039;nosniff&#039;);\n  res.set(&#039;Referrer-Policy&#039;, &#039;strict-origin-when-cross-origin&#039;);\n  res.set(&#039;Reporting-Endpoints&#039;, &#039;csp-endpoint=&quot;https:\/\/example.com\/csp-report&quot;&#039;);\n  res.set(&#039;Content-Security-Policy-Report-Only&#039;,\n    `default-src &#039;self&#039;; base-uri &#039;self&#039;; object-src &#039;none&#039;; script-src &#039;nonce-${nonce}&#039;; style-src &#039;self&#039;; img-src &#039;self&#039; data:; font-src &#039;self&#039;; connect-src &#039;self&#039;; frame-ancestors &#039;self&#039;; report-to csp-endpoint; report-uri \/csp-report`);\n  next();\n});<\/code><\/pre>\n<h3><span id=\"EJS_Template_for_the_Express_Examples\">EJS Template for the Express Examples<\/span><\/h3>\n<pre class=\"language-bash line-numbers\"><code class=\"language-bash\">&lt;script nonce=&quot;&lt;%= cspNonce %&gt;&quot;&gt;\n  \/\/ Application-owned inline initialization\n&lt;\/script&gt;\n\n&lt;script src=&quot;\/app.js&quot; nonce=&quot;&lt;%= cspNonce %&gt;&quot;&gt;&lt;\/script&gt;<\/code><\/pre>\n<p>The Express middleware must run before a route renders this EJS template so cspNonce is available from res.locals. Both script tags use the same nonce as that response\u2019s policy; the next response needs a new value. Add nonces only to application-approved scripts, never to every tag in untrusted HTML. A valid nonce authorizes the script under this policy; it does not verify the script\u2019s file integrity.<\/p>\n<h2 id=\"section-8\"><span id=\"Debugging_and_Monitoring\">Debugging and Monitoring<\/span><\/h2>\n<h3><span id=\"DevTools_First_Logs_Second\">DevTools First, Logs Second<\/span><\/h3>\n<p>Start with the public response and browser console. Then inspect the configured report collector, accounting for browser support and the report-to or legacy report-uri format. Group repeatable failures by directive, affected page and resource, and compare them with recent changes. Missing reports do not establish that no violations occurred.<\/p>\n<h3><span id=\"Edge_Cases_with_Third-Party_Widgets\">Edge Cases with Third-Party Widgets<\/span><\/h3>\n<p>Payment and identity integrations may load nested frames and resources from several origins. Identify which operation each directive governs and permit only the required behavior. A dedicated route or hostname can simplify a complex integration\u2019s policy. For unrelated WordPress runtime concerns, see <a href=\"https:\/\/www.dchost.com\/blog\/en\/wordpress-server-tuning-php-redis-mysql\/\">server-side WordPress tuning<\/a>; performance tuning does not validate CSP rules.<\/p>\n<h3><span id=\"Dont_Forget_the_Big_Picture\">Don\u2019t Forget the Big Picture<\/span><\/h3>\n<p>Use the specific failure as the starting point. For mixed content, correct stored resource URLs and verify HTTPS destinations. The upgrade-insecure-requests directive upgrades applicable requests when enforced; failed HTTPS does not fall back to HTTP, and a Report-Only policy does not perform that upgrade. The older block-all-mixed-content directive is obsolete and is omitted from these starters. Keep DNS, certificates and application maintenance in separate checks.<\/p>\n<h2 id=\"section-9\"><span id=\"Deployment_and_Maintenance_Checklist\">Deployment and Maintenance Checklist<\/span><\/h2>\n<p>Verify HTTPS, stage the HSTS lifetime, choose the required framing scope and serve correct content types. Monitor the candidate CSP with a configured collector, test the application, and enable enforcement only when those checks support it. Record the origin\/CDN ownership and cache strategy for each response.<\/p>\n<p>The Apache example uses an HTTPS expression for HSTS and clears the onsuccess header table before setting managed values in the always table. This addresses duplicate fields across Apache\u2019s two header lists; it does not prevent another proxy from adding a header later. The Express example uses req.secure, so configure proxy trust only for the actual trusted path and ensure the proxy overwrites forwarded scheme headers.<\/p>\n<p>Retest after framework, template or infrastructure changes, and include the public error paths in those checks. The guide to <a href=\"https:\/\/www.dchost.com\/blog\/en\/what-is-an-ssl-certificate-ways-to-secure-your-website\/\">SSL certificates<\/a> provides background for the HTTPS layer. Keep rollback expectations realistic, particularly for HSTS policies already stored by browsers.<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Understand the roles and limits of HTTP security headers. Use short HSTS policies, CSP Report-Only, deliberate framing rules and correct MIME types, with Nginx, Apache and Express examples to validate.<\/p>\n","protected":false},"author":1,"featured_media":8932,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[],"class_list":["post-1253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/posts\/1253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/comments?post=1253"}],"version-history":[{"count":3,"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/posts\/1253\/revisions"}],"predecessor-version":[{"id":8934,"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/posts\/1253\/revisions\/8934"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/media\/8932"}],"wp:attachment":[{"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/media?parent=1253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/categories?post=1253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dchost.com\/blog\/en\/wp-json\/wp\/v2\/tags?post=1253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}