
{"id":26630,"date":"2025-08-22T14:51:46","date_gmt":"2025-08-22T12:51:46","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=26630"},"modified":"2026-01-16T13:00:52","modified_gmt":"2026-01-16T12:00:52","slug":"how-to-set-up-a-content-security-policy-csp","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/how-to-set-up-a-content-security-policy-csp\/","title":{"rendered":"How to Set Up a Content Security Policy (CSP)\u00a0"},"content":{"rendered":"\n<p>A Content Security Policy, or CSP, helps protect your website from cross-site scripting attacks. It limits which resources the browser can load and adds an important layer of defense to your <a href=\"https:\/\/contabo.com\/en\/vps\/\" target=\"_blank\" rel=\"noreferrer noopener\">VPS setup<\/a>. In this article, you learn how to configure CSP, test your rules safely, and improve your site\u2019s overall security with additional headers.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-a-content-security-policy-does\">What a Content Security Policy Does\u00a0<\/h2>\n\n\n\n<p>CSP acts like a gatekeeper. It lets the browser know which scripts, images, or styles to trust. When a suspicious resource tries to load, the browser blocks it. Because of this behavior, CSP reduces the risk of unwanted scripts being executed on your site.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-add-a-basic-csp-header\">Add a Basic CSP Header\u00a0<\/h2>\n\n\n\n<p>You can start with a simple configuration. When using Apache, open your configuration file and add the following line:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header set Content-Security-Policy \"default-src 'self'; script-src 'self' <a href=\"https:\/\/trusted.cdn.com\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">https:\/\/trusted.cdn.com<\/a>\"&nbsp;<\/code><\/pre>\n\n\n\n<p>This rule tells the browser to load scripts only from your domain and a trusted <a href=\"https:\/\/contabo.com\/blog\/how-to-implement-cdn\/\" target=\"_blank\" rel=\"noreferrer noopener\">CDN<\/a>. After you save the file, reload your server and refresh your browser to apply the changes.&nbsp;<\/p>\n\n\n\n<p>If you use <a href=\"https:\/\/contabo.com\/blog\/what-is-nginx\/\" target=\"_blank\" rel=\"noreferrer noopener\">NGINX<\/a>, add this header instead:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_header Content-Security-Policy \"default-src 'self'; script-src 'self' <a href=\"https:\/\/trusted.cdn.com\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">https:\/\/trusted.cdn.com<\/a>\";&nbsp;<\/code><\/pre>\n\n\n\n<p>Then run:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl reload nginx&nbsp;<\/code><\/pre>\n\n\n\n<p>Reloading ensures your new security settings take effect.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-test-safely-with-report-only-mode\">Test Safely with Report-Only Mode\u00a0<\/h2>\n\n\n\n<p>If you want to test your policy before enforcing it, enable CSP Report-Only mode. It reports violations without blocking anything:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Content-Security-Policy-Report-Only: default-src 'self'; report-uri \/csp-report-endpoint&nbsp;<\/code><\/pre>\n\n\n\n<p>This approach helps you see what your future policy would block. You can check violations through your browser\u2019s developer tools and adjust the rule set until everything works smoothly.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-block-mixed-content\">Block Mixed Content\u00a0<\/h2>\n\n\n\n<p>Mixed content happens when your HTTPS site loads elements over HTTP. Modern browsers block some of these requests automatically. To avoid issues, update all resource URLs to HTTPS. Then let CSP enforce this behavior by keeping your allowed sources strictly secure.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-combine-csp-with-other-security-headers\">Combine CSP with Other Security Headers\u00a0<\/h2>\n\n\n\n<p>CSP is powerful on its own, yet it works even better when combined with other protections. Add the following headers to improve your site\u2019s security:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Strict-Transport-Security: max-age=31536000; includeSubDomains&nbsp;\nX-Content-Type-Options: nosniff&nbsp;\nX-Frame-Options: DENY&nbsp;<\/code><\/pre>\n\n\n\n<p>Together, these headers strengthen user trust and increase your website\u2019s safety. If you feel unsure about where to begin, you can use tools like <a href=\"https:\/\/developer.mozilla.org\/en-US\/observatory\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Mozilla Observatory<\/a> to create a ready-to-use policy.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-watch-our-youtube-video-on-setting-up-csp\">Watch Our YouTube Video on Setting Up CSP\u00a0<\/h2>\n\n\n\n<p>If you prefer a visual walkthrough, check out the connected YouTube video that this script is based on.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container \" style=\"height: 100%;\"><iframe loading=\"lazy\" title=\"Setting up a content security policy (CSP)\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/oyScLJH9sTs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion\u00a0<\/h2>\n\n\n\n<p>CSP is one of the most effective defenses against cross-site scripting attacks. You can implement it quickly, test it safely, and enhance it with related headers for stronger protection. These steps help secure your <a href=\"https:\/\/contabo.com\/en\/vps\/\" target=\"_blank\" rel=\"noreferrer noopener\">VPS setup<\/a> and reduce risks caused by untrusted scripts.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Content Security Policy, or CSP, helps protect your website from cross-site scripting attacks. It limits which resources the browser can load and adds an important layer of defense to your VPS setup. In this article, you learn how to configure CSP, test your rules safely, and improve your site\u2019s overall security with additional headers.&nbsp; [&hellip;]<\/p>\n","protected":false},"author":77,"featured_media":26634,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[],"ppma_author":[3116],"class_list":["post-26630","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"uagb_featured_image_src":{"full":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2025\/12\/How-to-Enhance-cPanel-Account-Security-with-Recommended-Settings-1.png",1200,630,false],"thumbnail":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2025\/12\/How-to-Enhance-cPanel-Account-Security-with-Recommended-Settings-1-150x150.png",150,150,true],"medium":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2025\/12\/How-to-Enhance-cPanel-Account-Security-with-Recommended-Settings-1-600x315.png",600,315,true],"medium_large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2025\/12\/How-to-Enhance-cPanel-Account-Security-with-Recommended-Settings-1-768x403.png",768,403,true],"large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2025\/12\/How-to-Enhance-cPanel-Account-Security-with-Recommended-Settings-1.png",1200,630,false],"1536x1536":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2025\/12\/How-to-Enhance-cPanel-Account-Security-with-Recommended-Settings-1.png",1200,630,false],"2048x2048":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2025\/12\/How-to-Enhance-cPanel-Account-Security-with-Recommended-Settings-1.png",1200,630,false]},"uagb_author_info":{"display_name":"Anika Kopte","author_link":"https:\/\/contabo.com\/blog\/author\/anika\/"},"uagb_comment_info":0,"uagb_excerpt":"A Content Security Policy, or CSP, helps protect your website from cross-site scripting attacks. It limits which resources the browser can load and adds an important layer of defense to your VPS setup. In this article, you learn how to configure CSP, test your rules safely, and improve your site\u2019s overall security with additional headers.&nbsp;&hellip;","authors":[{"term_id":3116,"user_id":77,"is_guest":0,"slug":"anika","display_name":"Anika Kopte","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/1c425caa652c679ae47e3f85a48de4e19f09d37bcb5593ba88a7aa4a08bb1d81?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/26630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=26630"}],"version-history":[{"count":2,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/26630\/revisions"}],"predecessor-version":[{"id":27422,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/26630\/revisions\/27422"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media\/26634"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=26630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=26630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=26630"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=26630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}