nginx_config_generator_kthompson 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +18 -0
- data/Manifest +8 -0
- data/README +20 -0
- data/Rakefile +20 -0
- data/VERSION +1 -0
- data/bin/generate_nginx_config +2 -0
- data/lib/config.yml.example +80 -0
- data/lib/nginx.erb +216 -0
- data/lib/nginx_config_generator.rb +35 -0
- metadata +77 -0
data/LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2007 Chris Wanstrath
|
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,20 @@
|
|
1
|
+
A work in progress.
|
2
|
+
|
3
|
+
Check it:
|
4
|
+
http://errtheblog.com/post/3908
|
5
|
+
|
6
|
+
Use it:
|
7
|
+
$ generate_nginx_config nginx_config.yml /etc/nginx.conf
|
8
|
+
|
9
|
+
See an example config file:
|
10
|
+
$ generate_nginx_config --example
|
11
|
+
|
12
|
+
You can set two environment variables:
|
13
|
+
- NGINX_CONFIG_YAML to specify the YAML config file
|
14
|
+
- NGINX_CONFIG_FILE to specify the nginx config file
|
15
|
+
|
16
|
+
By default, generate_nginx_config won't overwrite your OUT file. To rock this behavior,
|
17
|
+
pass in --overwrite or -o or -y or --force or -f. Whichever.
|
18
|
+
|
19
|
+
>> Chris Wanstrath
|
20
|
+
=> chris[at]ozmm[dot]org
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gemspec|
|
7
|
+
gemspec.name = "nginx_config_generator_kthompson"
|
8
|
+
gemspec.summary = "Creates Nginx config files from YAML options"
|
9
|
+
gemspec.description = "You got Nginx in my YAML! You got YAML in my Nginx!"
|
10
|
+
gemspec.email = "mrunleaded@gmail.com"
|
11
|
+
gemspec.homepage = "http://github.com/kthompson/nginx_config_generator"
|
12
|
+
gemspec.authors = ["Kevin Thompson", "Chris Wanstrath"]
|
13
|
+
gemspec.executables = ["generate_nginx_config"]
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
18
|
+
end
|
19
|
+
|
20
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# This defines the default roots and alternate roots.
|
2
|
+
# The name (or key) of your site will be sprintf'd with
|
3
|
+
# the below root, right.
|
4
|
+
root:
|
5
|
+
rails: /home/chris/sites/%s/current/public
|
6
|
+
default: /home/chris/sites/%s
|
7
|
+
|
8
|
+
# Some more additional config variables
|
9
|
+
user: deploy
|
10
|
+
group: deploy
|
11
|
+
|
12
|
+
pid_path: /var/run/
|
13
|
+
config_path: /etc/nginx/
|
14
|
+
log_path: /var/log/nginx_
|
15
|
+
|
16
|
+
# All your vhosts.
|
17
|
+
sites:
|
18
|
+
# We're naming this vhost 'errtheblog'
|
19
|
+
errtheblog:
|
20
|
+
# Two upstream servers to proxy balance fairly.
|
21
|
+
upstream:
|
22
|
+
- 127.0.0.1:8000
|
23
|
+
- 127.0.0.1:8001
|
24
|
+
fair: true
|
25
|
+
# Just a string of server names.
|
26
|
+
server_name: errtheblog.com www.errtheblog.com
|
27
|
+
# www.errtheblog.com => errtheblog.com
|
28
|
+
no_www: true
|
29
|
+
# Just strings that get spit out as rewrites
|
30
|
+
# which won't rewrite for feedburner itself
|
31
|
+
feeds:
|
32
|
+
- ^/feed/feedburner http://feeds.feedburner.com/errtheblog
|
33
|
+
- ^/feed/atom.xml http://feeds.feedburner.com/errtheblog
|
34
|
+
# Which root to use. You can add a custom one here, too.
|
35
|
+
root: rails
|
36
|
+
|
37
|
+
cheat:
|
38
|
+
upstream: 127.0.0.1:8020
|
39
|
+
server_name: cheat.errtheblog.com
|
40
|
+
|
41
|
+
subtlety:
|
42
|
+
upstream: 127.0.0.1:8021
|
43
|
+
server_name: subtlety.errtheblog.com
|
44
|
+
|
45
|
+
sfruby:
|
46
|
+
upstream: 127.0.0.1:8030
|
47
|
+
server_name: sfruby.org www.sfruby.org
|
48
|
+
no_www: true
|
49
|
+
root: rails
|
50
|
+
|
51
|
+
trac:
|
52
|
+
upstream: 127.0.0.1:9000
|
53
|
+
server_name: require.errtheblog.com
|
54
|
+
|
55
|
+
qa:
|
56
|
+
server_name: qa.famupdate.com
|
57
|
+
auth_file: /home/builder/conf/htpasswd
|
58
|
+
root: /home/builder/site
|
59
|
+
|
60
|
+
ssl_sites:
|
61
|
+
errtheblog:
|
62
|
+
# ssl configuration
|
63
|
+
ssl:
|
64
|
+
key: /path/to/err.key
|
65
|
+
cert: /path/to/err.crt
|
66
|
+
# SSL only allows one vhost per IP this can be solved if you have multiple IPs.
|
67
|
+
listen: 10.10.10.1:443
|
68
|
+
# Two upstream servers to proxy balance.
|
69
|
+
upstream:
|
70
|
+
- 127.0.0.1:8000
|
71
|
+
- 127.0.0.1:8001
|
72
|
+
no_www: true
|
73
|
+
server_name: errtheblog.com
|
74
|
+
# Just strings that get spit out as rewrites
|
75
|
+
rewrites:
|
76
|
+
- ^/feed/feedburner http://feeds.feedburner.com/errtheblog
|
77
|
+
- ^/feed/atom.xml http://feeds.feedburner.com/errtheblog
|
78
|
+
# Which root to use. You can add a custom one here, too.
|
79
|
+
root: rails
|
80
|
+
|
data/lib/nginx.erb
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
## http://brainspl.at/nginx.conf.txt
|
2
|
+
|
3
|
+
#user and group to run as
|
4
|
+
user <%= config['user'] || "nginx" %> <%= config['group'] || "nginx" %>;
|
5
|
+
|
6
|
+
# number of nginx workers
|
7
|
+
worker_processes 2;
|
8
|
+
|
9
|
+
# pid of nginx master process
|
10
|
+
pid <%= config['pid_path'] || 'logs/' %>nginx.pid;
|
11
|
+
|
12
|
+
server_names_hash_bucket_size 128;
|
13
|
+
|
14
|
+
# Number of worker connections. 1024 is a good default
|
15
|
+
events {
|
16
|
+
worker_connections 1024;
|
17
|
+
}
|
18
|
+
|
19
|
+
# start the http module where we config http access.
|
20
|
+
http {
|
21
|
+
# pull in mime-types. You can break out your config
|
22
|
+
# into as many include's as you want to make it cleaner
|
23
|
+
include <%= config['config_path'] || 'conf/' %>mime.types;
|
24
|
+
|
25
|
+
<% if config['passenger_root'] && config['passenger_ruby'] %>
|
26
|
+
passenger_root <%= config['passenger_root'] %>;
|
27
|
+
passenger_ruby <%= config['passenger_ruby'] %>;
|
28
|
+
<% end %>
|
29
|
+
|
30
|
+
# set a default type for the rare situation that
|
31
|
+
# nothing matches from the mimie-type include
|
32
|
+
default_type application/octet-stream;
|
33
|
+
|
34
|
+
# configure log format
|
35
|
+
log_format main '$remote_addr - $remote_user [$time_local] $status '
|
36
|
+
'"$request" $body_bytes_sent "$http_referer" '
|
37
|
+
'"$http_user_agent" "http_x_forwarded_for"';
|
38
|
+
|
39
|
+
# main access log
|
40
|
+
access_log <%= config['log_path'] || 'logs/' %>access.log main;
|
41
|
+
|
42
|
+
# main error log
|
43
|
+
error_log <%= config['log_path'] || 'logs/' %>error.log main;
|
44
|
+
#error_log logs/error.log debug_http;
|
45
|
+
|
46
|
+
# no sendfile on OSX
|
47
|
+
sendfile on;
|
48
|
+
|
49
|
+
# These are good default values.
|
50
|
+
tcp_nopush on;
|
51
|
+
tcp_nodelay off;
|
52
|
+
# output compression saves bandwidth
|
53
|
+
gzip on;
|
54
|
+
gzip_http_version 1.0;
|
55
|
+
gzip_comp_level 2;
|
56
|
+
gzip_proxied any;
|
57
|
+
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml
|
58
|
+
application/xml+rss text/javascript;
|
59
|
+
|
60
|
+
# this is where you define your mongrel clusters.
|
61
|
+
# you need one of these blocks for each cluster
|
62
|
+
# and each one needs its own name to refer to it later.
|
63
|
+
<% Array(config['sites']).each do |name, site| %>
|
64
|
+
<% next unless site['upstream'] %>
|
65
|
+
upstream <%= name %> {
|
66
|
+
<% if site['fair'] %>
|
67
|
+
fair;
|
68
|
+
<% end %>
|
69
|
+
<% Array(site['upstream'] || site['upstreams']).each do |server| %>
|
70
|
+
server <%= server %>;
|
71
|
+
<% end %>
|
72
|
+
}
|
73
|
+
<% end %>
|
74
|
+
|
75
|
+
<% %w(sites ssl_sites).each do |site_type| %>
|
76
|
+
|
77
|
+
<% Array(config[site_type]).each do |name, site| %>
|
78
|
+
# the server directive is nginx's virtual host directive.
|
79
|
+
server {
|
80
|
+
|
81
|
+
<% if site['ssl'] %>
|
82
|
+
|
83
|
+
<% if site['listen'] %>
|
84
|
+
listen <%= site['listen'] %>;
|
85
|
+
<% else %>
|
86
|
+
listen 443;
|
87
|
+
<% end %>
|
88
|
+
ssl on;
|
89
|
+
|
90
|
+
# path to your certificate
|
91
|
+
ssl_certificate <%= site['ssl']['cert'] %>;
|
92
|
+
|
93
|
+
# path to your ssl key
|
94
|
+
ssl_certificate_key <%= site['ssl']['key'] %>;
|
95
|
+
|
96
|
+
<% else %>
|
97
|
+
|
98
|
+
# port to listen on. Can also be set to an IP:PORT
|
99
|
+
listen 80;
|
100
|
+
|
101
|
+
# sets the domain[s] that this vhost server requests for
|
102
|
+
server_name <%= site['server_name'] %>;
|
103
|
+
|
104
|
+
<% end %>
|
105
|
+
|
106
|
+
# doc root
|
107
|
+
root <%= (config['root'][ site['root'] || 'default' ] || site['root']) % name %>;
|
108
|
+
<% if site['passenger'] %>
|
109
|
+
passenger_enabled on;
|
110
|
+
rails_env <%= site['passenger']['environment'] || 'development' %>;
|
111
|
+
<% end %>
|
112
|
+
# vhost specific access log
|
113
|
+
access_log logs/<%= name %>.access.log main;
|
114
|
+
|
115
|
+
#Set the max size for file uploads to 50Mb
|
116
|
+
client_max_body_size 50M;
|
117
|
+
|
118
|
+
# this rewrites all the requests to the maintenance.html
|
119
|
+
# page if it exists in the doc root. This is for capistrano's
|
120
|
+
# disable web task
|
121
|
+
if (-f $document_root/system/maintenance.html) {
|
122
|
+
rewrite ^(.*)$ /system/maintenance.html last;
|
123
|
+
break;
|
124
|
+
}
|
125
|
+
|
126
|
+
<% if site['no_www'] %>
|
127
|
+
if ($host ~* "www") {
|
128
|
+
rewrite ^(.*)$ http://<%= site['server_name'].split.first.sub(/^\./, '') %>$1 permanent;
|
129
|
+
break;
|
130
|
+
}
|
131
|
+
<% end %>
|
132
|
+
|
133
|
+
<% if site['always_www'] %>
|
134
|
+
if ($host != "www.<%= site['server_name'].split.first %>") {
|
135
|
+
rewrite ^(.*)$ http://www.<%= site['server_name'].split.first %>$1 permanent;
|
136
|
+
break;
|
137
|
+
}
|
138
|
+
<% end %>
|
139
|
+
|
140
|
+
location / {
|
141
|
+
<% Array(site['rewrite'] || site['rewrites']).each do |rewrite| %>
|
142
|
+
rewrite <%= rewrite %> break;
|
143
|
+
<% end %>
|
144
|
+
|
145
|
+
<% Array(site['feed'] || site['feeds']).each do |feed_rewrite| %>
|
146
|
+
if ($http_user_agent !~ FeedBurner) {
|
147
|
+
rewrite <%= feed_rewrite %> break;
|
148
|
+
}
|
149
|
+
<% end %>
|
150
|
+
|
151
|
+
<% if site['auth_file'] %>
|
152
|
+
auth_basic "<%= name %> requires credentials.";
|
153
|
+
auth_basic_user_file <%= site['auth_file'] %>;
|
154
|
+
<% end %>
|
155
|
+
|
156
|
+
# needed to forward user's IP address to rails
|
157
|
+
proxy_set_header X-Real-IP $remote_addr;
|
158
|
+
|
159
|
+
# needed for HTTPS
|
160
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
161
|
+
proxy_set_header Host $http_host;
|
162
|
+
proxy_redirect false;
|
163
|
+
proxy_max_temp_file_size 0;
|
164
|
+
<% if site['ssl'] %>
|
165
|
+
# set X-FORWARDED_PROTO so ssl_requirement plugin works
|
166
|
+
proxy_set_header X-FORWARDED_PROTO https;
|
167
|
+
<% end %>
|
168
|
+
|
169
|
+
# If the file exists as a static file serve it directly without
|
170
|
+
# running all the other rewite tests on it
|
171
|
+
if (-f $request_filename) {
|
172
|
+
break;
|
173
|
+
}
|
174
|
+
|
175
|
+
# check for index.html for directory index
|
176
|
+
# if its there on the filesystem then rewite
|
177
|
+
# the url to add /index.html to the end of it
|
178
|
+
# and then break to send it to the next config rules.
|
179
|
+
if (-f $request_filename/index.html) {
|
180
|
+
rewrite (.*) $1/index.html break;
|
181
|
+
}
|
182
|
+
|
183
|
+
# this is the meat of the rails page caching config
|
184
|
+
# it adds .html to the end of the url and then checks
|
185
|
+
# the filesystem for that file. If it exists, then we
|
186
|
+
# rewite the url to have explicit .html on the end
|
187
|
+
# and then send it on its way to the next config rule.
|
188
|
+
# if there is no file on the fs then it sets all the
|
189
|
+
# necessary headers and proxies to our upstream mongrels
|
190
|
+
if (-f $request_filename.html) {
|
191
|
+
rewrite (.*) $1.html break;
|
192
|
+
}
|
193
|
+
|
194
|
+
<% if site['upstream'] %>
|
195
|
+
if (!-f $request_filename) {
|
196
|
+
proxy_pass http://<%= name %>;
|
197
|
+
break;
|
198
|
+
}
|
199
|
+
<% end %>
|
200
|
+
}
|
201
|
+
|
202
|
+
error_page 500 502 503 504 /50x.html;
|
203
|
+
location = /50x.html {
|
204
|
+
root html;
|
205
|
+
}
|
206
|
+
|
207
|
+
<% Array(site['location'] || site['locations']).each do |path, setting| %>
|
208
|
+
location = <%= path %> {
|
209
|
+
<%= setting %>
|
210
|
+
}
|
211
|
+
<% end %>
|
212
|
+
|
213
|
+
}
|
214
|
+
<% end %>
|
215
|
+
<% end %>
|
216
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
%w(erb yaml).each &method(:require)
|
3
|
+
|
4
|
+
def error(message) puts(message) || exit end
|
5
|
+
def file(file) "#{File.dirname(__FILE__)}/#{file}" end
|
6
|
+
|
7
|
+
if ARGV.include? '--example'
|
8
|
+
example = file:'config.yml.example'
|
9
|
+
error open(example).read
|
10
|
+
end
|
11
|
+
|
12
|
+
env_in = ENV['NGINX_CONFIG_YAML']
|
13
|
+
env_out = ENV['NGINX_CONFIG_FILE']
|
14
|
+
|
15
|
+
error "Usage: generate_nginx_config [config file] [out file]" if ARGV.empty? && !env_in
|
16
|
+
|
17
|
+
overwrite = %w(-y -o -f --force --overwrite).any? { |f| ARGV.delete(f) }
|
18
|
+
|
19
|
+
config = YAML.load(ERB.new(File.read(env_in || ARGV.shift || 'config.yml')).result)
|
20
|
+
template = if custom_template_index = (ARGV.index('--template') || ARGV.index('-t'))
|
21
|
+
custom = ARGV[custom_template_index+1]
|
22
|
+
error "=> Specified template file #{custom} does not exist." unless File.exist?(custom)
|
23
|
+
ARGV.delete_at(custom_template_index) # delete the --argument
|
24
|
+
ARGV.delete_at(custom_template_index) # and its value
|
25
|
+
custom
|
26
|
+
else
|
27
|
+
file:'nginx.erb'
|
28
|
+
end
|
29
|
+
|
30
|
+
if File.exists?(out_file = env_out || ARGV.shift || 'nginx.conf') && !overwrite
|
31
|
+
error "=> #{out_file} already exists, won't overwrite it. Quitting."
|
32
|
+
else
|
33
|
+
open(out_file, 'w+').write(ERB.new(File.read(template), nil, '>').result(binding))
|
34
|
+
error "=> Wrote #{out_file} successfully."
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nginx_config_generator_kthompson
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kevin Thompson
|
14
|
+
- Chris Wanstrath
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-08-08 00:00:00 -07:00
|
20
|
+
default_executable: generate_nginx_config
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: You got Nginx in my YAML! You got YAML in my Nginx!
|
24
|
+
email: mrunleaded@gmail.com
|
25
|
+
executables:
|
26
|
+
- generate_nginx_config
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files:
|
30
|
+
- LICENSE
|
31
|
+
- README
|
32
|
+
files:
|
33
|
+
- LICENSE
|
34
|
+
- Manifest
|
35
|
+
- README
|
36
|
+
- Rakefile
|
37
|
+
- VERSION
|
38
|
+
- bin/generate_nginx_config
|
39
|
+
- lib/config.yml.example
|
40
|
+
- lib/nginx.erb
|
41
|
+
- lib/nginx_config_generator.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/kthompson/nginx_config_generator
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.3.7
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Creates Nginx config files from YAML options
|
76
|
+
test_files: []
|
77
|
+
|