{"id":224468,"date":"2021-08-21T08:59:00","date_gmt":"2021-08-21T00:59:00","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=224468"},"modified":"2021-08-18T08:59:32","modified_gmt":"2021-08-18T00:59:32","slug":"nginx-get-realip","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-get-realip.html","title":{"rendered":"nginx\u53cd\u5411\u4ee3\u7406\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u771f\u5b9eIP\u548c\u57df\u540d"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nnginx\u53cd\u5411\u4ee3\u7406\u540e\uff0c\u5728\u5e94\u7528\u4e2d\u53d6\u5f97\u7684ip\u90fd\u662f\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u7684ip\uff0c\u53d6\u5f97\u7684\u57df\u540d\u4e5f\u662f\u53cd\u5411\u4ee3\u7406\u914d\u7f6e\u7684url\u7684\u57df\u540d\uff0c\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u9700\u8981\u5728nginx\u53cd\u5411\u4ee3\u7406\u914d\u7f6e\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u914d\u7f6e\u4fe1\u606f\uff0c\u76ee\u7684\u5c06\u5ba2\u6237\u7aef\u7684\u771f\u5b9eip\u548c\u57df\u540d\u4f20\u9012\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/a>
\nnginx\u53cd\u5411\u4ee3\u7406\u540e\uff0c\u5728\u5e94\u7528\u4e2d\u53d6\u5f97\u7684ip\u90fd\u662f\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u7684ip\uff0c\u53d6\u5f97\u7684\u57df\u540d\u4e5f\u662f\u53cd\u5411\u4ee3\u7406\u914d\u7f6e\u7684url\u7684\u57df\u540d\uff0c\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u9700\u8981\u5728nginx\u53cd\u5411\u4ee3\u7406\u914d\u7f6e\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u914d\u7f6e\u4fe1\u606f\uff0c\u76ee\u7684\u5c06\u5ba2\u6237\u7aef\u7684\u771f\u5b9eip\u548c\u57df\u540d\u4f20\u9012\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002<\/p>\n

nginx\u53cd\u5411\u4ee3\u7406\u914d\u7f6e\u65f6\uff0c\u4e00\u822c\u4f1a\u6dfb\u52a0\u4e0b\u9762\u7684\u914d\u7f6e\uff1a<\/strong><\/span><\/div>\n
proxy_set_header Host $host;\r\nproxy_set_header X-Real-IP $remote_addr;\r\nproxy_set_header REMOTE-HOST $remote_addr;\r\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\n<\/pre>\n

\u5176\u4e2d\u7b2c\u4e00\u884c\u5173\u4e8ehost\u7684\u914d\u7f6e\uff0c\u662f\u5173\u4e8e\u57df\u540d\u4f20\u9012\u7684\u914d\u7f6e\uff0c\u4f59\u4e0b\u8ddfIP\u76f8\u5173\u3002<\/p>\n

\u8bdd\u4e0d\u591a\u8bf4\u76f4\u63a5\u8d34\u51faNginx\u5b9e\u4f8b\u4ee3\u7801\uff1a<\/p>\n

upstream\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u65b9\u6cd5\u6765\u5b9e\u73b0\u5728\u8f6e\u8be2\u548c\u5ba2\u6237\u7aefIP\u4e4b\u95f4\u7684\u540e\u7aef\u670d\u52a1\u5668\u8d1f\u8377\u5e73\u8861\u3002<\/p>\n

upstream abc.com {\r\nserver 127.0.0.1:8080;\r\nserver 127.0.0.1:80;\r\nserver 127.0.0.1:8000;\r\n}\r\n\r\nserver {\r\nlisten 80;\r\nserver_name www.test.com;\r\nlocation \/ {\r\nproxy_pass http:\/\/abc.com;\r\nproxy_set_header Host $host;#\u4fdd\u7559\u4ee3\u7406\u4e4b\u524d\u7684host\r\nproxy_set_header X-Real-IP $remote_addr;#\u4fdd\u7559\u4ee3\u7406\u4e4b\u524d\u7684\u771f\u5b9e\u5ba2\u6237\u7aefip\r\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\nproxy_set_header HTTP_X_FORWARDED_FOR $remote_addr;#\u5728\u591a\u7ea7\u4ee3\u7406\u7684\u60c5\u51b5\u4e0b\uff0c\u8bb0\u5f55\u6bcf\u6b21\u4ee3\u7406\u4e4b\u524d\u7684\u5ba2\u6237\u7aef\u771f\u5b9eip\r\nproxy_set_header X-Forwarded-Proto $scheme; #\u8868\u793a\u5ba2\u6237\u7aef\u771f\u5b9e\u7684\u534f\u8bae\uff08http\u8fd8\u662fhttps\uff09\r\nproxy_redirect default;#\u6307\u5b9a\u4fee\u6539\u88ab\u4ee3\u7406\u670d\u52a1\u5668\u8fd4\u56de\u7684\u54cd\u5e94\u5934\u4e2d\u7684location\u5934\u57df\u8ddfrefresh\u5934\u57df\u6570\u503c\r\n}\r\n<\/pre>\n
php\u4e2d\u53d6\u5f97\u5ba2\u6237\u7aef\u771f\u5b9eIP\uff1a<\/strong><\/span><\/div>\n
\/**\r\n* \u83b7\u53d6\u5ba2\u6237\u7aefip\r\n*\/\r\nfunction getClientIP() {\r\n$ip = \"unknown\";\r\n\/*\r\n* \u8bbf\u95ee\u65f6\u7528localhost\u8bbf\u95ee\u7684\uff0c\u8bfb\u51fa\u6765\u7684\u662f\u201c::1\u201d\u662f\u6b63\u5e38\u60c5\u51b5\u3002\r\n* \uff1a\uff1a1\u8bf4\u660e\u5f00\u542f\u4e86ipv6\u652f\u6301,\u8fd9\u662fipv6\u4e0b\u7684\u672c\u5730\u56de\u73af\u5730\u5740\u7684\u8868\u793a\u3002\r\n* \u4f7f\u7528ip\u5730\u5740\u8bbf\u95ee\u6216\u8005\u5173\u95edipv6\u652f\u6301\u90fd\u53ef\u4ee5\u4e0d\u663e\u793a\u8fd9\u4e2a\u3002\r\n* *\/\r\nif (isset($_SERVER)) {\r\nif (isset($_SERVER[\"HTTP_X_FORWARDED_FOR\"])) {\r\n$ip = $_SERVER[\"HTTP_X_FORWARDED_FOR\"];\r\n} elseif (isset($_SERVER[\"HTTP_CLIENT_ip\"])) {\r\n$ip = $_SERVER[\"HTTP_CLIENT_ip\"];\r\n} else {\r\n$ip = $_SERVER[\"REMOTE_ADDR\"];\r\n}\r\n} else {\r\nif (getenv('HTTP_X_FORWARDED_FOR')) {\r\n$ip = getenv('HTTP_X_FORWARDED_FOR');\r\n} elseif (getenv('HTTP_CLIENT_ip')) {\r\n$ip = getenv('HTTP_CLIENT_ip');\r\n} else {\r\n$ip = getenv('REMOTE_ADDR');\r\n}\r\n}\r\nif(trim($ip)==\"::1\"){\r\n$ip=\"127.0.0.1\";\r\n}\r\nreturn $ip;\r\n}\r\n<\/pre>\n
java\u53d6\u5f97\u5ba2\u6237\u7aef\u771f\u5b9eIP\uff1a<\/strong><\/span><\/div>\n
public String getClientIP(HttpServletRequest request) {\r\nString ip = request.getHeader(\"x-forwarded-for\");\r\nif(ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\r\nip = request.getHeader(\"Proxy-Client-IP\");\r\n}\r\nif(ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\r\nip = request.getHeader(\"WL-Proxy-Client-IP\");\r\n\r\n}\r\nif(ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {\r\nip = request.getRemoteAddr();\r\n}\r\nreturn ip;\r\n}\r\n\r\n<\/pre>\n
php\u53d6\u5f97\u57df\u540d\uff1a<\/strong><\/span><\/div>\n

Php\u4ee3\u7801<\/p>\n

$_SERVER['SERVER_NAME'];\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

nginx\u53cd\u5411\u4ee3\u7406\u540e\uff0c\u5728\u5e94\u7528\u4e2d\u53d6\u5f97\u7684ip\u90fd\u662f\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u7684ip\uff0c\u53d6\u5f97\u7684\u57df\u540d\u4e5f\u662f\u53cd\u5411\u4ee3\u7406\u914d\u7f6e\u7684url\u7684\u57df\u540d\uff0c\u89e3 […]<\/p>\n","protected":false},"author":643,"featured_media":224474,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-224468","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\/224468","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\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=224468"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224468\/revisions"}],"predecessor-version":[{"id":224475,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224468\/revisions\/224475"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/224474"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=224468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=224468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=224468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}