{"id":226514,"date":"2021-09-24T08:59:11","date_gmt":"2021-09-24T00:59:11","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=226514"},"modified":"2021-09-14T09:00:07","modified_gmt":"2021-09-14T01:00:07","slug":"go-bind-host","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/go-bind-host.html","title":{"rendered":"Go \u4ee3\u7801\u4e2d\u5982\u4f55\u7ed1\u5b9a Host"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u516c\u53f8\u5185\u90e8\uff0c\u4e3a\u4e86\u5b89\u5168\uff0c\u53ef\u80fd\u4f1a\u6709\u57df\u540d\u662f\u9700\u8981\u7ed1\u5b9a host \u624d\u80fd\u8bbf\u95ee\u7684\uff0c\u5982\u679c\u662f\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\uff0c\u6211\u4eec\u4f1a\u4fee\u6539\u672c\u673a\u7684 hosts \u6587\u4ef6;\u7136\u800c\uff0c\u5982\u679c\u662f\u8981\u901a\u8fc7\u7a0b\u5e8f\u8bbf\u95ee\u8fd9\u6837\u7684\u57df\u540d\uff0c\u6211\u4eec\u662f\u5426\u4f9d\u7136\u5fc5\u987b\u7ed1\u5b9a host \u5462?
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u5728\u516c\u53f8\u5185\u90e8\uff0c\u4e3a\u4e86\u5b89\u5168\uff0c\u53ef\u80fd\u4f1a\u6709\u57df\u540d\u662f\u9700\u8981\u7ed1\u5b9a host \u624d\u80fd\u8bbf\u95ee\u7684\uff0c\u5982\u679c\u662f\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\uff0c\u6211\u4eec\u4f1a\u4fee\u6539\u672c\u673a\u7684 hosts \u6587\u4ef6;\u7136\u800c\uff0c\u5982\u679c\u662f\u8981\u901a\u8fc7\u7a0b\u5e8f\u8bbf\u95ee\u8fd9\u6837\u7684\u57df\u540d\uff0c\u6211\u4eec\u662f\u5426\u4f9d\u7136\u5fc5\u987b\u7ed1\u5b9a host \u5462?\u7b54\u6848\u5f53\u7136\u662f\u5426\u5b9a\u7684\uff0c\u800c\u4e14\uff0c\u4f9d\u8d56\u672c\u5730\u7ed1\u5b9a\u7684 host\uff0c\u7a0b\u5e8f\u5230\u5176\u4ed6\u673a\u5668\u90e8\u7f72\uff0c\u4e5f\u5fc5\u987b\u5728\u90a3\u53f0\u673a\u5668\u7ed1\u5b9a host\uff0c\u5982\u679c\u673a\u5668\u5f88\u591a\u5462?<\/p>\n

\u521a\u597d\u524d\u4e24\u5929\uff0c\u7fa4\u91cc\u4e5f\u6709\u4eba\u95ee\u7c7b\u4f3c\u7684\u95ee\u9898<\/p>\n

\u672c\u6587\u793a\u4f8b\uff1a<\/p>\n

  • IP\uff1a192.168.1.102\uff0c\u4e5f\u5c31\u662f\u8bf4\u9700\u8981\u8bbf\u95ee\u8fd9\u53f0\u673a\u5668\u4e0a\u7684\u8d44\u6e90<\/li>\n
  • \u57df\u540d\uff1astudygolang.com\uff0cnginx \u914d\u7f6e\u7684\u865a\u62df\u4e3b\u673a<\/li>\n
  • url path\uff1a\/testhost.txt\uff0c\u5185\u5bb9\u662f\uff1aWelcome to studygolang.com<\/li>\n

    \u9700\u6c42\uff1a\u9700\u8981\u8bf7\u6c42\u670d\u52a1\u5668\u4e0a\u7684 testhost.txt \u8d44\u6e90\u3002<\/p>\n

    1\u3001Linux Shell \u7684\u89e3\u51b3\u65b9\u6848<\/strong><\/div>\n

    Linux \u4e0b\u7684 curl \u7a0b\u5e8f\u53ef\u4ee5\u7ed1\u5b9a host\uff0c\u56e0\u6b64\uff0c\u5728 shell \u4e2d\u53ef\u4ee5\u5f88\u7b80\u5355\u7684\u5b9e\u73b0\uff0c\u5982\uff1acurl -H \"Host:studygolang.com\" http:\/\/192.168.1.102\/testhost.txt\u3002<\/p>\n

    2\u3001PHP \u7684\u89e3\u51b3\u65b9\u6848<\/strong><\/div>\n

    1)\u901a\u8fc7 curl \u6269\u5c55\u5b9e\u73b0<\/p>\n

    \r\n$ch = curl_init(); \r\ncurl_setopt($ch, CURLOPT_HTTPHEADER, array('Host:studygolang.com')); \r\ncurl_setopt($ch, CURLOPT_URL, 'http:\/\/192.168.1.102\/testhost.txt'); \r\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); \r\n$ret = curl_exec($ch); \r\nvar_dump($ret); <\/pre>\n

    2)\u4e0d\u4f9d\u8d56 curl \u6269\u5c55\u7684\u65b9\u5f0f<\/p>\n

    \r\n\r\n\/\/ Create a stream \r\n$opts = array( \r\n    'http'=>array( \r\n        'method'=>\"GET\", \r\n        'header'=>\"Host:studygolang.com\" \r\n    ) \r\n); \r\n \r\n$context = stream_context_create($opts); \r\n \r\n\/\/ Open the file using the HTTP headers set above \r\n$ret = file_get_contents('http:\/\/192.168.1.102\/testhost.txt', false, $context); \r\nvar_dump($ret); <\/pre>\n
    3\u3001Golang \u7684\u89e3\u51b3\u65b9\u6848<\/strong><\/div>\n

    \u7531\u4e8e Go \u6807\u51c6\u5e93\u5b9e\u73b0\u4e86 http \u534f\u8bae\uff0c\u5728 net\/http \u5305\u4e2d\u5bfb\u627e\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n

    \u4e00\u822c\u7684\uff0c\u8bf7\u6c42\u4e00\u4e2a url\uff0c\u6211\u4eec\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n

    http.Get(url) <\/pre>\n

    \u7136\u800c\uff0c\u9488\u5bf9\u672c\u6587\u8bf4\u5230\u7684\u8fd9\u79cd\u60c5\u51b5\uff0c\u65e0\u8bba url = \"http:\/\/192.168.1.102\/testhost.txt\" \u8fd8\u662f url = \"http:\/\/studygolang.com\/testhost.txt\"\uff0c\u90fd\u65e0\u6cd5\u8bf7\u6c42\u5230\u8d44\u6e90(\u6ca1\u6709\u7ed1\u5b9a host \u7684\u60c5\u51b5)\u3002<\/p>\n

    \u5728 http \u5305\u4e2d\u7684 Request \u7ed3\u6784\u4e2d\uff0c\u6709\u4e00\u4e2a\u5b57\u6bb5\uff1aHost\uff0c\u6211\u4eec\u53ef\u4ee5\u53c2\u8003\u4e0a\u9762\u4e24\u79cd\u89e3\u51b3\u65b9\u6848\uff0c\u8bbe\u7f6e Host \u7684\u503c\u3002\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n

    \r\n\r\npackage main \r\n \r\nimport ( \r\n    \"net\/http\" \r\n    \"io\/ioutil\" \r\n    \"fmt\" \r\n) \r\n \r\nfunc main() { \r\n    req, err := http.NewRequest(\"GET\", \"http:\/\/192.168.1.102\/testhost.txt\", nil) \r\n    if err != nil { \r\n        panic(err) \r\n    } \r\n    req.Host = \"studygolang.com\" \r\n    resp, err := http.DefaultClient.Do(req) \r\n    if err != nil { \r\n        panic(err) \r\n    } \r\n    defer resp.Body.Close() \r\n    body, err := ioutil.ReadAll(resp.Body) \r\n    if err != nil { \r\n        panic(err) \r\n    } \r\n    fmt.Println(string(body)) \r\n} <\/pre>\n
    4\u3001\u603b\u7ed3<\/strong><\/div>\n

    \u4e0d\u7ba1\u662f\u4ec0\u4e48\u65b9\u5f0f\u3001\u4ec0\u4e48\u8bed\u8a00\uff0c\u5f52\u6839\u7ed3\u5e95\uff0c\u9700\u8981\u544a\u77e5\u670d\u52a1\u5668\u8bf7\u6c42\u7684\u662f\u54ea\u4e2a Host\uff0c\u8fd9\u4e2a\u662f HTTP \u534f\u8bae\u7684 Host \u5934\u3002\u5982\u679c\u4e0d\u624b\u52a8\u8bbe\u7f6e Host \u5934\uff0c\u5219\u4f1a\u4ece\u8bf7\u6c42\u7684 url \u4e2d\u83b7\u53d6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u5728\u516c\u53f8\u5185\u90e8\uff0c\u4e3a\u4e86\u5b89\u5168\uff0c\u53ef\u80fd\u4f1a\u6709\u57df\u540d\u662f\u9700\u8981\u7ed1\u5b9a host \u624d\u80fd\u8bbf\u95ee\u7684\uff0c\u5982\u679c\u662f\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\uff0c\u6211\u4eec\u4f1a\u4fee\u6539\u672c\u673a\u7684 h […]<\/p>\n","protected":false},"author":317,"featured_media":211222,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-226514","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/226514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=226514"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/226514\/revisions"}],"predecessor-version":[{"id":226517,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/226514\/revisions\/226517"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/211222"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=226514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=226514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=226514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}