Created
November 17, 2009 04:16
-
-
Save stephencelis/236635 to your computer and use it in GitHub Desktop.
A first take on http://freelancing-gods.com/posts/script_nginx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "optparse" | |
require "erb" | |
module Script | |
class Nginx | |
TEMP_PATH = "tmp/nginx.conf" | |
def initialize | |
@host = "0.0.0.0" | |
@port = "8080" | |
parse_nginx_options | |
parse_rails_options | |
load_temp_config unless @config | |
end | |
def run | |
puts "=> Booting nginx" | |
`nginx -p . -c #@config` | |
puts "=> nginx proxying to Rails on http://#@host:#@port" | |
at_exit do | |
`nginx -p . -c #@config -s stop` | |
File.delete @config if @config == TEMP_PATH | |
end | |
load "script/server" | |
end | |
private | |
def parse_nginx_options | |
ARGV.options do |opts| | |
opts.banner = "Usage: #$0 [options] -- [script/server options]" | |
opts.on("-P", "--nginx-port=port", | |
"Runs nginx on the specified port.", "Default: #@port", | |
String) { |port| @port = port } | |
opts.on("-B", "--nginx-binding=ip", | |
"Binds nginx to the specified ip.", "Default: #@host", | |
String) { |host| @host = host } | |
opts.on("-C", "--nginx-config=file", | |
"Use custom nginx configuration file", | |
String) { |config| @config = config } | |
opts.on("-h", "--help", | |
"Show this help message.") { puts opts; exit } | |
begin | |
opts.parse! | |
rescue OptionParser::ParseError => e | |
warn e | |
puts opts | |
exit 1 | |
end | |
end | |
end | |
def parse_rails_options | |
@app_host = @host | |
@app_port = "3000" | |
ARGV.clone.options do |opts| | |
opts.on("-p", "--port=port") { |port| @app_port = port } | |
opts.on("-b", "--binding=ip") { |host| @app_host = host } | |
opts.parse! | |
end | |
end | |
def load_temp_config | |
File.open TEMP_PATH, "w" do |file| | |
file.write ERB.new(DATA.read).result(binding) | |
@config = file.path | |
end | |
end | |
end | |
end | |
Script::Nginx.new.run | |
__END__ | |
pid tmp/pids/nginx.pid; | |
error_log log/nginx.error.log; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
access_log log/nginx.access.log; | |
error_log log/nginx.error.log; | |
client_body_temp_path tmp/nginx.client_body_temp; | |
fastcgi_temp_path tmp/nginx.fastcgi_temp; | |
proxy_temp_path tmp/nginx.proxy_temp; | |
gzip on; | |
upstream proxy { | |
server <%= @app_host %>:<%= @app_port %>; | |
} | |
server { | |
listen <%= @port %>; | |
server_name <%= @host %>; | |
location / { | |
root public; | |
index index.html; | |
if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png)\?[0-9]+$") { | |
expires max; | |
break; | |
} | |
if (-f $request_filename) { | |
break; | |
} | |
if (!-f $request_filename) { | |
proxy_pass http://proxy; | |
break; | |
} | |
error_page 404 = /404.html; | |
error_page 422 = /422.html; | |
error_page 500 502 502 504 = /500.html; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment