mazeltov7のweb断片

備忘録的なテキトーなことを書きます。(技術記事はQiitaに移行しました http://qiita.com/mazeltov7 )

nginxとunicornでrailsアプリを表示させる

とりあえず表示させるだけなので、簡易設定。
以下らへんをざくっと参考にさせて頂いた。
http://railscasts.com/episodes/293-nginx-unicorn
http://blog.livedoor.jp/sasata299/archives/51810645.html
http://d.hatena.ne.jp/ntaku/20111112/1321093327
http://semind.github.io/blog/2012/01/19/nginx-plus-unicornderails3-dot-1gadong-zuo-suruhuan-jing-wozuo-ru/
http://blog.tanitanin.net/?p=387
http://blog.notsobad.jp/post/36942239092/nginx-unicorn

で、まずnginx、

$ yum install nginx

して、

とりあえず、nginx.confに以下を入れてみる

user  nginx;
worker_processes  2;

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


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;


	upstream backend {
		server 127.0.0.1:3000;
		}

	server {
		listen 80;
		server_name hoge.com;
		root  /usr/share/nginx/html/hoge/public;

		location / {
			proxy_pass http://backend;
			proxy_set_header X-Forwarded-Host $host;
		}

		error_page 500 502 503 504 /500.html;

		location /doc/ {
			alias /usr/share/doc/;
			autoindex on;
			allow 127.0.0.1;
			allow ::1;
			deny all;
		}
	}

}

backendとかやってるのは、unicornで動かしたところのつなぎ。
で、unicornをbundle installとかして、webアプリのconfig/unicorn.rbとかに

worker_processes 2
working_directory /usr/share/nginx/html/hoge
 
listen '/usr/share/nginx/html/hoge/tmp/unicorn.sock'
pid '/usr/share/nginx/html/hoge/tmp/unicorn.pid'
 
timeout 60
 
preload_app true # ダウンタイムをなくす
 
stdout_path '/usr/share/nginx/html/hoge/log/unicorn.stdout.log'
stderr_path '/usr/share/nginx/html/hoge/log/unicorn.stderr.log'

とか書いてやる。
で、動かす。

$ bundle exec unicorn -c config/unicorn.rb -E development -p 3000 -D

このときrootになってやらなかったら怒られたりなどした。

で、psとかで見て動いてたらおk

あとは、ブラウザで確認して、表示されたらgood! yay!