{"id":710,"date":"2019-08-06T16:55:30","date_gmt":"2019-08-06T08:55:30","guid":{"rendered":"https:\/\/www.zhuyanbin.com\/?p=710"},"modified":"2019-08-06T18:22:54","modified_gmt":"2019-08-06T10:22:54","slug":"ngnix%e4%bb%a3%e7%90%86%e4%b8%ad%e7%9a%84proxy_set_header%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/www.yanbin888.com\/?p=710","title":{"rendered":"Ngnix\u4ee3\u7406\u4e2d\u7684proxy_set_header\u8bbe\u7f6e"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">proxy_set_header\u5b98\u7f51\u8bf4\u660e<\/h2>\n\n\n\n<p>\u5b98\u7f51\u5173\u4e8eproxy_set_header\u7684\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Syntax:   <code><strong>proxy_set_header<\/strong>&nbsp;<em>field<\/em>&nbsp;<em>value<\/em>;<\/code>\nDefault:  proxy_set_header Host $proxy_host;\n          proxy_set_header Connection close;\nContext:  <code>http<\/code>,&nbsp;<code>server<\/code>,&nbsp;<code>location<\/code><\/pre>\n\n\n\n<p>Allows redefining or appending fields to the request header&nbsp;<a href=\"http:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#proxy_pass_request_headers\">passed<\/a>&nbsp;to the proxied server. The&nbsp;<code><em>value<\/em><\/code>&nbsp;can contain text, variables, and their combinations. These directives are inherited from the previous level if and only if there are no&nbsp;<code>proxy_set_header<\/code>&nbsp;directives defined on the current level. By default, only two fields are redefined:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">proxy_set_header Host       $proxy_host;\nproxy_set_header Connection close;<\/pre>\n\n\n\n<p>If caching is enabled, the header fields \u201cIf-Modified-Since\u201d, \u201cIf-Unmodified-Since\u201d, \u201cIf-None-Match\u201d, \u201cIf-Match\u201d, \u201cRange\u201d, and \u201cIf-Range\u201d from the original request are not passed to the proxied server.<\/p>\n\n\n\n<p>An unchanged \u201cHost\u201d request header field can be passed like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">proxy_set_header Host       $http_host;<\/pre>\n\n\n\n<p>However, if this field is not present in a client request header then nothing will be passed. In such a case it is better to use the&nbsp;<code>$host<\/code>&nbsp;variable&nbsp;&#8211; its value equals the server name in the \u201cHost\u201d request header field or the primary server name if this field is not present:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">proxy_set_header Host       $host;<\/pre>\n\n\n\n<p>In addition, the server name can be passed together with the port of the proxied server:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">proxy_set_header Host       $host:$proxy_port;<\/pre>\n\n\n\n<p>f the value of a header field is an empty string then this field will not be passed to a proxied server:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">proxy_set_header Accept-Encoding \"\";<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u51e0\u4e2a\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\uff1a<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>$host\u3001$proxy_host\u548c$http_host\u533a\u522b<\/li><li>Http\u534f\u8baeheader\u5934\u4e2d\u7684host\u5bf9Nginx\u7684\u5f71\u54cd<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">$host\u3001$proxy_host\u548c$http_host\u533a\u522b<\/h2>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u65b9\u4fbf\u7684\u8bf4\u660e\u8fd9\u4e09\u8005\u4e4b\u95f4\u7684\u533a\u522b\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u8f6c\u53d1\u8303\u4f8b\u8fdb\u884c\u8bf4\u660e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    upstream myapp1 {\n        server app.example.com;\n    }\n\n    server {\n        listen 80;\n        server_name  proxy1.test.com;\n        location \/ {\n            proxy_pass http:\/\/myapp1;\n            proxy_set_header Host $host;\n        }\n    }\n\n    server {\n        listen 80;\n        server_name  proxy2.test.com;\n        location \/ {\n            proxy_pass http:\/\/myapp1;\n            proxy_set_header Host $proxy_host;\n        }\n    }\n\n   server {\n        listen 80;\n        server_name  proxy3.test.com;\n        location \/ {\n            proxy_pass http:\/\/myapp1;\n            proxy_set_header Host $http_host;\n        }\n    }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>$host  \u6309\u7167\u5982\u4e0b\u4f18\u5148\u7ea7\u83b7\u5f97<ul><li>\u8bf7\u6c42\u884c\u4e2d\u7684host<\/li><li>\u8bf7\u6c42\u5934\u4e2d\u7684Host\u5934\u90e8<\/li><li>\u4e0e\u4e00\u6761\u8bf7\u6c42\u5339\u914d\u7684server name<\/li><\/ul><\/li><li>$proxy_host \u9ed8\u8ba4\u503c\uff0c\u5373\u4ee3\u7406\u7684\u548c\u8f6c\u53d1\u7684host<\/li><li>$http_host  \u8bf7\u6c42\u5934\u4e2d\u8bfb\u53d6\u5230\u7684Host<\/li><\/ul>\n\n\n\n<p>\u8303\u4f8b1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET \\\n  http:\/\/proxy1.test.com\/ \\\n  -H 'Accept: *\/*' \\\n  -H 'Accept-Encoding: gzip, deflate' \\\n  -H 'Cache-Control: no-cache' \\\n  -H 'Connection: keep-alive' \\\n  -H 'Host: proxy1.test.com' \\\n  -H 'Referer: http:\/\/proxy1.test.com\/' \\\n  -H 'cache-control: no-cache'<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6$host\u7684\u503c\u4e3aproxy1.test.com<\/p>\n\n\n\n<p>\u8303\u4f8b2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET \\\n  http:\/\/proxy2.test.com\/ \\\n  -H 'Accept: *\/*' \\\n  -H 'Accept-Encoding: gzip, deflate' \\\n  -H 'Cache-Control: no-cache' \\\n  -H 'Connection: keep-alive' \\\n  -H 'Host: proxy2.test.com' \\\n  -H 'Referer: http:\/\/proxy1.test.com\/' \\\n  -H 'cache-control: no-cache'<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6$proxy_host\u7684\u503c\u4e3aapp.example.com<\/p>\n\n\n\n<p>\u8303\u4f8b2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X GET \\\n  http:\/\/proxy3.test.com\/ \\\n  -H 'Accept: *\/*' \\\n  -H 'Accept-Encoding: gzip, deflate' \\\n  -H 'Cache-Control: no-cache' \\\n  -H 'Connection: keep-alive' \\\n  -H 'Host: proxy3.test.com' \\\n  -H 'Referer: http:\/\/proxy1.test.com\/' \\\n  -H 'cache-control: no-cache'<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6$http_host\u7684\u503c\u4e3aproxy3.test.com<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">header\u5934\u4e2d\u7684Host\u5bf9Nginx\u4ee3\u7406\u7684\u5f71\u54cd<\/h2>\n\n\n\n<p>Nginx\u5728\u505a\u4ee3\u7406\u670d\u52a1\u65f6\uff0c\u901a\u5e38\u4f1a\u542f\u7528\u591a\u4e2aserver\uff0c\u8bf7\u6c42\u4f1a\u6839\u636eserver name\uff0c\u8fdb\u5165\u76f8\u5e94\u7684server\u914d\u7f6e\u6267\u884c\u6d41\u7a0b\uff0c\u800cHost\u5c31\u662fNginx\u8fdb\u5165\u4e0d\u540cserver\u6267\u884c\u6d41\u7a0b\u7684\u4f9d\u636e\uff0c\u56e0\u6b64\uff0c\u5728\u591aserver name\u7684\u914d\u7f6e\u4e2d\uff0cHost\u7684\u6b63\u786e\u81f3\u5173\u91cd\u8981\uff0c\u4e5f\u8981\u6c42\u6211\u4eec\u5728\u6784\u5efaHttp\u8bf7\u6c42\u65f6\uff0c\u4e00\u5b9a\u89c4\u8303\u7684\u4f7f\u7528Host\u8fd9\u4e2aheader\u5934\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>proxy_set_header\u5b98\u7f51\u8bf4\u660e \u5b98\u7f51\u5173\u4e8eproxy_set_header\u7684\u8bf4\u660e\u5982\u4e0b\uff1a Syntax: <span class=\"ellipsis\">&hellip;<\/span> <span class=\"more-link-wrap\"><a href=\"https:\/\/www.yanbin888.com\/?p=710\" class=\"more-link\"><span>Read More &rarr;<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[21],"class_list":["post-710","post","type-post","status-publish","format-standard","hentry","category-nginx","tag-nginx"],"_links":{"self":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=710"}],"version-history":[{"count":10,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/710\/revisions"}],"predecessor-version":[{"id":721,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/710\/revisions\/721"}],"wp:attachment":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}