nodeをデーモン化してnginxをリバースプロキシとして連動させる
Node.jsアプリをNginxと連動させてみます。
やることは2つ
・node.jsをデーモン化します。
・nginxをリバースプロキシとして稼働させます。
目次
nginxにリバースプロキシとして設定をする
conf.dのhoge.com.confをを設定する
#vi conf.d/hoge.com.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<span style="font-size: 14pt;">upstream backend_node {</span> <span style="font-size: 14pt;"> ip_hash;</span> <span style="font-size: 14pt;"> server 127.0.0.1:3000;</span> <span style="font-size: 14pt;">}</span> <span style="font-size: 14pt;">server {</span> <span style="font-size: 14pt;"> listen 80;</span> <span style="font-size: 14pt;"> server_name hoge.com;</span> <span style="font-size: 14pt;">:</span> <span style="font-size: 14pt;">:</span> <span style="font-size: 14pt;"> #---- To redirect setting</span> <span style="font-size: 14pt;">proxy_redirect off;</span> <span style="font-size: 14pt;">proxy_set_header Host $http_host;</span> <span style="color: #0000ff;"><span style="font-size: 14pt;">localhostをnginxが受け取ったリクエストのホスト名に書き直す</span> <span style="font-size: 14pt;">proxy_redirect default;でこの設定なしだと、どこからきてもlocalhostになってしまう(^^;</span></span> <span style="font-size: 14pt;">proxy_set_header X-Real-IP $remote_addr;</span> <span style="color: #0000ff;"><span style="font-size: 14pt;">アプリケーション層の情報。</span> <span style="font-size: 14pt;">ロードバランサやプロキシを経由する時に送信元を判別するために利用。</span> <span style="font-size: 14pt;">x-real-ipにクライアントIPを設定してAPIへ渡す。</span> <span style="font-size: 14pt;">$remote_addrはアクセス元のIP。ネットワーク層の情報。</span> <span style="font-size: 14pt;">基本的に直前のIPを保持している。</span></span> <span style="font-size: 14pt;">proxy_set_header X-Forwarded-Host $host;</span> <span style="font-size: 14pt;">proxy_set_header X-Forwarded-Server $host;</span> <span style="color: #0000ff;"><span style="font-size: 14pt;">アプリケーション層の情報。</span> <span style="font-size: 14pt;">ホスト名を渡す</span></span> <span style="font-size: 14pt;">proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span> <span style="color: #0000ff;"><span style="font-size: 14pt;">アプリケーション層の情報。</span> <span style="font-size: 14pt;">ロードバランサやプロキシを経由する時に送信元を判別するために利用。</span> <span style="font-size: 14pt;">x-forwarded-forのIPアドレスをAPIへ渡す。</span></span> <span style="font-size: 14pt;">prosy_set_header X-NginX-Proxy true;</span> <span style="font-size: 14pt;">:</span> <span style="font-size: 14pt;">:</span> <span style="font-size: 14pt;"> location / {</span> <span style="font-size: 14pt;"> root /opt/www/ngxtst.kumakake.com/html;</span> <span style="font-size: 14pt;"> index index.html index.htm;</span> |
1 2 3 4 |
# 存在するファイルパスが指定された時はnginxで処理する <span style="font-size: 14pt;"> if ( -f $request_filename ) {</span> <span style="font-size: 14pt;"> break;</span> <span style="font-size: 14pt;"> }</span> |
1 2 3 4 5 6 7 8 9 10 11 12 |
# ファイルパスが存在しない時はbackend_nodeで処理する <span style="font-size: 14pt;"> if ( !-f $request_filename ) {</span> <span style="font-size: 14pt;"> proxy_pass http://backend_node;</span> <span style="font-size: 14pt;"> break;</span> <span style="font-size: 14pt;"> }</span> <span style="font-size: 14pt;"> } ※ifディレクティブはifと(の間は空白をあけること if ( xxxx if( xxxとすると nginx: [emerg] unknown directive "prosy_set_header" in /etc/nginx/conf.d/hoge.com.conf:21 って怒られます(^^;</span> |
nodeをforeverでデーモン化する
foreverをnpmでインストールします
# npm install forever -g
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
<span style="font-size: 14pt;">/usr/local/bin/forever -> /usr/local/lib/node_modules/forever/bin/forever</span> <span style="font-size: 14pt;">/usr/local/lib</span> <span style="font-size: 14pt;">`-- forever@0.15.3</span> <span style="font-size: 14pt;"> +-- cliff@0.1.10</span> <span style="font-size: 14pt;"> | +-- colors@1.0.3</span> <span style="font-size: 14pt;"> | `-- eyes@0.1.8</span> <span style="font-size: 14pt;"> +-- clone@1.0.2</span> <span style="font-size: 14pt;"> +-- colors@0.6.2</span> <span style="font-size: 14pt;"> +-- flatiron@0.4.3</span> <span style="font-size: 14pt;"> | +-- broadway@0.3.6</span> <span style="font-size: 14pt;"> | | +-- cliff@0.1.9</span> <span style="font-size: 14pt;"> | | `-- winston@0.8.0</span> <span style="font-size: 14pt;"> | +-- director@1.2.7</span> <span style="font-size: 14pt;"> | +-- optimist@0.6.0</span> <span style="font-size: 14pt;"> | `-- prompt@0.2.14</span> <span style="font-size: 14pt;"> | +-- read@1.0.7</span> <span style="font-size: 14pt;"> | | `-- mute-stream@0.0.7</span> <span style="font-size: 14pt;"> | `-- revalidator@0.1.8</span> <span style="font-size: 14pt;"> +-- forever-monitor@1.7.1</span> <span style="font-size: 14pt;"> | +-- chokidar@1.7.0</span> <span style="font-size: 14pt;"> | | +-- anymatch@1.3.0</span> <span style="font-size: 14pt;"> | | | +-- arrify@1.0.1</span> <span style="font-size: 14pt;"> | | | `-- micromatch@2.3.11</span> <span style="font-size: 14pt;"> | | | +-- arr-diff@2.0.0</span> <span style="font-size: 14pt;"> | | | | `-- arr-flatten@1.0.3</span> <span style="font-size: 14pt;"> | | | +-- array-unique@0.2.1</span> <span style="font-size: 14pt;"> | | | +-- braces@1.8.5</span> <span style="font-size: 14pt;"> | | | | +-- expand-range@1.8.2</span> <span style="font-size: 14pt;"> | | | | | `-- fill-range@2.2.3</span> <span style="font-size: 14pt;"> | | | | | +-- is-number@2.1.0</span> <span style="font-size: 14pt;"> | | | | | +-- isobject@2.1.0</span> <span style="font-size: 14pt;"> | | | | | +-- randomatic@1.1.7</span> <span style="font-size: 14pt;"> | | | | | | +-- is-number@3.0.0</span> <span style="font-size: 14pt;"> | | | | | | | `-- kind-of@3.2.2</span> <span style="font-size: 14pt;"> | | | | | | `-- kind-of@4.0.0</span> <span style="font-size: 14pt;"> | | | | | `-- repeat-string@1.6.1</span> <span style="font-size: 14pt;"> | | | | +-- preserve@0.2.0</span> <span style="font-size: 14pt;"> | | | | `-- repeat-element@1.1.2</span> <span style="font-size: 14pt;"> | | | +-- expand-brackets@0.1.5</span> <span style="font-size: 14pt;"> | | | | `-- is-posix-bracket@0.1.1</span> <span style="font-size: 14pt;"> | | | +-- extglob@0.3.2</span> <span style="font-size: 14pt;"> | | | +-- filename-regex@2.0.1</span> <span style="font-size: 14pt;"> | | | +-- kind-of@3.2.2</span> <span style="font-size: 14pt;"> | | | | `-- is-buffer@1.1.5</span> <span style="font-size: 14pt;"> | | | +-- normalize-path@2.1.1</span> <span style="font-size: 14pt;"> | | | | `-- remove-trailing-separator@1.0.2</span> <span style="font-size: 14pt;"> | | | +-- object.omit@2.0.1</span> <span style="font-size: 14pt;"> | | | | +-- for-own@0.1.5</span> <span style="font-size: 14pt;"> | | | | | `-- for-in@1.0.2</span> <span style="font-size: 14pt;"> | | | | `-- is-extendable@0.1.1</span> <span style="font-size: 14pt;"> | | | +-- parse-glob@3.0.4</span> <span style="font-size: 14pt;"> | | | | +-- glob-base@0.3.0</span> <span style="font-size: 14pt;"> | | | | `-- is-dotfile@1.0.3</span> <span style="font-size: 14pt;"> | | | `-- regex-cache@0.4.3</span> <span style="font-size: 14pt;"> | | | +-- is-equal-shallow@0.1.3</span> <span style="font-size: 14pt;"> | | | `-- is-primitive@2.0.0</span> <span style="font-size: 14pt;"> | | +-- async-each@1.0.1</span> <span style="font-size: 14pt;"> | | +-- glob-parent@2.0.0</span> <span style="font-size: 14pt;"> | | +-- inherits@2.0.3</span> <span style="font-size: 14pt;"> | | +-- is-binary-path@1.0.1</span> <span style="font-size: 14pt;"> | | | `-- binary-extensions@1.8.0</span> <span style="font-size: 14pt;"> | | +-- is-glob@2.0.1</span> <span style="font-size: 14pt;"> | | | `-- is-extglob@1.0.0</span> <span style="font-size: 14pt;"> | | `-- readdirp@2.1.0</span> <span style="font-size: 14pt;"> | | +-- graceful-fs@4.1.11</span> <span style="font-size: 14pt;"> | | +-- readable-stream@2.2.11</span> <span style="font-size: 14pt;"> | | | +-- core-util-is@1.0.2</span> <span style="font-size: 14pt;"> | | | +-- isarray@1.0.0</span> <span style="font-size: 14pt;"> | | | +-- process-nextick-args@1.0.7</span> <span style="font-size: 14pt;"> | | | +-- safe-buffer@5.0.1</span> <span style="font-size: 14pt;"> | | | +-- string_decoder@1.0.2</span> <span style="font-size: 14pt;"> | | | `-- util-deprecate@1.0.2</span> <span style="font-size: 14pt;"> | | `-- set-immediate-shim@1.0.1</span> <span style="font-size: 14pt;"> | +-- minimatch@3.0.4</span> <span style="font-size: 14pt;"> | | `-- brace-expansion@1.1.8</span> <span style="font-size: 14pt;"> | | +-- balanced-match@1.0.0</span> <span style="font-size: 14pt;"> | | `-- concat-map@0.0.1</span> <span style="font-size: 14pt;"> | `-- ps-tree@0.0.3</span> <span style="font-size: 14pt;"> | `-- event-stream@0.5.3</span> <span style="font-size: 14pt;"> | `-- optimist@0.2.8</span> <span style="font-size: 14pt;"> +-- nconf@0.6.9</span> <span style="font-size: 14pt;"> | +-- async@0.2.9</span> <span style="font-size: 14pt;"> | +-- ini@1.3.4</span> <span style="font-size: 14pt;"> | `-- optimist@0.6.0</span> <span style="font-size: 14pt;"> +-- nssocket@0.5.3</span> <span style="font-size: 14pt;"> | +-- eventemitter2@0.4.14</span> <span style="font-size: 14pt;"> | `-- lazy@1.0.11</span> <span style="font-size: 14pt;"> +-- object-assign@3.0.0</span> <span style="font-size: 14pt;"> +-- optimist@0.6.1</span> <span style="font-size: 14pt;"> | +-- minimist@0.0.10</span> <span style="font-size: 14pt;"> | `-- wordwrap@0.0.3</span> <span style="font-size: 14pt;"> +-- path-is-absolute@1.0.1</span> <span style="font-size: 14pt;"> +-- prettyjson@1.2.1</span> <span style="font-size: 14pt;"> | +-- colors@1.1.2</span> <span style="font-size: 14pt;"> | `-- minimist@1.2.0</span> <span style="font-size: 14pt;"> +-- shush@1.0.0</span> <span style="font-size: 14pt;"> | +-- caller@0.0.1</span> <span style="font-size: 14pt;"> | | `-- tape@2.3.3</span> <span style="font-size: 14pt;"> | | +-- deep-equal@0.1.2</span> <span style="font-size: 14pt;"> | | +-- defined@0.0.0</span> <span style="font-size: 14pt;"> | | +-- jsonify@0.0.0</span> <span style="font-size: 14pt;"> | | +-- resumer@0.0.0</span> <span style="font-size: 14pt;"> | | `-- through@2.3.8</span> <span style="font-size: 14pt;"> | `-- strip-json-comments@0.1.3</span> <span style="font-size: 14pt;"> +-- timespan@2.3.0</span> <span style="font-size: 14pt;"> +-- utile@0.2.1</span> <span style="font-size: 14pt;"> | +-- async@0.2.10</span> <span style="font-size: 14pt;"> | +-- deep-equal@1.0.1</span> <span style="font-size: 14pt;"> | +-- i@0.3.5</span> <span style="font-size: 14pt;"> | +-- mkdirp@0.5.1</span> <span style="font-size: 14pt;"> | | `-- minimist@0.0.8</span> <span style="font-size: 14pt;"> | +-- ncp@0.4.2</span> <span style="font-size: 14pt;"> | `-- rimraf@2.6.1</span> <span style="font-size: 14pt;"> | `-- glob@7.1.2</span> <span style="font-size: 14pt;"> | +-- fs.realpath@1.0.0</span> <span style="font-size: 14pt;"> | +-- inflight@1.0.6</span> <span style="font-size: 14pt;"> | | `-- wrappy@1.0.2</span> <span style="font-size: 14pt;"> | `-- once@1.4.0</span> <span style="font-size: 14pt;"> `-- winston@0.8.3</span> <span style="font-size: 14pt;"> +-- cycle@1.0.3</span> <span style="font-size: 14pt;"> +-- isstream@0.1.2</span> <span style="font-size: 14pt;"> +-- pkginfo@0.3.1</span> <span style="font-size: 14pt;"> `-- stack-trace@0.0.10</span> |
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/forever/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.2: wanted {“os”:”darwin”,”arch”:”any”} (current: {“os”:”linux”,”arch”:”x64″})
# which forever
/usr/local/bin/forever
起動します
$ forever start www
warn: –minUptime not set. Defaulting to: 1000ms
warn: –spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: www
状態を確認します
$ forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] ML5b /usr/local/bin/node www 23978 23984 /home/kumakake/.forever/ML5b.log 0:0:0:14.581
停止します
モジュール名を指定する
$ forever stop www
error: Forever cannot find process with id: www
uidを指定する
$ forever stop 23978
error: Forever cannot find process with id: 23978
状態を確認します
$ forever list
info: No forever processes running