How to Add a Custom HTTP Header to All Sites on a Plesk Server
Adding custom headers is a useful way to identify, trace, or control web traffic across a hosted environment. This guide walks through the process of adding the HTTP header X-Hosted-By: ExampleHost to all websites running on a Plesk server, across both nginx and Apache layers. Why Add a Custom Header? Custom HTTP headers can: Identify the infrastructure powering your site (e.g., for branding or tracing) Assist in diagnostics or security logging Be used for custom caching or CDN logic Step-by-Step: Add Header in nginx Step 1: Copy the nginx Template First, copy the default Plesk template into a custom directory:
|
1 2 3 4 |
cd /usr/local/psa/admin/conf/templates mkdir -p custom/domain cp default/domain/nginxDomainVirtualHost.php custom/domain/ |
Step 2: Edit the Template Open the copied file:
|
1 2 |
vi custom/domain/nginxDomainVirtualHost.php |
Locate the block that outputs header directives, such as:
|
1 2 3 4 5 6 7 |
<?php foreach ((array)$VAR->domain->physicalHosting->headers as list($name, $value)): ?> add_header <?=$VAR->quote([$name, $value])?>; <?php endforeach ?> <?php if ($VAR->server->xPoweredByHeader) : ?> add_header X-Powered-By PleskLin; <?php endif ?> |
Insert your custom header right after:
|
1 2 3 4 |
<?php /* CUSTOM HEADER START */ ?> add_header X-Hosted-By "ExampleHost" always; <?php /* CUSTOM HEADER END */ ?> |
Step 3: Rebuild nginx Configurations
|
1 2 |
/usr/local/psa/admin/sbin/httpdmng --reconfigure-all |
Step 4: Restart nginx
|
1 2 |
systemctl restart nginx |
Step-by-Step: Add Header in Apache Step 1: Copy the Apache Template