Linux/Unix:curl命令传递主机头

时间:2020-01-09 10:42:03  来源:igfitidea点击:

如何在Apple OS X或者基于Unix或者Linux的系统上使用curl命令行选项将标头发送到Web服务器(例如Nginx/Lighttpd/Apache/ISS),以测试和调试Web应用程序或者服务器节点后负载均衡器?

curl命令支持-H或者--header选项以传递额外的HTTP标头,以便从Web服务器获取网页时使用。您可以指定任意数量的额外标题。当您添加的自定义标头与curl使用的内部标头之一具有相同的名称时,将使用外部设置的标头代替内部标头。语法为:

curl -H 'YOUR-EXTRA-HEADER-HERE' apache-server-ip
curl -H 'YOUR-EXTRA-HEADER-1-HERE' -H 'YOUR-EXTRA-HEADER-2-HERE' www.theitroad.local

例如,将Host标头发送到www.theitroad.local以获得来自75.126.153.206:80的HTML响应,运行:

curl -H 'Host: www.theitroad.local' 75.126.153.206:80

当75.126.153.206具有多个虚拟主机集时,这也很有用。
默认值是不发送虚拟主机头时不响应任何内容:

$ curl -I 75.126.153.206:80

输出示例:

HTTP/1.1 500 Internal Server Error
Server: nginx
Date: Wed, 31 Oct 2012 18:51:03 GMT
Content-Type: text/html
Connection: keep-alive
X-Whom: l1-com-cyber

现在,将www.theitroad.local作为主机标头发送:

$ curl -I -H 'Host: www.theitroad.local' 75.126.153.206:80

输出示例:

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 31 Oct 2012 18:52:20 GMT
Content-Type: text/html
Connection: keep-alive
X-Whom: l2-com-cyber
Vary: Cookie
Last-Modified: Wed, 31 Oct 2012 18:48:58 GMT
Cache-Control: max-age=98, must-revalidate
X-Galaxy: Andromeda-1
X-Origin-Type: DynamicViaDAL

您可以使用此命令来测试负载均衡器后面的Apache服务器节点(仅适用于您自己的VLAN/LAN设置):

## see if 192.168.1.11:95 apache node #1 is working or not ##
curl -I --header 'Host: www.theitroad.local' 'http://192.168.1.11:95/'

输出示例:

HTTP/1.1 200 OK
X-Whom: l1-com-cyber
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

可以多次使用此选项来添加/替换/删除多个标题:

curl www.theitroad.local \ 
-H "Accept-Language: en" \ 
-H "Host www.theitroad.local" \ 
-H "User-Agent: curl"