getalog

console.log geta6

nginx + node.js

nginxが静的ファイルとゲートウェイを担当、node.jsが動的ファイルを担当、というスタイルでウェブサーバを運用している。

新しくアプリケーション作ろうと思った時、逐一nginxにupstreamを作ってnode.jsへPROXYするのは面倒だし煩雑である。設定が膨大になると読み返すのも面倒で、アプリケーション実装自体が億劫になってしまう。

手間と効果を鑑みて、node.jsでnode.js用のゲートウェイを立てることにした。

nginxは下記のように設定する、追加はserver_nameのところを増やして再起動するだけ。

upstream nodejs {
  server 127.0.0.1:3000;
}
server {
  listen       80;
  charset      UTF-8;
  server_name  hoge.example.com fuga.example.com poyo.example.com;

  location / {
    include     includes/proxy.conf;
    proxy_pass  http://nodejs;
  }
}

で、localhost:3000では下記のnode.jsを走らせておく。

ドメインの追加時にはswitch文の中でドメイン振り分けをして再起動する。

起動にはnpm -g install foreverしてからnpm startする、停止はnpm stop、再起動はnpm restart

websocketをPROXY経由で供給したい時は、ポート3000番を開放してexample.com:3000を参照させれば良い。