Add custom HTTP headers through CloudNS / Nginx


Written by Dennis Nind

Last published at: February 26th, 2019

How do I send or set arbitrary HTTP headers using the Nginx web server at Brixly?

You need to use the add_header directive. It sends arbitrary HTTP header when the response code is equal to 200, 204, 301, 302 or 304. 

The following lines can be added directly to your nginx.conf file (located in the public_html)

add_header name value

In this example, set X-Whom to www-node01:

add_header X-Whom www-node01

How Do I Test It?

Simply use curl as follows:

$ curl -I

Sample outputs:

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 15 Mar 2010 10:40:58 GMT
Content-Type: text/html; charset=ISO-8859-1
Connection: keep-alive
X-Whom: www-node01

How Do I Apply the Changes?

Using our CloudNS nginx module, it is possible to apply your own custom 'nginx directives' (which are the nginx equivalents to .htaccess rules for Apache) 

To enable a custom nginx configuration, please create / upload a file named 'nginx.conf' in the public_html directory of your account 

example: /home/user/public_html/nginx.conf 

Within this file, you would enter the configuration / directives that would normally reside within the 'server{}' tags of a standard nginx configuration file. 

Once this has been done, you can activate the configuration for your domain using the CloudNS plugin in cPanel... 

cPanel -> CloudNS -> Server Settings -> user_config (set to 'enabled')