How do you send a header to your Web server on a Apple OS X or Unix or Linux based system using a curl command line option for testing and debugging my web apps or server nodes behind a load balancer?
The curl command supports -H or --headeroption to pass extra HTTP header to use when getting a web page from your web server. You may specify any number of extra headers. When you add a custom header that has the same name as one of the internal ones curl would use, your externally set header will be used instead of the internal one. The syntax is:
curl -H 'YOUR-EXTRA-HEDER-HERE' apache-server-ip
curl -H 'YOUR-EXTRA-HEDER-1-HERE' -H 'YOUR-EXTRA-HEDER-2-HERE' blog.urfix.com
For example, send Host header to blog.urfix.com to get HTML response from 75.126.153.206:80, run:
curl -H 'Host: blog.urfix.com' 71.201.204.10:80
This is also useful when 75.126.153.206 has multiple virtual host set. The default is not to response anything when virtual host header is not sent:
$ curl -I 75.126.153.206:80
Sample outputs:
HTTP/1.1 200 OK
Date: Sat, 03 Nov 2012 20:09:20 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.4
Expires: Sat, 27 Oct 2012 20:09:20 GMT
Last-Modified: Sat, 03 Nov 2012 20:09:20 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
X-Pingback: http://blog.urfix.com/xmlrpc.php
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
Now, sent blog.urfix.com as Host header:
$ curl -I -H 'Host: blog.urfix.com' curl -H 'Host: blog.urfix.com' 71.201.204.10:80
Sample outputs:
HTTP/1.1 200 OK
Date: Sat, 03 Nov 2012 20:13:22 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.4
Expires: Sat, 27 Oct 2012 20:13:22 GMT
Last-Modified: Sat, 03 Nov 2012 20:13:22 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
X-Pingback: http://blog.urfix.com/xmlrpc.php
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
You can use this command to test Apache server node behind a load balancer (only work with your own VLAN/LAN setup):
## see if 192.168.1.11:95 apache node #1 is working or not ##
curl -I --header 'Host: blog.urfix.com' 'http://192.168.1.11:95/'
Sample outputs:
HTTP/1.1 200 OK
X-Whom: urfix.com
Vary: Cookie
Last-Modified: Wed, 31 Oct 2012 18:54:00 GMT
Cache-Control: max-age=77, must-revalidate
Content-type: text/html
Date: Wed, 31 Oct 2012 18:57:43 GMT
Server: lighttpd
This option can be used multiple times to add/replace/remove multiple headers:
curl blog.urfix.com \
-H "Accept-Language: en" \
-H "Host blog.urfix.com" \
-H "User-Agent: curl"
References
See curl command man page for more options:
$ man curl