{"id":155,"date":"2019-05-09T21:37:21","date_gmt":"2019-05-09T13:37:21","guid":{"rendered":"https:\/\/cloudbool.com\/archive\/?p=155"},"modified":"2019-05-09T21:37:21","modified_gmt":"2019-05-09T13:37:21","slug":"nginx-proxy-real-ip","status":"publish","type":"post","link":"https:\/\/cloudbool.com\/archive\/nginx-proxy-real-ip.html","title":{"rendered":"Nginx\u53cd\u5411\u4ee3\u7406\u540e\u83b7\u5f97\u771f\u5b9e\u7528\u6237IP"},"content":{"rendered":"<p>\u56e0\u5386\u53f2\u539f\u56e0\uff0c\u624b\u4e0a\u6709\u51e0\u4e2a\u7ad9\u4e00\u76f4\u5728\u56fd\u5916\u98d8\u7740\uff0c\u4f17\u6240\u5468\u77e5\uff0c\u7f51\u7edc\u98a0\u7c38\uff0c\u66f4\u522b\u8bf4\u8fd8\u8de8\u4e86\u4e2a\u592a\u5e73\u6d0b\u4e86\uff0c\u5927\u591a\u6570\u56de\u56fd\u7ebf\u8def\u665a\u9ad8\u5cf0\u7206\u70b8\u662f\u7ecf\u5e38\u7684\u4e8b\uff0c\u8d81\u7740\u4fc3\u9500\u6b63\u597d\u5165\u4e86\u53f0\u642c\u74e6\u5de5\u7684CN2 GIA\u7ebf\u8defVPS\uff0c\u4f46\u662f\u95ee\u9898\u662fCN2 GIA\u56de\u56fd\u7ebf\u8def\u662f\u5f88\u597d\uff0c\u4f46\u662f\u914d\u7f6e\u592a\u4f4e\uff0c\u914d\u7f6e\u597dNginx+PHP-FPM+MySQL\u4e4b\u540e\u518d\u8dd1WordPress\u7a0d\u5fae\u6709\u70b9\u6d41\u91cf\u5c31\u5f88\u5403\u529b\uff0c\u4e8e\u662f\u8fd9\u4e48\u4e45\u4f9d\u8d56\u6211\u90a3\u51e0\u4e2a\u7ad9\u90fd\u662f\u653e\u5728\u4e00\u53f0\u914d\u7f6e\u66f4\u9ad8\u7684\u540e\u7aef\u670d\u52a1\u5668\u4e0a\uff0c\u8fd9\u53f0\u7ebf\u8def\u597d\u7684CN2 GIA VPS\u53ea\u662f\u7528\u6765\u627f\u62c5\u56fd\u5185\u8fc7\u53bb\u7684\u524d\u7aef\u6d41\u91cf\uff0c\u8fd9\u5c31\u5bfc\u81f4\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u540e\u7aef\u670d\u52a1\u5668\u62ff\u4e0d\u5230\u771f\u5b9e\u7684\u7528\u6237IP\uff0c\u8fd9\u4e48\u4e45\u4e00\u6765\u540e\u7aef\u670d\u52a1\u5668\u62ff\u5230\u7684\u90fd\u662f\u524d\u7aef\u670d\u52a1\u5668\u4e5f\u5c31\u662f\u642c\u74e6\u5de5\u7684CN2 GIA\u7ebf\u8def\u7684VPS\u7684IP\uff0c\u800c\u4e0d\u662f\u771f\u5b9e\u7684\u7528\u6237IP\uff0c\u4eca\u5929\u82b1\u4e86\u70b9\u65f6\u95f4\u7ec8\u4e8e\u89e3\u51b3\u4e86IP\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<br \/>\n<!--more--><\/p>\n<h2>\u51c6\u5907\u5de5\u4f5c<\/h2>\n<p>\u9996\u5148\uff0c\u914d\u7f6eLNMP\u4ee5\u53ca\u524d\u7aef\u670d\u52a1\u5668\u53cd\u5411\u4ee3\u7406\u524d\u7aef\u7528\u6237\u8bf7\u6c42\u5230\u540e\u7aef\u670d\u52a1\u5668\u662f\u4e0d\u7528\u8bf4\u4e86\uff0c\u8fd9\u91cc\u76f4\u63a5\u8d34\u4e00\u4e2a\u524d\u7aef\u670d\u52a1\u5668\u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>server\n{\n        listen          80;\n        server_name cloudbool.com www.cloudbool.com;\n        location \/ {\n                proxy_pass http:\/\/cloudbool.com\/;\n        }\n                access_log \/var\/log\/nginx\/cloudbool.log;\n        error_log \/var\/log\/nginx\/cloudbool-error.log;\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e48\u51e0\u884c\u5c31\u80fd\u914d\u7f6e\u4e00\u4e2aNginx\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u4e86\uff0c\u7b80\u5355\u662f\u5f88\u7b80\u5355\uff0c\u4f46\u662f\u8fd9\u4e48\u505a\u7528\u6237\u7684\u8bbf\u95eeIP\u53ea\u80fd\u5728\u524d\u7aef\u4e5f\u5c31\u662f\u914d\u7f6e\u53cd\u5411\u4ee3\u7406\u7684\u8fd9\u53f0Nginx\u6240\u5728\u670d\u52a1\u5668\u624d\u80fd\u83b7\u53d6\u5230\uff0c\u540e\u7aef\u670d\u52a1\u5668\u5f97\u5230\u7684\u53ea\u662f\u8fd9\u53f0\u524d\u7aef\u670d\u52a1\u5668\u7684IP\uff0c\u6bd4\u5982\u8bf4\u8fd9\u53f0\u524d\u7aef\u670d\u52a1\u5668\u7684IP\u662f<strong>1.2.3.4<\/strong>\uff0c\u54ea\u540e\u7aef\u670d\u52a1\u5668\u7684\u7528\u6237\u8bbf\u95ee\u65e5\u5fd7\u5168\u90e8\u90fd\u662f<strong>1.2.3.4<\/strong>\uff0c\u5bf9\u4e8e\u6211\u4eec\u5206\u6790\u8bbf\u95ee\u65e5\u5fd7\u53ca\u914d\u7f6e\u7f51\u7ad9\u5c31\u6709\u70b9\u4e0d\u4fbf\uff0c\u6211\u4eec\u9700\u8981\u7684\u662f\u5728\u540e\u7aef\u4e5f\u80fd\u62ff\u5230\u7528\u6237\u7684\u771f\u5b9eIP\u3002<\/p>\n<h2>\u524d\u7aefNginx\u670d\u52a1\u5668\u914d\u7f6e<\/h2>\n<p>\u65e2\u7136\u8981\u5728\u540e\u7aef\u670d\u52a1\u5668\u62ff\u5230\u7528\u6237\u7684\u771f\u5b9eIP\uff0c\u90a3\u5c31\u9700\u8981\u5728\u524d\u7aefNginx\u670d\u52a1\u5668\u8bf7\u6c42\u540e\u7aefNginx\u7684\u65f6\u5019\u4e00\u5e76\u5c06\u7528\u6237\u771f\u5b9eIP\u5e26\u4e0a\uff0c\u8fd9\u5c31\u9700\u8981\u7528\u5230Nginx\u7684<strong>ngx_http_realip_module<\/strong>\u6a21\u5757\uff0c\u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u7528\u7684\u662fNginx\u5b98\u65b9\u6e90\u5b89\u88c5\u7684Nginx\uff0c\u9ed8\u8ba4\u5c31\u662f\u7f16\u8bd1\u4e86\u8fd9\u4e2a\u6a21\u5757\uff0c\u5982\u679c\u6ca1\u6709\u7f16\u8bd1\u8fd9\u4e2a\u6a21\u5757\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u7f16\u8bd1\uff0c\u5df2\u7ecf\u914d\u7f6e\u597d\u7684Nginx\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u547d\u4ee4\u67e5\u770bNginx\u7684\u7f16\u8bd1\u60c5\u51b5\uff1a<strong>nginx -t<\/strong>\u3002<strong>ngx_http_realip_module<\/strong>\u6a21\u5757\u76f8\u5173\u7684\u6587\u6863\u5730\u5740\uff1ahttps:\/\/nginx.org\/en\/docs\/http\/ngx_http_realip_module.html<br \/>\n\u51c6\u5907\u597d<strong>ngx_http_realip_module<\/strong>\u6a21\u5757\u4e4b\u540e\uff0c\u63a5\u7740\u5c31\u662f\u914d\u7f6e\u524d\u7aefNginx\u670d\u52a1\u5668\u4e86\u3002<br \/>\n\u5728\u524d\u7aefNginx\u670d\u52a1\u5668\u7684\u9700\u8981\u4ee3\u7406\u7684\u7f51\u7ad9\u914d\u7f6e\u6587\u4ef6\u4e2d\u52a0\u5165\u6216\u8005\u66f4\u6539\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>location \/ {\n     proxy_set_header Accept-Encoding \"\";\n     proxy_pass https:\/\/cloudbool.com\/archive\/;\n     proxy_set_header   Host    $host;\n     proxy_redirect off;\n     proxy_set_header X-Real-IP $remote_addr;\n     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n}\n<\/code><\/pre>\n<p>\u4e5f\u5c31\u662f\u52a0\u4e0a4\u30015\u30016\u30017\u884c\uff0c\u5c06\u76f8\u5e94\u7684\u771f\u5b9eIP\u4f20\u9012\u5230\u540e\u7aef\u670d\u52a1\u5668\u3002\u66f4\u6539\u5b8c\u4e4b\u540e\uff0c\u91cd\u542f\u524d\u7aefNginx\u5373\u53ef\u3002<\/p>\n<h2>\u540e\u7aefNginx\u670d\u52a1\u5668\u914d\u7f6e<\/h2>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cNginx\u7684\u65e5\u5fd7\u683c\u5f0f\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code>log_format  main  '$remote_addr - $remote_user [$time_local] \"$request\" '\n                                    '$status $body_bytes_sent \"$http_referer\" '\n                                    '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n<\/code><\/pre>\n<p>\u5982\u679c\u662f\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5Nginx\uff0c\u9ed8\u8ba4\u7684Nginx\u4e3b\u914d\u7f6e\u6587\u4ef6\u662f\u5728<strong>\/etc\/nginx\/nginx.conf<\/strong>\u3002<br \/>\n\u6211\u4eec\u5982\u679c\u8981\u63a5\u6536\u524d\u7aefNginx\u4f20\u6765\u7684\u7528\u6237\u771f\u5b9eIP\u4fe1\u606f\u5c31\u9700\u8981\u66f4\u6539\u4e0b\uff0c\u5c06\u65e5\u5fd7\u683c\u5f0f\u5f00\u5934\u7684<strong>$remote_addr<\/strong>\u66f4\u6539\u6210<strong>$http_x_forwarded_for<\/strong>\uff0c\u5e76\u5728\u540e\u9762\u52a0\u4e0a\u4e0b\u9762\u51e0\u884c\uff0c\u76f8\u5173\u90e8\u5206\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre><code>log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] \"$request\" '\n                                    '$status $body_bytes_sent \"$http_referer\" '\n                                    '\"$http_user_agent\" \"$http_x_forwarded_for\"';\nset_real_ip_from 1.2.3.4;\nreal_ip_header X-Forwarded-For;\nreal_ip_recursive on;\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684<strong>1.2.3.4<\/strong>\u9700\u8981\u66ff\u6362\u6210\u771f\u5b9e\u7684\u524d\u7aefNginx\u670d\u52a1\u5668\u5730\u5740\u3002\u9664\u4e86\u5355\u72ec\u7684IPv4\u5730\u5740\u5916\uff0cNginx\u8fd8\u652f\u6301CIDR\u683c\u5f0f\u53caIPv6\u7684\u5730\u5740\u683c\u5f0f\u3002<br \/>\n\u914d\u7f6e\u5b8c\u4e4b\u540e\uff0c\u9700\u8981<strong>\u91cd\u542f<\/strong>Nginx\uff0c\u91cd\u542f\u4e4b\u540e\u5e94\u8be5\u5c31\u80fd\u83b7\u53d6\u5230\u771f\u5b9e\u7684\u7528\u6237IP\u4fe1\u606f\u4e86\uff0c\u5982\u56fe\uff1a<br \/>\n<img decoding=\"async\" src=\"https:\/\/x.cloudbool.com\/img\/2019\/nginx-real-ip.png\" alt=\"nginx-real-ip\" \/><br \/>\n\u5b9e\u9645\u4e2d\u4f7f\u7528\u53d1\u73b0\uff0c<strong>ngx_http_realip_module<\/strong>\u6a21\u5757\u53ef\u80fd\u548cNginx\u81ea\u5e26\u7684<strong>allow\/deny<\/strong>\u6709\u51b2\u7a81\uff0c\u5982\u679c\u540e\u7aef\u670d\u52a1\u5668\u9ed8\u8ba4\u5f00\u542f\u4e86<strong>allow\/deny<\/strong>\u6307\u4ee4\u518d\u4f7f\u7528<strong>ngx_http_realip_module<\/strong>\u6a21\u5757\uff0c\u4f1a\u5bfc\u81f4\u524d\u7aef\u8bbf\u95ee\u8fd4\u56de403\u7684\u95ee\u9898\uff0c\u6682\u65f6\u6ca1\u6709\u627e\u5230\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u641c\u7d22\u4e86\u4e00\u6ce2\u53d1\u73b0\u6709\u4eba\u9047\u5230\u4e86\u8fd9\u4e2a\u95ee\u9898\u4f46\u662f\u51e0\u5e74\u4e86\u4e5f\u6ca1\u6709\u4eba\u89e3\u51b3\u3002<br \/>\n\u9644\u5e26\u4e00\u4e2a\u524d\u7aefNginx\u7684\u5b8c\u6574\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>server{\n        listen 443 ssl http2;\n        listen [::]:443 ssl http2;\n        server_name  cloudbool.com www.cloudbool.com;\n        ssl on;\n        ssl_certificate \/path\/to\/ssl\/cloudbool-rsa.crt;\n        ssl_certificate_key \/path\/to\/ssl\/cloudbool-rsa.key;\n        ssl_session_cache        shared:SSL:10m;\n        ssl_session_timeout      10m;\n        ssl_session_tickets      on;\n        resolver                 8.8.4.4 8.8.8.8  valid=300s;\n        resolver_timeout         10s;\n        ssl_session_cache builtin:1000 shared:SSL:10m;\n        ssl_dhparam \/path\/to\/ssl\/dhparam.pem;\n        ssl_prefer_server_ciphers on;\n        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;\n        ssl_ciphers [TLS13+AESGCM+AES128|TLS13+AESGCM+AES256|TLS13+CHACHA20]:[EECDH+ECDSA+AESGCM+AES128|EECDH+ECDSA+CHACHA20]:EECDH+ECDSA+AESGCM+AES256:EECDH+ECDSA+AES128+SHA:EECDH+ECDSA+AES256+SHA:[EECDH+aRSA+AESGCM+AES128|EECDH+aRSA+CHACHA20]:EECDH+aRSA+AESGCM+AES256:EECDH+aRSA+AES128+SHA:EECDH+aRSA+AES256+SHA;\n        ssl_stapling on;\n        ssl_stapling_verify on;\n        location \/ {\n           proxy_set_header Accept-Encoding \"\";\n           proxy_pass https:\/\/cloudbool.com\/archive\/;\n           proxy_set_header   Host    $host;\n           proxy_redirect off;\n           proxy_set_header X-Real-IP $remote_addr;\n           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        }\n        access_log \/var\/log\/nginx\/cloudbool.log;\n        error_log \/var\/log\/nginx\/cloudbool-error.log;\n        error_page 404 \/404.html;\n        error_page 500 502 503 504 \/50x.html;\n        location = \/50x.html {\n                root \/usr\/share\/nginx\/html;\n        }\n}\nserver\n{\n        listen          80;\n        server_name cloudbool.com www.cloudbool.com;\n        location \/ {\n                rewrite ^\/(.*)$ https:\/\/cloudbool.com\/archive\/$1 permanent;\n        }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u56e0\u5386\u53f2\u539f\u56e0\uff0c\u624b\u4e0a\u6709\u51e0\u4e2a\u7ad9\u4e00\u76f4\u5728\u56fd\u5916\u98d8\u7740\uff0c\u4f17\u6240\u5468\u77e5\uff0c\u7f51\u7edc\u98a0\u7c38\uff0c\u66f4\u522b\u8bf4\u8fd8\u8de8\u4e86\u4e2a\u592a\u5e73\u6d0b\u4e86\uff0c\u5927\u591a\u6570\u56de\u56fd\u7ebf\u8def\u665a\u9ad8\u5cf0\u7206\u70b8\u662f &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/cloudbool.com\/archive\/nginx-proxy-real-ip.html\"> <span class=\"screen-reader-text\">Nginx\u53cd\u5411\u4ee3\u7406\u540e\u83b7\u5f97\u771f\u5b9e\u7528\u6237IP<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"footnotes":""},"categories":[138],"tags":[76],"class_list":["post-155","post","type-post","status-publish","format-standard","hentry","category-share","tag-nginx"],"_links":{"self":[{"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/comments?post=155"}],"version-history":[{"count":0,"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/posts\/155\/revisions"}],"wp:attachment":[{"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/categories?post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/tags?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}