Skip to content

Instantly share code, notes, and snippets.

@Shinpeim
Created July 12, 2012 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shinpeim/3095986 to your computer and use it in GitHub Desktop.
Save Shinpeim/3095986 to your computer and use it in GitHub Desktop.
rails + unicorn + nginx の立ちあげちょっと簡単にするやつ
#!/usr/bin/env ruby
require 'thor'
class NginxConf < Thor
desc 'register PORT', 'install new config file'
option :prefix, type: :string, default: "/usr/local"
def register(port)
root = Dir::pwd
project = root.split(/\//).last
server = "#{project}.local"
file = <<-EOT
upstream #{project}-app {
server unix:#{root}/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen #{port};
client_max_body_size 2G;
server_name #{server};
keepalive_timeout 5;
root #{root}/public;
access_log off;
error_log off;
if ($request_method !~ ^(GET|HEAD|PUT|POST|DELETE|OPTIONS)$ ){
return 405;
}
location / {
try_files $uri/index.html $uri.html $uri @#{project}-app;
error_page 404 /404.html;
error_page 422 /422.html;
error_page 500 502 503 504 /500.html;
error_page 403 /403.html;
}
location @#{project}-app {
proxy_pass http://#{project}-app;
}
location = /favicon.ico {
expires max;
add_header Cache-Control public;
}
}
EOT
conf_dir = "#{options.prefix}/etc/nginx/sites-available"
conf_dir_enabled = "#{options.prefix}/etc/nginx/sites-enabled"
conf = "#{conf_dir}/#{server}"
begin
unless File.directory? conf_dir
raise "not a directory: #{conf_dir}"
end
unless File.directory? conf_dir_enabled
raise "not a directory: #{conf_dir_enabled}"
end
if File.exists? conf
raise "already exists: #{conf}"
end
`echo '#{file}' | sudo tee #{conf}`
`sudo ln -s #{conf} #{conf_dir_enabled}/#{server}`
rescue
$stderr.puts "Error: #{$!.message}"
end
end
end
NginxConf.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment