LoginSignup
4
4

More than 5 years have passed since last update.

(8) EC2にnginx経由のunicorn|EC2でgit+RonR+unicorn+nginx+capistrano+Jenkins

Posted at

その8|nginx経由でunicorn

1.これまでの目次

  1. EC2+gitリモートサーバ構築#1
  2. (2)EC2+gitリモートサーバ構築#2
  3. (3)EC2にrvm+rubyインストール
  4. (4)EC2にrailsインストール
  5. (5)EC2にrails起動テスト
  6. (6)EC2にunicorn準備
  7. (7) EC2にnginx準備

参考サイト|プログラミングノート

2.nginx設定

とりあえず、いろいろと環境ややりたい事によって分割したりすると思いますが、動作の仕組みとして、こんな感じになる。

  • upstream

    今回はunicornでunixソケットを使う事にしている(httpソケットも選択出来る)。このソケットがunicorn入り口になるので、それを定数unicorn_appとして定義

  • server/locationディレクティブでserver_name _;としているところは、とりあえず全てのFQDNに対応する意味。httpプロトコル80番ポートで上記したunicornソケットへプロキシさせる。

/etc/nginx/nginx.conf

user nginx;
worker_processes 1;

pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log;

events {
  worker_connections 1024;
}

http {

  include mime.types;
  default_type  application/octet-stream;

  access_log /var/log/nginx/access.log;
  sendfile on;
  keepalive_timeout  65;

  upstream unicorn_app {
   server unix:/tmp/unicorn.sock;
  }

  server {
    listen 80;
    server_name _;
    location / {
      proxy_pass  http://unicorn_app;    
    }
  }
}

3.動作確認

rails project rootにcdし、unicornを実行後、nginxを起動し、
ブラウザで確認。railsアプリの画面が出ればOK

cd /work/sample/
unicorn -c config/unicorn.rb -D
/etc/rc.d/init.d/nginx start
4
4
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
4
4