{"id":1820,"date":"2023-03-28T22:36:36","date_gmt":"2023-03-28T14:36:36","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1820"},"modified":"2023-04-22T09:21:37","modified_gmt":"2023-04-22T01:21:37","slug":"enable-proxy-protocol-for-nginx-when-using-aws-elb-service","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/28\/enable-proxy-protocol-for-nginx-when-using-aws-elb-service\/","title":{"rendered":"\u4f7f\u7528AWS\u7684ELB\u670d\u52a1\u65f6\u4e3aNginx\u542f\u7528\u4ee3\u7406\u534f\u8bae"},"content":{"rendered":"<p>\u5728\u4f7f\u7528AWS\u4e91\u670d\u52a1\u7684\u65f6\u5019\uff0c90%\u8981\u4f7f\u7528ELB\u670d\u52a1\u4f5c\u4e3a\u8d1f\u8f7d\u5747\u8861\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f7f\u7528ELB\u8981\u6bd4\u81ea\u5df1\u642d\u5efa\u8d1f\u8f7d\u5747\u8861\u8981\u65b9\u4fbf\u5f97\u591a\u3002\u4e3b\u8981\u597d\u5904\u6709\uff1a<\/p>\n<ol>\n<li>\u53ef\u4ee5\u968f\u65f6\u76d1\u63a7\u5b9e\u4f8b\u7684\u5065\u5eb7\u72b6\u6001<\/li>\n<li>\u5f53\u670d\u52a1\u5668\u4e0d\u6b63\u5e38\u65f6\uff0cELB\u7684\u62a5\u8b66\u7b56\u7565\u5c06\u81ea\u52a8\u53d1\u9001\u90ae\u4ef6\u901a\u77e5\u8fd0\u7ef4\u4eba\u5458<\/li>\n<li>\u5f53\u670d\u52a1\u5668\u8d1f\u8f7d\u5230\u8fbe\u9608\u503c\u65f6\uff0c\u901a\u8fc7auto scanning\u5c06\u81ea\u52a8\u52a0\u5165\u65b0\u7684\u670d\u52a1\u5668\u5230\u96c6\u7fa4\u4e2d\uff0c\u540c\u65f6\u8d1f\u8f7d\u964d\u4e0b\u53bb\u540e\u5c06\u81ea\u52a8\u5173\u95ed\u591a\u4f59\u7684\u5b9e\u4f8b<\/li>\n<li>ELB\u7684\u5404\u9879\u76d1\u63a7\u6307\u6807\u8f83\u597d\u5730\u5e2e\u52a9\u5224\u65ad\u670d\u52a1\u5668\u6027\u80fd<\/li>\n<\/ol>\n<p><!-- more --><\/p>\n<p>Enable proxy protocol using the AWS CLI\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/docs.aws.amazon.com\/elasticloadbalancing\/latest\/classic\/enable-proxy-protocol.html#enable-proxy-protocol-cli\">https:\/\/docs.aws.amazon.com\/elasticloadbalancing\/latest\/classic\/enable-proxy-protocol.html#enable-proxy-protocol-cli<\/a><br \/>\nListener configurations for Classic Load Balancers\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/docs.aws.amazon.com\/elasticloadbalancing\/latest\/classic\/using-elb-listenerconfig-quickref.html\">https:\/\/docs.aws.amazon.com\/elasticloadbalancing\/latest\/classic\/using-elb-listenerconfig-quickref.html<\/a><br \/>\nHTTP headers and Classic Load Balancers\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/docs.aws.amazon.com\/elasticloadbalancing\/latest\/classic\/x-forwarded-headers.html\">https:\/\/docs.aws.amazon.com\/elasticloadbalancing\/latest\/classic\/x-forwarded-headers.html<\/a><\/p>\n<h2>AWS ELB nginx \u542f\u7528\u4ee3\u7406\u534f\u8bae<\/h2>\n<p>\u8981\u4f7f\u7528aws elb\u670d\u52a1\u5668\u6765\u505awebsocket\u8d1f\u8f7d\u5747\u8861\u65f6\uff0c\u53ea\u80fd\u4f7f\u7528tcp\u6a21\u5f0f\u3002<\/p>\n<p>\u4ee3\u7406\u534f\u8bae\u662f\u4e00\u79cd Internet \u534f\u8bae\uff0c\u7528\u4e8e\u5c06\u8fde\u63a5\u4fe1\u606f\u4ece\u8bf7\u6c42\u8fde\u63a5\u7684\u6e90\u4f20\u9012\u5230\u8bf7\u6c42\u8fde\u63a5\u5230\u7684\u76ee\u6807\u3002Elastic Load Balancing \u4f7f\u7528\u4ee3\u7406\u534f\u8bae\u7248\u672c 1\uff0c\u8be5\u7248\u672c\u4f7f\u7528\u7528\u6237\u53ef\u8bfb\u7684\u6807\u5934\u683c\u5f0f\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u5bf9\u524d\u7aef\u548c\u540e\u7aef\u8fde\u63a5\u4f7f\u7528\u4f20\u8f93\u63a7\u5236\u534f\u8bae (TCP) \u6216\u5b89\u5168\u5957\u63a5\u5b57\u5c42 (SSL) \u65f6\uff0c\u60a8\u7684\u8d1f\u8f7d\u5747\u8861\u5668\u4f1a\u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230\u540e\u7aef\u5b9e\u4f8b\uff0c\u800c\u4e0d\u4fee\u6539\u8bf7\u6c42\u6807\u5934\u3002\u5982\u679c\u60a8\u542f\u7528\u4ee3\u7406\u534f\u8bae\uff0c\u5219\u4f1a\u5411\u8bf7\u6c42\u6807\u5934\u6dfb\u52a0\u4e00\u4e2a\u7528\u6237\u53ef\u8bfb\u7684\u6807\u5934\uff0c\u5176\u4e2d\u5305\u542b\u8fde\u63a5\u4fe1\u606f\uff08\u5982\u6e90 IP \u5730\u5740\u3001\u76ee\u6807 IP \u5730\u5740\u548c\u7aef\u53e3\u53f7\uff09\u3002\u8be5\u6807\u5934\u968f\u540e\u4f5c\u4e3a\u8bf7\u6c42\u7684\u4e00\u90e8\u5206\u53d1\u9001\u5230\u540e\u7aef\u5b9e\u4f8b\u3002<br \/>\n\u60a8\u53ef\u4ee5\u5728\u4f7f\u7528 SSL \u548c TCP \u534f\u8bae\u7684\u7aef\u53e3\u4e0a\u542f\u7528\u4ee3\u7406\u534f\u8bae\u3002\u5f53\u4f7f\u7528\u975e HTTP \u534f\u8bae\u65f6\uff0c\u6216\u8005\u5f53\u4f7f\u7528 HTTPS \u5e76\u4e14\u672a\u5728\u8d1f\u8f7d\u5747\u8861\u5668\u4e0a\u7ec8\u6b62 SSL \u8fde\u63a5\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7406\u534f\u8bae\u6355\u83b7\u5ba2\u6237\u7aef\u7684\u6e90 IP\u3002<\/p>\n<h2>\u4ee3\u7406\u534f\u8bae\u5934<\/h2>\n<p>\u5728\u60a8\u4f7f\u7528\u4e3a TCP\/SSL \u8fde\u63a5\u914d\u7f6e\u7684\u8d1f\u8f7d\u5747\u8861\u5668\u65f6\uff0c\u4ee3\u7406\u534f\u8bae\u6807\u5934\u6709\u52a9\u4e8e\u8bc6\u522b\u5ba2\u6237\u7aef\u7684 IP \u5730\u5740\u3002\u56e0\u4e3a\u8d1f\u8f7d\u5747\u8861\u5668\u4f1a\u62e6\u622a\u5ba2\u6237\u7aef\u4e0e\u60a8\u7684\u540e\u7aef\u5b9e\u4f8b\u4e4b\u95f4\u7684\u6d41\u91cf\uff0c\u56e0\u6b64\u60a8\u7684\u540e\u7aef\u5b9e\u4f8b\u7684\u8bbf\u95ee\u65e5\u5fd7\u4e2d\u5c06\u5305\u542b\u8d1f\u8f7d\u5747\u8861\u5668\u7684 IP \u5730\u5740\u800c\u4e0d\u662f\u539f\u59cb\u5ba2\u6237\u7aef\u7684 IP \u5730\u5740\u3002\u60a8\u53ef\u4ee5\u5206\u6790\u8be5\u8bf7\u6c42\u7684\u7b2c\u4e00\u884c\uff0c\u4ee5\u68c0\u7d22\u8be5\u5ba2\u6237\u7aef\u7684 IP \u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002<\/p>\n<p>IPv6 \u6807\u5934\u4e2d\u7684\u4ee3\u7406\u5730\u5740\u662f\u8d1f\u8f7d\u5747\u8861\u5668\u7684\u516c\u6709 IPv6 \u5730\u5740\u3002\u6b64 IPv6 \u5730\u5740\u4e0e\u4ece\u8be5\u8d1f\u8f7d\u5747\u8861\u5668\u4ee5 ipv6 \u6216 dualstack \u5f00\u5934\u7684 DNS \u540d\u79f0\u89e3\u6790\u800c\u6765\u7684 IP \u5730\u5740\u76f8\u5339\u914d\u3002\u5982\u679c\u5ba2\u6237\u7aef\u4f7f\u7528 IPv4 \u8fdb\u884c\u8fde\u63a5\uff0c\u5219\u4ee3\u7406\u6807\u5934\u4e2d\u7684\u5730\u5740\u662f\u8be5\u8d1f\u8f7d\u5747\u8861\u5668\u7684\u79c1\u6709 IPv4 \u5730\u5740\uff0c\u4e0d\u80fd\u5728 EC2-Classic \u7f51\u7edc\u5916\u90e8\u901a\u8fc7 DNS \u67e5\u627e\u8fdb\u884c\u89e3\u6790\u3002<\/p>\n<p>\u8be5\u4ee3\u7406\u534f\u8bae\u884c\u4ee5\u56de\u8f66\u7b26\u548c\u6362\u884c\u7b26 (&quot;\\r\\n&quot;) \u7ed3\u675f\uff0c\u4e14\u5177\u6709\u4ee5\u4e0b\u5f62\u5f0f\uff1a<\/p>\n<pre><code class=\"language-bash\">PROXY_STRING + single space + INET_PROTOCOL + single space + CLIENT_IP + single space + PROXY_IP + single space + CLIENT_PORT + single space + PROXY_PORT + &quot;\\r\\n&quot;<\/code><\/pre>\n<p>\u5b9e\u4f8b\uff1a<\/p>\n<pre><code class=\"language-bash\">PROXY TCP4 198.51.100.22 203.0.113.7 35646 80\\r\\n<\/code><\/pre>\n<h2>\u5b89\u88c5AWS CLI\u5de5\u5177<\/h2>\n<p>AWS \u7ba1\u7406\u63a7\u5236\u53f0\u662f\u4e0d\u652f\u6301\u542f\u7528\u4ee3\u7406\u534f\u8bae\u7684\uff0c\u56e0\u6b64\u9700\u8981\u901a\u8fc7\u547d\u4ee4\u884c\u6765\u542f\u7528<\/p>\n<pre><code class=\"language-bash\"># sudo apt-get install python-pip\n# sudo pip install awscli<\/code><\/pre>\n<p>\u914d\u7f6e\u6388\u6743\u8fde\u63a5\u53c2\u6570\u6587\u4ef6<\/p>\n<pre><code class=\"language-bash\"># sudo vi ~\/.aws\/config<\/code><\/pre>\n<pre><code class=\"language-bash\">[default]\naws_access_key_id = YOU_ACCESS_ID\naws_secret_access_key = YOU_SECRET_ID\noutput = json OR bson OR text\nregion = PREFERRED_AWS_REGION<\/code><\/pre>\n<p>\u7c7b\u4f3c\u8fd9\u6837\u7684\uff0c<code>aws_access_key_id<\/code>\u3001<code>aws_secret_access_key<\/code>\u3001<code>region<\/code>\u6839\u636e\u4f60\u7684aws\u5b9e\u4f8b\u586b\u5199<\/p>\n<h2>AWS ELB\u542f\u7528\u4ee3\u7406\u534f\u8bae<\/h2>\n<p>\u67e5\u770bELB\u652f\u6301\u7684\u7b56\u7565\u3002\u54cd\u5e94\u5305\u542b\u652f\u6301\u7684\u7b56\u7565\u7c7b\u578b\u7684\u540d\u79f0\u548c\u63cf\u8ff0\u3002<\/p>\n<pre><code class=\"language-json\"># aws elb describe-load-balancer-policy-types\n{\n  &quot;PolicyTypeDescriptions&quot;: [\n    ...\n    {\n      &quot;PolicyAttributeTypeDescriptions&quot;: [\n        {\n          &quot;Cardinality&quot;: &quot;ONE&quot;,\n          &quot;AttributeName&quot;: &quot;ProxyProtocol&quot;,\n          &quot;AttributeType&quot;: &quot;Boolean&quot;\n        }\n      ],\n      &quot;PolicyTypeName&quot;: &quot;ProxyProtocolPolicyType&quot;,\n      &quot;Description&quot;: &quot;Policy that controls whether to include the IP address and port of the originating \nrequest for TCP messages. This policy operates on TCP\/SSL listeners only&quot;\n    },\n    ...\n  ]\n}<\/code><\/pre>\n<p>\u521b\u5efa\u542f\u7528\u4ee3\u7406\u534f\u8bae\u7684\u7b56\u7565<\/p>\n<pre><code class=\"language-bash\"># aws elb create-load-balancer-policy --load-balancer-name YOU_ELB_NAME --policy-name EnableProxyProtocol --policy-type-name ProxyProtocolPolicyType --policy-attributes AttributeName=ProxyProtocol,AttributeValue=True<\/code><\/pre>\n<p>\u8be5\u547d\u4ee4\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u79f0\u4e3a<code>EnableProxyProtocol<\/code>\u7684\u7b56\u7565\uff0c\u5e76\u5206\u914d\u4e0b\u5217ELB\u5c5e\u6027&quot;AttributeName=ProxyProtocol &amp; AttributeValue=True&quot;<\/p>\n<p>\u5728\u6307\u5b9a\u7aef\u53e3\u4e0a\u542f\u7528\u4e0a\u8ff0\u7684\u7b56\u7565\uff1a<\/p>\n<pre><code class=\"language-bash\"># aws elb set-load-balancer-policies-for-backend-server --load-balancer-name YOU_ELB_NAME --instance-port 80 --policy-names EnableProxyProtocol\n# aws elb set-load-balancer-policies-for-backend-server --load-balancer-name YOU_ELB_NAME --instance-port 81 --policy-names EnableProxyProtocol\n# aws elb set-load-balancer-policies-for-backend-server --load-balancer-name YOU_ELB_NAME --instance-port 443 --policy-names EnableProxyProtocol<\/code><\/pre>\n<p>\u6b64\u547d\u4ee4\u5c06\u66ff\u4ee3\u5f53\u524d\u5df2\u542f\u7528\u7684\u7b56\u7565\u7ec4\u3002\u56e0\u6b64\uff0c<code>--policy-names<\/code>\u9009\u9879\u5fc5\u987b\u540c\u65f6\u6307\u5b9a\u60a8\u6b63\u5728\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u7684\u7b56\u7565\u548c\u4efb\u4f55\u5f53\u524d\u5df2\u542f\u7528\u7684\u7b56\u7565\u3002<\/p>\n<h2>\u67e5\u770b\u662f\u5426\u542f\u7528<\/h2>\n<pre><code class=\"language-json\"># aws elb describe-load-balancers --load-balancer-name YOU_ELB_NAME | jq &#039;.LoadBalancerDescriptions[].BackendServerDescriptions&#039;\n[\n {\n  &quot;PolicyNames&quot;: [\n   &quot;EnableProxyProtocol&quot;\n  ],\n  &quot;InstancePort&quot;: 80\n },\n {\n  &quot;PolicyNames&quot;: [\n   &quot;EnableProxyProtocol&quot;\n  ],\n  &quot;InstancePort&quot;: 81\n },\n {\n  &quot;PolicyNames&quot;: [\n   &quot;EnableProxyProtocol&quot;\n  ],\n  &quot;InstancePort&quot;: 443\n }\n]<\/code><\/pre>\n<p>\u5982\u679c\u8981\u7981\u7528\u4ee3\u7406\u534f\u8bae\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a<\/p>\n<pre><code class=\"language-bash\"># aws elb set-load-balancer-policies-for-backend-server --load-balancer-name YOU_ELB_NAME --instance-port 80 --policy-names &quot;[]&quot;<\/code><\/pre>\n<h2>\u914d\u7f6eNginx\u63a5\u53d7\u4ee3\u7406\u534f\u8bae\u5934<\/h2>\n<p>Nginx\u542f\u7528\u8fd9\u4e2a\u4e3b\u8981\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u83b7\u53d6\u5230\u771f\u5b9e\u7684\u5ba2\u6237\u7aefIP\u5730\u5740\u3002\u5426\u5219\uff0c\u90fd\u662fELB\u7684\u5185\u7f51IP\u5730\u5740\u3002<\/p>\n<pre><code class=\"language-json\">set_real_ip_from 127.0.0.1;\nset_real_ip_from 10.0.0.0\/8;\nreal_ip_header  proxy_protocol;\nreal_ip_recursive on;\n\nserver {\n  listen 80 proxy_protocol;\n  listen 443 proxy_protocol ssl;\n  ...\n  location \/ {\n    proxy_set_header Host               $host;\n    proxy_set_header X-Real-IP          $proxy_protocol_addr;\n    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;\n    proxy_set_header X-Forwarded-Proto  tcp;\n    proxy_set_header X-NginX-Proxy      true;\n  }\n  ...\n}<\/code><\/pre>\n<p>\u5f53Nginx\u542f\u7528\u4e86\u4ee3\u7406\u534f\u8bae\uff0c<code>$proxy_protocol_addr<\/code>\u53d8\u91cf\u5c06\u662f\u771f\u5b9e\u7684\u5ba2\u6237\u7aefIP<\/p>\n<p>\u5982\u679c\u6ca1\u6709\u53cd\u4ee3\uff0cNginx\u8fd9\u4e48\u914d\u7f6e\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-bash\">log_format elb_log &#039;$proxy_protocol_addr - $remote_user [$time_local] &#039; &#039;&quot;$request&quot; $status $body_bytes_sent &quot;$http_referer&quot; &#039; &#039;&quot;$http_user_agent&quot;&#039;;\n\nset_real_ip_from 172.31.0.0\/20;\nset_real_ip_from 10.0.0.0\/8;\nreal_ip_header proxy_protocol;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4f7f\u7528AWS\u4e91\u670d\u52a1\u7684\u65f6\u5019\uff0c90%\u8981\u4f7f\u7528ELB\u670d\u52a1\u4f5c\u4e3a\u8d1f\u8f7d\u5747\u8861\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f7f\u7528ELB\u8981\u6bd4\u81ea\u5df1\u642d\u5efa\u8d1f\u8f7d\u5747\u8861\u8981\u65b9\u4fbf\u5f97 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[90],"tags":[400,459],"class_list":["post-1820","post","type-post","status-publish","format-standard","hentry","category-cloud-devops","tag-aws","tag-elb"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1820"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1820\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}