22
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nginx + foreverで、Webアプリ管理らくらく

Posted at

最近、websocket(socket.io)をけっこう重点的に使ってます。

仕事以外でも先日アプリを書いてみて、公開してみよーと思ったのですが、
なるほど、apacheはwebsocket使えないんすね。

node-http-proxyを導入するのもなんだか面倒くさそうだったので、
いいかげんに自鯖をnginxに乗り換えることに。
現状でもnodeアプリばっかりで、ProxyPassだらけだったし。

環境

nginx、最新版がほしい

最初は何も考えずに、「yum install nginx」でインストールしたんですが、
これで落ちてくるのは、なかなかの旧型nginxらしい。websocketつかえない!

ので、rpmを公式から落としてきてのインストールになります。

あとはwebsocket使いたいアプリの設定に、3行ほど追加するだけ。

  location / {
    proxy_pass http://hogehoge;
    allow all;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }


楽! 素敵! 超はやい!

sites-available / sites-enabledで管理したい

以前、触っていたdebian系のサーバーでは、

  • /etc/nginx.conf で include /etc/nginx/sites-enabled/* しておく
  • /etc/nginx/sites-available にアプリごとの設定を置く
  • アプリを有効化したいときは、availableからenabledへsymlinkを張る

という構成になっていたので、これを真似してみた。

もちろんapacheでもちゃんと管理すればそうできるんですが、
アプリごとに独立した設定・管理がしやすいnginxは素敵です。

nodeアプリ追加の流れ

また、nodeアプリはぜんぶforeverで管理・daemon化しているので、こちらもらくちん。

nodeのwebアプリを追加するときの流れは、いまこんな感じ。

  1. 開発する。(portは決めておく)
  2. サブドメイン切っておく (たとえば、hogehoge.fnobi.com)
  3. git通して、プロジェクトをサーバー上へ (たとえば、~/service/hogehoge)
  4. foreverを使って起動。 (forever ~/service/hogehoge/app.js)
  5. nginxの設定追加。(たとえば /nginx/sites-available/hogehoge.fnobi.com)
  6. symlink (ln -s /nginx/sites-available/hogehoge.fnobi.com /nginx/sites-enabled/)
  7. nginx再起動 (sudo /etc/init.d/nginx restart)

ふむ、だいぶすっきり、分りやすくなった気がするけれど、
もっと効率化もできそう。

22
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?