{"id":5881,"date":"2020-02-18T19:11:31","date_gmt":"2020-02-18T10:11:31","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=5881"},"modified":"2020-03-11T07:38:31","modified_gmt":"2020-03-10T22:38:31","slug":"%e3%80%90express-nginx%e3%80%91ip%e3%82%a2%e3%83%89%e3%83%ac%e3%82%b9%e3%81%8c%e3%80%8cffff127-0-0-1%e3%80%8d%e3%81%97%e3%81%8b%e5%8f%96%e5%be%97%e3%81%a7%e3%81%8d%e3%81%aa%e3%81%84%ef%bc%81","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=5881","title":{"rendered":"\u3010Express + nginx\u3011IP\u30a2\u30c9\u30ec\u30b9\u304c\u300c::ffff:127.0.0.1\u300d\u3057\u304b\u53d6\u5f97\u3067\u304d\u306a\u3044\uff01\uff1f"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001<code>Express<\/code>\u958b\u767a\u3092\u9032\u3081\u3066\u304d\u3066\u306f\u3084\u6570\u30ab\u6708\u304c\u7d4c\u3061\u3001\u3044\u304f\u3064\u3082\u300c\u3067\u304d\u306a\u3044 \u2192 \u89e3\u6c7a\u300d\u3092\u4f53\u9a13\u3057\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u3057\u3066\u305d\u3093\u306a\u4e2d\u3067\u3082\u3001\u300c<strong>\u3046\u3063\u3001\u3001\u8a00\u308f\u308c\u3066\u307f\u308c\u3070\u5f53\u305f\u308a\u524d\u3067\u3059\u3088\u306d<\/strong>\u30fb\u30fb\u30fb\u300d\u3068\u3044\u3046\u5185\u5bb9\u304c\u3042\u3063\u305f\u306e\u3067\u3053\u3053\u3067\u5099\u5fd8\u9332\u7684\u306b\u8a18\u4e8b\u3092\u3064\u304f\u3063\u3066\u304a\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u5185\u5bb9\u306f\u3001\u3068\u3044\u3046\u3068\u30fb\u30fb\u30fb<\/p>\n<p><strong style=\"font-size: 35px;\">Express\u3067IP\u30a2\u30c9\u30ec\u30b9\u304c \u300c::ffff:127.0.0.1\u300d\u3057\u304b\u53d6\u5f97\u3067\u304d\u306a\u3044\ud83d\ude2b<\/strong><\/p>\n<p>\u3068\u3044\u3046\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u306f\u3001<code>Express<\/code> + <code>nginx<\/code>\u3067\u904b\u7528\u3059\u308b\u5834\u5408\u306e\u3061\u3087\u3063\u3068\u3057\u305f\u7f60\uff08\u7b11\uff09\u306e\u3088\u3046\u306a\u3082\u306e\u306a\u306e\u3067\u3059\u304c\u3001\u30e1\u30a4\u30f3\u306e\u958b\u767a\u3092\u9577\u5e74<code>PHP<\/code>\u3067\u3084\u3063\u3066\u304d\u305f\u79c1\u306f\u3001\u304d\u3063\u3061\u308a\u3053\u306e\u7f60\u306b\u30cf\u30de\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u7686\u3055\u3093\u304c\u79c1\u306e\u3088\u3046\u306b\u7f60\u306b\u30cf\u30de\u3089\u306a\u3044\u3088\u3046\u3001<code>Express<\/code> + <code>nginx<\/code>\u3067IP\u30a2\u30c9\u30ec\u30b9\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u306e\u304a\u5f79\u306b\u7acb\u3066\u308b\u3068\u5b09\u3057\u3044\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<p>\u203b\u89e3\u6c7a\u6cd5\u306b\u6025\u3050\u5834\u5408 \u2192 <a href=\"#get_ip_address\">\u5b9f\u969b\u306b\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3344 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22.png\" alt=\"\" width=\"320\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22.png 320w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22-150x150.png 150w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22-300x300.png 300w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_22-100x100.png 100w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Node 8\u3001Express 4.1\u3001Ubuntu 18.04<\/p>\n<h1>\u4f55\u304c\u539f\u56e0\u306a\u306e\u304b\uff1f\uff1f<\/h1>\n<p>\u3042\u308b\u30c9\u30e1\u30a4\u30f3\u3067<code>Express<\/code>\u3092\u904b\u7528\u3059\u308b\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<ol>\n<li>\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u30a2\u30af\u30bb\u30b9<\/li>\n<li>nginx \u3092\u4e2d\u7d99<\/li>\n<li>Express\u306b\u30a2\u30af\u30bb\u30b9<\/li>\n<\/ol>\n<p>\u3053\u308c\u306f\u3001<code>Express<\/code>\u3060\u3051\u3067\u8d77\u52d5\u3059\u308b\u3068\u3001<\/p>\n<pre><strong>http:\/\/localhost:5000<\/strong><\/pre>\n<p>\u3068\u3044\u3046<code>URL<\/code>\u306b\u306a\u308b\u306e\u3067\u3001\u30c9\u30e1\u30a4\u30f3\u3092\u4f7f\u3046\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>Express<\/code>\u3078\u300c\u8ee2\u9001\u300d\u3059\u308b\u51e6\u7406\u304c\u5fc5\u8981\u306b\u306a\u3063\u3066\u304f\u308b\u305f\u3081\u3067\u3059\u3002<\/p>\n<pre>server {\n    listen 80;\n    server_name express.test;\n    location \/ {\n        <strong>proxy_pass http:\/\/localhost:5000;<\/strong>\n     }\n}<\/pre>\n<p>\u3057\u304b\u3057\u3001IP\u30a2\u30c9\u30ec\u30b9\u304c\u3046\u307e\u304f\u53d6\u5f97\u3067\u304d\u306a\u3044\u554f\u984c\u70b9\u306f\u307e\u3055\u306b\u3053\u3053\u306b\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001<code>PHP<\/code>\u306e\u5834\u5408\u306f<code>nginx\u304c<\/code>\u76f4\u63a5\u30a6\u30a7\u30d6\u30b5\u30fc\u30d0\u30fc\u306b\u306a\u308a\u307e\u3059\u304c\u3001Express\u306e\u5834\u5408\u306f\u4ee3\u7406\u4eba\u3068\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u3001<strong>\u305d<\/strong><strong><strong>\u306e\u307e\u307e\u3060\u3068<\/strong><\/strong><strong><code><strong>nginx<\/strong><\/code><\/strong><strong>\u306eIP\u30a2\u30c9\u30ec\u30b9\u304c\u53d6\u5f97\u3055\u308c\u3066\u3057\u307e\u3046<\/strong>\u3068\u3044\u3046\u308f\u3051\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001<code>::ffff:127.0.0.1<\/code>\u306f\u3001<code>nginx<\/code>\uff08\u81ea\u5206\u306e\u30b5\u30fc\u30d0\u30fc\uff09\u306e\u3053\u3068\u3060\u3063\u305f\u308f\u3051\u3067\u3059\u306d\u3002<\/p>\n<h1>\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5\u8ad6<\/h1>\n<p>\u3053\u308c\u307e\u3067\u306f\u3001<code>nginx<\/code>\u304c\u4ee3\u7406\u4eba\u3060\u3063\u305f\u306e\u3067IP\u30a2\u30c9\u30ec\u30b9\u306f\u300c<strong>::ffff:127.0.0.1<\/strong>\u300d\u3067\u56fa\u5b9a\u3067\u3057\u305f\u304c\u3001<code>nginx<\/code>\u306b\u306fIP\u30a2\u30c9\u30ec\u30b9\u3092\u5909\u6570\u3068\u3057\u3066\u4f7f\u3048\u308b\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u3053\u306e\u5909\u6570\u3092\u7279\u5b9a\u306e\u30d8\u30c3\u30c0\u30fc\u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u3042\u305f\u304b\u3082IP\u30a2\u30c9\u30ec\u30b9\u3092\u300c<strong>\u6c34\u30d0\u30b1\u30c4\u30ea\u30ec\u30fc<\/strong>\u300d\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u308f\u3051\u3067\u3059\u3002<\/p>\n<ol>\n<li>\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u30a2\u30af\u30bb\u30b9<\/li>\n<li>nginx \u3092\u4e2d\u7d99\uff08IP\u30a2\u30c9\u30ec\u30b9\u3092\u30d8\u30c3\u30c0\u30fc\u306b\u8ffd\u52a0\uff09<\/li>\n<li>Express\u5185\u3067\u3001\u305d\u306e\u30d8\u30c3\u30c0\u30fc\u304b\u3089IP\u30a2\u30c9\u30ec\u30b9\u3092\u53d6\u5f97\u3059\u308b<\/li>\n<\/ol>\n<p>\u3067\u306f\u3001\u6b21\u304b\u3089\u306f\u5b9f\u969b\u306e\u4f5c\u696d\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\uff01<\/p>\n<div id=\"get_ip_address\"><\/div>\n<h1>\u5b9f\u969b\u306b\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5<\/h1>\n<p>\u3053\u3053\u304b\u3089\u306f\u5b9f\u969b\u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u4eba\u306eIP\u30a2\u30c9\u30ec\u30b9\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3092\u3054\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n<p>\u203b\u79c1\u306e\u74b0\u5883\u306f<code>Ubuntu 18.04<\/code>\u3067\u3059\u304c\u3001<code>CentOS<\/code>\u306a\u3069\u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u9069\u5b9c\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>nginx\u306e\u30b3\u30f3\u30d5\u30a3\u30b0\u3092\u5909\u66f4\u3059\u308b<\/h2>\n<p>\u307e\u305a\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067nginx\u306e\u8a2d\u5b9a\u3092\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>sudo vi \/etc\/nginx\/sites-available\/default<\/strong><\/pre>\n<p>\u305d\u3057\u3066\u3001\u300c<strong>X-Forwarded-For<\/strong>\u300d\u306bIP\u30a2\u30c9\u30ec\u30b9\u3092\u30bb\u30c3\u30c8\u3057\u307e\u3059\u3002<\/p>\n<pre>server {\n    listen 80;\n    server_name express.test;\n    location \/ {\n        proxy_pass http:\/\/localhost:5000;\n        <strong>proxy_set_header X-Forwarded-For $remote_addr;<\/strong>\n     }\n}<\/pre>\n<p>\u305d\u3057\u3066<code>:wq!<\/code>\u306a\u3069\u3067\u4fdd\u5b58\u3057\u305f\u3089\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067<code>nginx<\/code>\u3092\u518d\u8d77\u52d5\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>sudo systemctl restart nginx<\/strong><\/pre>\n<p>\u3053\u308c\u3067<code>nginx<\/code>\u5074\u306e\u4f5c\u696d\u306f\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n<h2>nginx\u304b\u3089\u9001\u4fe1\u3055\u308c\u305fIP\u30a2\u30c9\u30ec\u30b9\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u7d9a\u3044\u3066<code>Express<\/code>\u5074\u306e\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n<p>\u3068\u3044\u3063\u3066\u3082\u3001<code>req<\/code>\u304b\u3089\u30d8\u30c3\u30c0\u30fc\u60c5\u5831\u3092\u53d6\u5f97\u3059\u308b\u3060\u3051\u3067OK\u3067\u3059\u3002<\/p>\n<pre>app.get('\/ip', (req, res) =&gt; {\n\n  <strong>const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;<\/strong>\n  res.send(ip);\n\n});<\/pre>\n<p>\u306a\u304a\u3001<code>req.connection.remoteAddress<\/code>\u306f\u300c<strong>x-forwarded-for<\/strong>\u300d\u304c\u5b58\u5728\u3057\u306a\u3044\uff08\u3064\u307e\u308a\u3001\u30c9\u30e1\u30a4\u30f3\u4ee5\u5916\u3067\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\uff09\u5834\u5408\u306e\u4fdd\u967a\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u30c9\u30e1\u30a4\u30f3\u4ee5\u5916\u306e\u30a2\u30af\u30bb\u30b9\u306e\u5834\u5408\u3001<code>req.headers['x-forwarded-for']<\/code>\u304b\u3089IP\u30a2\u30c9\u30ec\u30b9\u3092\u53d6\u5f97\u3057\u3088\u3046\u3068\u3057\u3066\u3082<code>undefined<\/code>\u304c\u8fd4\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u300c<strong>Express + nginx<\/strong>\u300d\u3067IP\u30a2\u30c9\u30ec\u30b9\u304c\u56fa\u5b9a\u3055\u308c\u3066\u3057\u307e\u3046\u5834\u5408\u306e\u89e3\u6c7a\u6cd5\u3092\u304a\u5c4a\u3051\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u5192\u982d\u3067\u3082\u66f8\u304d\u307e\u3057\u305f\u304c\u3001<code>nginx<\/code>\u304c\u9593\u306b\u306f\u3055\u307e\u3063\u3066\u3044\u308b\u306e\u3067\u3001IP\u30a2\u30c9\u30ec\u30b9\u304c\u3046\u307e\u304f\u53d6\u5f97\u3067\u304d\u306a\u3044\u306e\u3082\u5f53\u7136\u306e\u8a71\u3067\u3059\u3088\u306d\u3002\u3084\u3063\u3071\u308a\u56fa\u5b9a\u6982\u5ff5\u3067\u52d5\u3053\u3046\u3068\u3059\u308b\u3068\u7f60\u306b\u30cf\u30de\u3063\u3066\u3057\u307e\u3044\u3084\u3059\u3044\u3067\u3059\ud83d\udca6<\/p>\n<p>\u79c1\u306e\u5834\u5408\u3001\u3053\u308c\u306e\u305b\u3044\u3067\u6570\u6642\u9593\u7121\u99c4\u306b\u3057\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u304c\u3001\u3053\u306e\u8a18\u4e8b\u3067\u7686\u3055\u3093\u306e\u6642\u9593\u3092\u30b7\u30e7\u30fc\u30c8\u30ab\u30c3\u30c8\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u305f\u3089\u5b09\u3057\u3044\u3067\u3059\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3467 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9.png\" alt=\"\" width=\"216\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9.png 216w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9-203x300.png 203w\" sizes=\"auto, (max-width: 216px) 100vw, 216px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001Express\u958b\u767a\u3092\u9032\u3081\u3066\u304d\u3066\u306f\u3084\u6570\u30ab\u6708\u304c\u7d4c\u3061\u3001\u3044\u304f\u3064\u3082\u300c\u3067\u304d\u306a\u3044 \u2192 \u89e3\u6c7a\u300d\u3092\u4f53\u9a13\u3057\u3066\u304d\u307e\u3057\u305f\u3002 \u305d\u3057\u3066\u305d\u3093\u306a\u4e2d\u3067\u3082\u3001\u300c\u3046\u3063\u3001\u3001\u8a00\u308f\u308c\u3066\u307f\u308c\u3070\u5f53\u305f\u308a &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=5881\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u3010Express + nginx\u3011IP\u30a2\u30c9\u30ec\u30b9\u304c\u300c::ffff:127.0.0.1\u300d\u3057\u304b\u53d6\u5f97\u3067\u304d\u306a\u3044\uff01\uff1f&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":5883,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[201,73],"tags":[205,33],"class_list":["post-5881","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-express","category-73","tag-ip","tag-nginx"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5881"}],"version-history":[{"count":6,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5881\/revisions"}],"predecessor-version":[{"id":6167,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5881\/revisions\/6167"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/5883"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}