受欢迎的博客标签

nginx: Add Custom HTTP Headers

Published

You can use custom headers for the debugging purpose. You can use NGINX as a Reverse Proxy/Load Balancer for several Apache / Lighttpd nodes. Each pair of NGINX LBs can be identified using custom headers as well as internal nodes. Another usage is to monitor your servers with custom headers.

Step 1:Add Custom HTTP Headers

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

Add the headers as follows in http, server, or location section:

add_header name value

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

add_header X-Whom www-node01


Save and close the file. Reload nginx:

# /usr/local/nginx/sbin/nginx -s reload

 

Step 2:Test It

Simply use curl as follows:

$ curl -I example.com


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