{"id":1485,"date":"2024-09-04T18:14:54","date_gmt":"2024-09-04T10:14:54","guid":{"rendered":"https:\/\/www.mailabc.cn\/blog\/?p=1485"},"modified":"2024-09-05T11:04:29","modified_gmt":"2024-09-05T03:04:29","slug":"http%e5%8d%8f%e8%ae%ae%e9%80%8f%e4%bc%a0%e6%ba%90ip%ef%bc%9ax-forwarded-for","status":"publish","type":"post","link":"https:\/\/www.mailabc.cn\/blog\/2024\/09\/04\/http%e5%8d%8f%e8%ae%ae%e9%80%8f%e4%bc%a0%e6%ba%90ip%ef%bc%9ax-forwarded-for\/","title":{"rendered":"HTTP\u534f\u8bae\u900f\u4f20\u6e90IP\uff1aX-Forwarded-For"},"content":{"rendered":"<h2>\u4ec0\u4e48\u662fX-Forwarded-For<\/h2>\n<p>X-Forwarded-For \uff08\u7b80\u79f0XFF\uff09\u662f\u4e00\u4e2a HTTP \u6269\u5c55\u5934\u90e8\uff0c\u7528\u4e8e\u8868\u793a HTTP \u8bf7\u6c42\u7684\u771f\u5b9e IP \u5730\u5740\u3002HTTP\/1.1\uff08RFC 2616\uff09\u534f\u8bae\u4e2d\u5e76\u6ca1\u6709\u5bf9\u5b83\u8fdb\u884c\u5b9a\u4e49\uff0c\u8be5\u529f\u80fd\u6700\u521d\u7531\u7f13\u5b58\u4ee3\u7406\u8f6f\u4ef6 Squid \u5f15\u5165\u3002\u5982\u4eca\uff0c\u5b83\u5df2\u6210\u4e3a\u4e8b\u5b9e\u4e0a\u7684\u6807\u51c6\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd HTTP \u4ee3\u7406\u548c\u8d1f\u8f7d\u5747\u8861\u670d\u52a1\u4e2d\uff0c\u5e76\u88ab\u7eb3\u5165\u4e86 RFC 7239\uff08Forwarded HTTP Extension\uff09\u6807\u51c6\u3002<\/p>\n<p>X-Forwarded-For \u8bf7\u6c42\u5934\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">X-Forwarded-For: client, proxy1, proxy2<\/pre>\n<p>X-Forwarded-For\u8bf7\u6c42\u5934\u662f\u7531\u300c\u82f1\u6587\u9017\u53f7 + \u7a7a\u683c\u300d\u9694\u5f00\u7684\u591a\u4e2a\u90e8\u5206\u7ec4\u6210\uff0c\u6700\u5f00\u59cb\u7684\u662f\u79bb\u670d\u52a1\u7aef\u6700\u8fdc\u7684\u8bbe\u5907 IP\uff0c\u7136\u540e\u662f\u6bcf\u4e00\u7ea7\u4ee3\u7406\u8bbe\u5907\u7684 IP\u3002<\/p>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6bcf\u7ecf\u8fc7\u4e00\u5c42\u4ee3\u7406\u670d\u52a1\u5668\uff0cX-Forwarded-For\u4e2d\u7684IP\u4f1a\u505a\u4e00\u6b21\u8ffd\u52a0\u3002<\/p>\n<p><a href=\"https:\/\/www.mailabc.cn\/blog\/wp-content\/uploads\/2024\/09\/2024090407451934.jpg\" rel=\"box\" class=\"fancybox\"><img class=\"aligncenter wp-image-1489\" data-original=\"https:\/\/www.mailabc.cn\/blog\/wp-content\/uploads\/2024\/09\/2024090407451934-1024x297.jpg\"  alt=\"x-forwarded-for\" width=\"640\" height=\"186\" \/><\/a><\/p>\n<noscript><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1489\" src=\"https:\/\/www.mailabc.cn\/blog\/wp-content\/uploads\/2024\/09\/2024090407451934-1024x297.jpg\" alt=\"x-forwarded-for\" width=\"640\" height=\"186\" \/><\/a><\/p><\/noscript>\n<h2>\u4ee3\u7406\u670d\u52a1\u5668\u5bf9X-Forwarded-For\u7684\u5904\u7406<\/h2>\n<p><span style=\"color: #ff6600;\">\u6ce8\uff1a\u4e3a\u4e86\u65b9\u4fbf\u6d4b\u8bd5X-Forwarded-For\u7684\u8fd0\u4f5c\u539f\u7406\uff0c\u8fd9\u91cc\u4ee5\u6700\u5e38\u7528\u7684\u53cd\u5411\u4ee3\u7406\u8f6f\u4ef6Nginx\u4e3a\u4f8b\u3002<\/span><\/p>\n<p>\u5728Nginx\u914d\u7f6e\u4e2d\uff0c\u6709\u51e0\u4e2a\u5173\u952e\u53d8\u91cf\u7684\u542b\u4e49\u9700\u8981\u4e86\u89e3\u4e00\u4e0b\uff1a<\/p>\n<ul>\n<li><strong>$http_x_forwarded_for\uff1a<\/strong>\u8868\u793aX-Forwarded-For\u503c\u3002<\/li>\n<li><strong> $remote_addr\uff1a<\/strong>\u8868\u793a\u8bbf\u95ee\u6765\u6e90\u7684IP\u5730\u5740\u3002<\/li>\n<li><strong>proxy_add_x_forwarded_for\uff1a<\/strong>\u8868\u793a\u201c$http_x_forwarded_for\u201d+ \u201c,\u201d+\u201c$remote_addr\u201d\uff0c\u5982\u679c$http_x_forwarded_for\u4e3a\u7a7a\uff0c\u5219\u8fd9\u4e2a\u503c\u4e0e$remote_addr\u76f8\u540c\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u672c\u673aNginx\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\uff0c\u5904\u7406X-Forwarded-For\u7684\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">location \/ {\r\n      proxy_pass http:\/\/backend;\r\n      proxy_set_header        X-FORWARDED-FOR $proxy_add_x_forwarded_for;\r\n      proxy_set_header        X-FORWARDED-PROTO $scheme;\r\n}<\/pre>\n<p>\u4e0a\u8ff0\u914d\u7f6e\u4e2d\u5c06$proxy_add_x_forwarded_for \u5185\u5bb9\u8bbe\u7f6e\u5230X-FORWARDED-FOR\u5c5e\u6027\u4e2d\uff0c\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u8282\u70b9\uff08\u4ee3\u7406\u670d\u52a1\u5668\u6216\u8005web\u670d\u52a1\u5668\uff09\u3002<\/p>\n<h2>WEB\u670d\u52a1\u5668\u5bf9X-FORWARDED-FOR\u5904\u7406<\/h2>\n<p><span style=\"color: #ff6600;\">\u6ce8\uff1aWEB\u670d\u52a1\u5668\u4ee5tomcat\u4e3a\u4f8b\u8bf4\u660e\u3002<\/span><\/p>\n<p>\u5728Tomcat\u4e2d\uff0c\u4e00\u822c\u901a\u8fc7\u4e0b\u9762\u7684\u914d\u7f6e\u6765\u83b7\u53d6X-FORWARDED-FOR\u4e2d\u7684IP\u4fe1\u606f\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;Valve className=\"org.apache.catalina.valves.RemoteIpValve\"\r\n         remoteIpHeader=\"X-FORWARDED-FOR\"\r\n         protocolHeader=\"X-FORWARDED-PROTO\"\r\n         internalProxies=\"127\\.0\\.0\\.1|2\\.2\\.2\\.2|3\\.3\\.3\\.3\"\/&gt;<\/pre>\n<p>\u53c2\u6570\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><strong>className<\/strong>: \u6307\u5b9a\u4e86\u4f7f\u7528\u7684\u7c7b\u540d\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>org.apache.catalina.valves.RemoteIpValve<\/code> \u7c7b\u7528\u4e8e\u5904\u7406\u5ba2\u6237\u7aef IP \u5730\u5740\u7684\u91cd\u5199\u3002<\/li>\n<li><strong>remoteIpHeader<\/strong>: \u6307\u5b9a\u4e86\u5305\u542b\u539f\u59cb\u5ba2\u6237\u7aef IP \u5730\u5740\u7684 HTTP \u5934\u540d\u79f0\u3002\u901a\u5e38\u53cd\u5411\u4ee3\u7406\u4f1a\u5c06\u539f\u59cb\u5ba2\u6237\u7aef\u7684 IP \u5730\u5740\u653e\u5165 <code>X-FORWARDED-FOR<\/code> \u5934\u4e2d\uff0c\u56e0\u6b64\u8fd9\u91cc\u8bbe\u7f6e\u4e3a <code>X-FORWARDED-FOR<\/code>\u3002<\/li>\n<li><strong>protocolHeader<\/strong>: \u6307\u5b9a\u4e86\u5305\u542b\u539f\u59cb\u534f\u8bae\uff08HTTP \u6216 HTTPS\uff09\u7684 HTTP \u5934\u540d\u79f0\u3002<code>X-FORWARDED-PROTO<\/code> \u901a\u5e38\u88ab\u7528\u4e8e\u6307\u793a\u539f\u59cb\u8bf7\u6c42\u662f\u901a\u8fc7 HTTP \u8fd8\u662f HTTPS \u8fdb\u884c\u7684\u3002<\/li>\n<li><strong>internalProxies<\/strong>: \u7528\u4e8e\u6307\u5b9a\u4e00\u7cfb\u5217\u5185\u90e8\u4ee3\u7406\u670d\u52a1\u5668\u7684 IP \u5730\u5740\uff08\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4e49\uff09\u3002\u8fd9\u4e9b\u4ee3\u7406\u670d\u52a1\u5668\u4f1a\u88ab\u8ba4\u4e3a\u662f\u53ef\u4fe1\u7684\uff0c\u5e76\u4e14\u4ed6\u4eec\u8f6c\u53d1\u7684 <code>X-FORWARDED-FOR<\/code> \u5934\u4f1a\u88ab Tomcat \u8ba4\u4e3a\u662f\u53ef\u4fe1\u7684\u5ba2\u6237\u7aef IP \u5730\u5740\u3002\u5728\u8fd9\u4e2a\u914d\u7f6e\u4e2d\uff0c<code>internalProxies<\/code> \u5217\u51fa\u4e86\u4e09\u4e2a IP \u5730\u5740\uff1a\n<ul>\n<li><code>127.0.0.1<\/code>: \u672c\u5730\u56de\u73af\u5730\u5740<\/li>\n<li><code>2.2.2.2<\/code>, <code>3.3.3.3<\/code>: \u5047\u8bbe\u8fd9\u4e9b\u662f\u5176\u4ed6\u53ef\u4fe1\u4efb\u7684\u5185\u90e8\u4ee3\u7406\u670d\u52a1\u5668\u7684 IP \u5730\u5740\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u57fa\u4e8e\u4e0a\u8ff0\u914d\u7f6e\uff0cTomcat\u4f1a\u9488\u5bf9X-FORWARDED-FOR\u4f20\u9012\u8fc7\u6765\u7684\u5185\u5bb9<strong>\u4ece\u53f3\u5411\u5de6<\/strong>\u8fdb\u884c\u9010\u4e2a\u68c0\u67e5\uff0c<strong>\u5982\u679c\u5730\u5740\u5728internalProxies\u4e2d\u5c06\u53bb\u9664\u8be5IP\uff1b\u5982\u679c\u5730\u5740\u4e0d\u5728internalProxies\u4e2d\u5219\u53d6\u8fd9\u4e2aIP\u4f5c\u4e3aremoteip<\/strong>\u3002\u6b63\u5e38\u6765\u8bf4\u6240\u6709\u4ee3\u7406\u670d\u52a1\u5668\u7684IP\u5e94\u8be5\u5305\u542b\u5728internalProxies\uff0c\u5426\u5219\u6700\u7ec8\u53ef\u80fd\u53d6\u5230\u4ee3\u7406\u670d\u52a1\u5668\u7684IP\u3002<\/p>\n<p>\u4ee5\u6587\u4e2d\u793a\u4f8b\u56fe\u6240\u793a\uff0c\u4ece1.1.1.1\u7ecf\u8fc72.2.2.2\u30013.3.3.3\u4e24\u5c42\u4ee3\u7406\uff0cWEB\u670d\u52a1\u5668\uff084.4.4.4\uff09\u6700\u7ec8\u63a5\u6536\u5230\u7684X-FORWARDED-FOR\u503c\u4e3a\u201c1.1.1.1, 2.2.2.2, 3.3.3.3\u201d\uff0c\u5982\u679cinternalProxies=&#8221;127\\.0\\.0\\.1|2\\.2\\.2\\.2&#8243;\uff0c\u5219Tomcat\u53d6\u5230\u7684remoteip\u662f3.3.3.3\uff0c\u800c\u4e0d\u4f1a\u53d6\u52301.1.1.1 \u3002<\/p>\n<h2>\u5982\u4f55\u6d4b\u8bd5\uff1f<\/h2>\n<p>\u53ef\u4ee5\u901a\u8fc7curl\u505aX-FORWARDED-FOR\u6a21\u62df\u6d4b\u8bd5\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">curl -H \"X-Forwarded-For: 203.0.113.195, 192.168.17.19\" https:\/\/www.mailabc.cn<\/pre>\n<p>\u5728Nginx\u914d\u7f6e\u4e2d\uff0c\u53ef\u4ee5\u9488\u5bf9\u8f93\u51fa\u7684\u65e5\u5fd7\u5c5e\u6027\u8fdb\u884c\u5b9a\u4e49\uff0c\u52a0\u5165$http_x_forwarded_for\u53d8\u91cf\u5185\u5bb9\u8f93\u51fa\uff0c\u5982\u4e0b\u793a\u4f8b\u6240\u793a\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">log_format  main  '$remote_addr - $remote_user [$time_local] \"$request\" '\r\n            '$status $body_bytes_sent \"$http_referer\" '\r\n            '\"$http_user_agent\" \"$http_x_forwarded_for\" '\r\n            '$upstream_response_time $request_time ';<\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u901a\u8fc7\u65e5\u5fd7\u67e5\u770b\u4e0a\u6e38\u4ee3\u7406\u8282\u70b9\u4f20\u9012\u7684X-FORWARDED-FOR\u503c\u5177\u4f53\u662f\u4ec0\u4e48\u3002\u5f53\u7136\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u6293\u5305\u5206\u6790\u3002<\/p>\n<h2>\u6ce8\u610f\u4e8b\u9879<\/h2>\n<p>X-FORWARDED-FOR \u53ef\u4ee5\u5728\u6570\u636e\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u88ab\u4fee\u6539\uff0c\u5c5e\u4e8e\u4e0d\u53ef\u4fe1\u6570\u636e\u3002\u914d\u7f6e\u8fc7\u7a0b\u4e2d\u9700\u8981\u6ce8\u610f\u505a\u597d\u4ee3\u7406\u8282\u70b9IP\u4fe1\u4efb\u63a7\u5236\uff0c\u9632\u6b62\u6076\u610f\u653b\u51fb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fX-Forwarded-For X-Forwarded-For \uff08\u7b80\u79f0XFF\uff09\u662f\u4e00\u4e2a HTTP \u6269\u5c55\u5934\u90e8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1400,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[134],"class_list":["post-1485","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-20","tag-ip"],"_links":{"self":[{"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/posts\/1485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/comments?post=1485"}],"version-history":[{"count":9,"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/posts\/1485\/revisions"}],"predecessor-version":[{"id":1496,"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/posts\/1485\/revisions\/1496"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/media\/1400"}],"wp:attachment":[{"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/media?parent=1485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/categories?post=1485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mailabc.cn\/blog\/wp-json\/wp\/v2\/tags?post=1485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}