chrislloyd-nginx-config 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +18 -0
- data/Manifest +11 -0
- data/README +15 -0
- data/Rakefile +5 -0
- data/bin/nginx-config +4 -0
- data/lib/nginx_config.rb +115 -0
- data/nginx-config.gemspec +20 -0
- data/templates/dot.nginx-config.erb +11 -0
- data/templates/nginx.conf.erb +86 -0
- data/templates/nginx.init.sh +65 -0
- data/templates/site.yml.erb +12 -0
- metadata +73 -0
data/LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2008 Chris Lloyd
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
7
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
8
|
+
subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
15
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
16
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
data/README
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Easily generate and maintain a complex nginx config file.
|
2
|
+
|
3
|
+
h2. Usage
|
4
|
+
|
5
|
+
# nginx-conf setup
|
6
|
+
|
7
|
+
# nginx-conf add /var/www/myblog
|
8
|
+
|
9
|
+
# /etc/init.d/nginx reload
|
10
|
+
|
11
|
+
Thats it! No generation. You can check in your config file to a SCM. When you make a change just ask nginx to reload the configuration.
|
12
|
+
|
13
|
+
h2. Thanks
|
14
|
+
|
15
|
+
* "Chris Wanstrath":http://ozmm.org (for creating the original nginx_config_generator)
|
data/Rakefile
ADDED
data/bin/nginx-config
ADDED
data/lib/nginx_config.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'thor'
|
3
|
+
require 'ostruct'
|
4
|
+
require 'erb'
|
5
|
+
|
6
|
+
module Nginx
|
7
|
+
|
8
|
+
class Runner < Thor
|
9
|
+
|
10
|
+
desc 'setup', 'setup the system for using nginx-config'
|
11
|
+
method_options :force => :boolean
|
12
|
+
def setup(opts)
|
13
|
+
unless system_config_exists? && !opts['force']
|
14
|
+
# TODO Refactor this to a new method
|
15
|
+
`cp #{Nginx::Template['dot.nginx-config']} ~/.nginx-config`
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'add DIR', 'adds a directory to Nginx'
|
20
|
+
method_options :force => :boolean
|
21
|
+
def add(dir,opts)
|
22
|
+
|
23
|
+
if File.directory?(dir)
|
24
|
+
dir = File.expand_path(dir)
|
25
|
+
|
26
|
+
setup(opts)
|
27
|
+
site_name = File.basename( dir )
|
28
|
+
|
29
|
+
`mkdir #{dir}/config` unless File.directory?(dir+'/config')
|
30
|
+
|
31
|
+
if !File.exists?(dir+'/config/nginx.yml') || opts['force']
|
32
|
+
open(dir + '/config/nginx.yml', 'w+').write(ERB.new(File.read(Nginx::Template['site.yml']), nil, '>').result(binding))
|
33
|
+
|
34
|
+
|
35
|
+
# add site to system wide config.
|
36
|
+
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
map 'gen' => :generate
|
44
|
+
desc 'gen', 'generates the nginx.conf file'
|
45
|
+
def generate(opts)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def system_config_exists?
|
51
|
+
File.exists?(File.expand_path('~/.nginx-config'))
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
class Template
|
57
|
+
def self.[](filename)
|
58
|
+
File.expand_path( File.join(File.dirname(__FILE__), '../templates/'+filename.to_s + '.erb') )
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class Config
|
63
|
+
def self.new(hash)
|
64
|
+
OpenStruct.new(hash.inject({}){|r,p| r[p[0]] = p[1].kind_of?(Hash) ? NestedOstruct.new(p[1]) : p[1]; r })
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
# #! /usr/bin/env ruby
|
71
|
+
# require 'yaml'
|
72
|
+
# require 'erb'
|
73
|
+
#
|
74
|
+
# def defaults
|
75
|
+
#
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# def error(message)
|
79
|
+
# puts(message) || exit
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# def file(file)
|
83
|
+
# "#{File.dirname(__FILE__)}/#{file}"
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# if ARGV.include? '--example'
|
87
|
+
# example = file:'config.yml.example'
|
88
|
+
# error open(example).read
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
# env_in = ENV['NGINX_CONFIG_YAML']
|
92
|
+
# env_out = ENV['NGINX_CONFIG_FILE']
|
93
|
+
#
|
94
|
+
# error "Usage: generate_nginx_config [config file] [out file]" if ARGV.empty? && !env_in
|
95
|
+
#
|
96
|
+
# overwrite = %w(-y -o -f --force --overwrite).any? { |f| ARGV.delete(f) }
|
97
|
+
#
|
98
|
+
# config = YAML.load(ERB.new(File.read(env_in || ARGV.shift || 'config.yml')).result)
|
99
|
+
# template = if custom_template_index = (ARGV.index('--template') || ARGV.index('-t'))
|
100
|
+
# custom = ARGV[custom_template_index+1]
|
101
|
+
# error "=> Specified template file #{custom} does not exist." unless File.exist?(custom)
|
102
|
+
# ARGV.delete_at(custom_template_index) # delete the --argument
|
103
|
+
# ARGV.delete_at(custom_template_index) # and its value
|
104
|
+
# custom
|
105
|
+
# else
|
106
|
+
# file:'nginx.debian.erb'
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
# if File.exists?(out_file = env_out || ARGV.shift || 'nginx.conf') && !overwrite
|
110
|
+
# error "=> #{out_file} already exists, won't overwrite it. Quitting."
|
111
|
+
# else
|
112
|
+
# open(out_file, 'w+').write(ERB.new(File.read(template), nil, '>').result(binding))
|
113
|
+
# error "=> Wrote #{out_file} successfully."
|
114
|
+
# end
|
115
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "nginx-config"
|
3
|
+
s.version = "0.9.1"
|
4
|
+
s.date = "2008-06-02"
|
5
|
+
|
6
|
+
s.homepage = "http://github.com/chrislloyd/nginx_config"
|
7
|
+
|
8
|
+
s.summary = "Easily generate and maintain a complex nginx config file."
|
9
|
+
s.description = "Easily generate and maintain a complex nginx config file."
|
10
|
+
s.authors = ["Chris Lloyd"]
|
11
|
+
s.email = "christopher.lloyd@gmail.com"
|
12
|
+
s.has_rdoc = true
|
13
|
+
|
14
|
+
s.files = ['LICENSE', 'Manifest', 'nginx-config.gemspec', 'Rakefile', 'README', 'bin/nginx-config', 'lib/nginx_config.rb', 'templates/dot.nginx-config.erb', 'templates/nginx.conf.erb', 'templates/nginx.init.sh', 'templates/site.yml.erb']
|
15
|
+
s.rdoc_options = ["--main", "README"]
|
16
|
+
s.extra_rdoc_files = ["Manifest", "README"]
|
17
|
+
s.add_dependency("thor", [">= 0.9.2"])
|
18
|
+
end
|
19
|
+
|
20
|
+
|
@@ -0,0 +1,86 @@
|
|
1
|
+
user <%= config['user'] || "nginx" %> <%= config['group'] || "nginx" %>;
|
2
|
+
|
3
|
+
worker_processes 2;
|
4
|
+
|
5
|
+
pid <%= config['pid_path'] || '/var/run/' %>nginx.pid;
|
6
|
+
|
7
|
+
events {
|
8
|
+
worker_connections 1024;
|
9
|
+
}
|
10
|
+
|
11
|
+
http {
|
12
|
+
include <%= config['config_path'] || '/etc/nginx/' %>mime.types;
|
13
|
+
default_type application/octet-stream;
|
14
|
+
|
15
|
+
log_format main '$remote_addr - $remote_user [$time_local] $status '
|
16
|
+
'"$request" $body_bytes_sent "$http_referer" '
|
17
|
+
'"$http_user_agent" "http_x_forwarded_for"';
|
18
|
+
access_log <%= config['log_path'] || '/var/log/nginx_' %>access.log main;
|
19
|
+
error_log <%= config['log_path'] || '/var/log/nginx_' %>error.log debug;
|
20
|
+
|
21
|
+
sendfile on;
|
22
|
+
tcp_nopush on;
|
23
|
+
tcp_nodelay off;
|
24
|
+
gzip on;
|
25
|
+
gzip_http_version 1.0;
|
26
|
+
gzip_comp_level 2;
|
27
|
+
gzip_proxied any;
|
28
|
+
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
|
29
|
+
|
30
|
+
<% config['sites'].each do |name, site| %>
|
31
|
+
<% next unless(site['type'] == 'rails') %>
|
32
|
+
upstream <%= name %>_mongrels {
|
33
|
+
<% (site['upstream'] || site['upstreams']).each do |server| %>
|
34
|
+
server <%= server %>;
|
35
|
+
<% end %>
|
36
|
+
}
|
37
|
+
<% end %>
|
38
|
+
|
39
|
+
<% config['sites'].sort.each do |name, site| %>
|
40
|
+
server {
|
41
|
+
listen 80;
|
42
|
+
server_name <%= site['domains'] %>;
|
43
|
+
root <%= config['types'][ site['type']||'default' ] % name %>;
|
44
|
+
access_log <%= config['log_path'] || '/var/logs/nginx_' %>access.<%= name %>.log main;
|
45
|
+
client_max_body_size 50M;
|
46
|
+
|
47
|
+
if (-f $document_root/maintenance.html) {
|
48
|
+
rewrite ^(.*)$ /maintenance.html last;
|
49
|
+
break;
|
50
|
+
}
|
51
|
+
|
52
|
+
location / {
|
53
|
+
<% if (site['rewrite'] || site['rewrites']) %>
|
54
|
+
<% (site['rewrite'] || site['rewrites']).each do |rewrite| %>
|
55
|
+
rewrite <%= rewrite %> break;
|
56
|
+
<% end %>
|
57
|
+
<% end %>
|
58
|
+
|
59
|
+
proxy_set_header X-Real-IP $remote_addr;
|
60
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
61
|
+
proxy_set_header Host $http_host;
|
62
|
+
proxy_redirect false;
|
63
|
+
proxy_max_temp_file_size 0;
|
64
|
+
|
65
|
+
if (-f $request_filename/index.html) {
|
66
|
+
rewrite (.*) $1/index.html break;
|
67
|
+
}
|
68
|
+
|
69
|
+
if (-f $request_filename.html) {
|
70
|
+
rewrite (.*) $1.html break;
|
71
|
+
}
|
72
|
+
|
73
|
+
<% if site['type'] == 'rails' %>
|
74
|
+
if (!-f $request_filename) {
|
75
|
+
proxy_pass http://<%= name %>_mongrels;
|
76
|
+
break;
|
77
|
+
}
|
78
|
+
<% end %> }
|
79
|
+
|
80
|
+
error_page 500 502 503 504 /50x.html;
|
81
|
+
location = /50x.html {
|
82
|
+
root html;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
<% end %>
|
86
|
+
}
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#! /bin/sh
|
2
|
+
|
3
|
+
### BEGIN INIT INFO
|
4
|
+
# Provides: nginx
|
5
|
+
# Required-Start: $all
|
6
|
+
# Required-Stop: $all
|
7
|
+
# Default-Start: 2 3 4 5
|
8
|
+
# Default-Stop: 0 1 6
|
9
|
+
# Short-Description: starts the nginx web server
|
10
|
+
# Description: starts nginx using start-stop-daemon
|
11
|
+
### END INIT INFO
|
12
|
+
|
13
|
+
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
14
|
+
DAEMON=/usr/local/sbin/nginx
|
15
|
+
NAME=nginx
|
16
|
+
DESC=nginx
|
17
|
+
|
18
|
+
test -x $DAEMON || exit 0
|
19
|
+
|
20
|
+
# Include nginx defaults if available
|
21
|
+
if [ -f /etc/default/nginx ] ; then
|
22
|
+
. /etc/default/nginx
|
23
|
+
fi
|
24
|
+
|
25
|
+
set -e
|
26
|
+
|
27
|
+
case "$1" in
|
28
|
+
start)
|
29
|
+
echo -n "Starting $DESC: "
|
30
|
+
# TODO Generate config file
|
31
|
+
start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
|
32
|
+
--exec $DAEMON -- $DAEMON_OPTS
|
33
|
+
echo "$NAME."
|
34
|
+
;;
|
35
|
+
stop)
|
36
|
+
echo -n "Stopping $DESC: "
|
37
|
+
start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
|
38
|
+
--exec $DAEMON
|
39
|
+
echo "$NAME."
|
40
|
+
;;
|
41
|
+
restart|force-reload)
|
42
|
+
echo -n "Restarting $DESC: "
|
43
|
+
start-stop-daemon --stop --quiet --pidfile \
|
44
|
+
/usr/local/nginx/logs/nginx.pid --exec $DAEMON
|
45
|
+
sleep 1
|
46
|
+
# TODO Generate config file
|
47
|
+
start-stop-daemon --start --quiet --pidfile \
|
48
|
+
/usr/local/nginx/logs/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
|
49
|
+
echo "$NAME."
|
50
|
+
;;
|
51
|
+
reload)
|
52
|
+
echo -n "Reloading $DESC configuration: "
|
53
|
+
# TODO Generate config file
|
54
|
+
start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
|
55
|
+
--exec $DAEMON
|
56
|
+
echo "$NAME."
|
57
|
+
;;
|
58
|
+
*)
|
59
|
+
N=/etc/init.d/$NAME
|
60
|
+
echo "Usage: $N {start|stop|restart|force-reload}" >&2
|
61
|
+
exit 1
|
62
|
+
;;
|
63
|
+
esac
|
64
|
+
|
65
|
+
exit 0
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chrislloyd-nginx-config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Lloyd
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-02 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thor
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.9.2
|
23
|
+
version:
|
24
|
+
description: Easily generate and maintain a complex nginx config file.
|
25
|
+
email: christopher.lloyd@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- Manifest
|
32
|
+
- README
|
33
|
+
files:
|
34
|
+
- LICENSE
|
35
|
+
- Manifest
|
36
|
+
- nginx-config.gemspec
|
37
|
+
- Rakefile
|
38
|
+
- README
|
39
|
+
- bin/nginx-config
|
40
|
+
- lib/nginx_config.rb
|
41
|
+
- templates/dot.nginx-config.erb
|
42
|
+
- templates/nginx.conf.erb
|
43
|
+
- templates/nginx.init.sh
|
44
|
+
- templates/site.yml.erb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/chrislloyd/nginx_config
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --main
|
50
|
+
- README
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.0.1
|
69
|
+
signing_key:
|
70
|
+
specification_version: 2
|
71
|
+
summary: Easily generate and maintain a complex nginx config file.
|
72
|
+
test_files: []
|
73
|
+
|