mattly-nginx_config_generator 0.5.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 ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2007 Chris Wanstrath, Matthew Lyon
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
@@ -0,0 +1,8 @@
1
+ LICENSE
2
+ Manifest
3
+ README
4
+ Rakefile
5
+ bin/generate_nginx_config
6
+ lib/config.yml.example
7
+ lib/nginx.erb
8
+ lib/nginx_config_generator.rb
data/README ADDED
@@ -0,0 +1,25 @@
1
+ A work in progress.
2
+
3
+ The original version:
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
+ original by:
20
+ >> Chris Wanstrath
21
+ => chris[at]ozmm[dot]org
22
+
23
+ new template and configuration options by:
24
+ >> Matthew Lyon
25
+ => matt[at]flowerpowered[dot]com
data/Rakefile ADDED
File without changes
@@ -0,0 +1,2 @@
1
+ #! /usr/bin/env ruby
2
+ load File.dirname(__FILE__) + '/../lib/nginx_config_generator.rb'
@@ -0,0 +1,103 @@
1
+ # These set various options for nginx. more to come.
2
+ user: deploy
3
+ group: deploy
4
+ pid_path: /var/run
5
+ log_path: /var/log/nginx
6
+ workers: 6
7
+
8
+ # This defines the default roots and alternate roots.
9
+ # The name (or key) of your site will be sprintf'd with the below root.
10
+ root:
11
+ rails: /var/apps/%s/current/public
12
+ default: /var/www/%s/current/web
13
+
14
+ # All your vhosts.
15
+ sites:
16
+ # serves static html from /var/www/placeholders/current/web
17
+ placeholders:
18
+ # "host" or "hosts": should be obvious
19
+ # by default will generate 'www' hosts for each entry
20
+ # add 'www: no' to override this behaviour
21
+ hosts: example.com example.org another-example.com example.net yet-another-example.com
22
+
23
+ # serves static html from /var/www/static_site/current/web
24
+ static_site:
25
+ hosts: static.com static.net static.org
26
+
27
+ # values for the "alias" key:
28
+ # 'all': redirects all hosts after the first to the first host
29
+ # 'none': create no redirects for any hostname
30
+ # 'www': redirect any domain not starting with 'www' to www.domain.etc
31
+ # default: redirect 'www' hostnames to the non-'www' version
32
+ # in this example %w(www.static.com static.net www.static.net static.org www.static.org) will each redirect to static.com
33
+ alias: all
34
+
35
+ # Specify that files of a certain type should only be served if the referer
36
+ # is "none", "blocked" (some proxies set this), or among your hostnames
37
+ # specified in "hosts" (including any auto-generated 'www' hosts)
38
+ # Just a list of file extensions, 'images' auto-expands to 'jpg jpeg gif png'
39
+ prevent_hotlinking: images swf
40
+
41
+ # redirect, redirects, 301s:
42
+ # hash of regex patterns to match and rewrite rules to go with.
43
+ # will return a '301 Permanent' redirect.
44
+ 301s:
45
+ '^/old_url.*': '/new_url'
46
+ '/old_categories/(.*)': '/$1'
47
+ '^/info/.*': 'info'
48
+ '^/info.html': 'info'
49
+
50
+ # specify the location of the 404 page.
51
+ # This will not be set if this value is not included.
52
+ 404_not_found: /404.html
53
+
54
+ rails_site:
55
+ hosts: proxy.com proxied.com
56
+
57
+ # two upstream servers to proxy to
58
+ # you can do single upstream servers with "upstream: 127.0.0.1:8000"
59
+ upstream:
60
+ - 127.0.0.1:8000
61
+ - 127.0.0.1:8001
62
+
63
+ # like before, aliases everything to 'proxy.com'
64
+ alias: all
65
+
66
+ # serve out of the 'rails' root
67
+ root: rails
68
+
69
+ # normal rewrites, specified as a hash.
70
+ rewrites:
71
+ '^foo': bar
72
+
73
+ # redirects all except for feedburner to feeds.feedburner.com/foo
74
+ feedburner:
75
+ '^/articles.atom': my_rails_site
76
+
77
+ # location of the error page for 500-class errors
78
+ 50x_error_page: /50x.html
79
+
80
+ # other options:
81
+ # specifies the path to an htpasswd-style authorization file
82
+ # auth_file: /path/to/an/htpasswd
83
+ # setting this will not generate 'www' hostnames for your hosts
84
+ # www: no
85
+
86
+ # You may also specify https hosts
87
+ ssl_sites:
88
+ secure_rails_site:
89
+ # ssl configuration
90
+ ssl:
91
+ key: /path/to/ssl.key
92
+ cert: /path/to/ssl.crt
93
+ # SSL only allows one vhost per IP this can be solved if you have multiple IPs.
94
+ listen: 10.10.10.1:443
95
+ # This proxies to our same upstream servers as the 'rails_site' entry above.
96
+ upstream:
97
+ - 127.0.0.1:8000
98
+ - 127.0.0.1:8001
99
+ alias: all
100
+ host: proxy.com
101
+ root: rails
102
+ www: no
103
+
data/lib/nginx.erb ADDED
@@ -0,0 +1,225 @@
1
+ user <%= config['user'] || 'nginx' %> <%= config['group'] || 'nginx' %>;
2
+
3
+ worker_processes <%= config['workers'] || 2 %>;
4
+
5
+ pid <%= config['pid_path'] || 'logs' %>/nginx.pid;
6
+
7
+
8
+ events {
9
+ worker_connections 1024;
10
+ }
11
+
12
+
13
+ http {
14
+ include mime.types;
15
+ default_type application/octet-stream;
16
+
17
+ log_format main '$remote_addr - $remote_user [$time_local] $request '
18
+ '"$status" $body_bytes_sent "$http_referer" '
19
+ '"$http_user_agent" "$http_x_forwarded_for"';
20
+
21
+ access_log <%= config['log_path'] || 'logs' %>/access.log main;
22
+ error_log <%= config['log_path'] || 'logs' %>/error.log debug;
23
+
24
+ # no sendfile on OS X
25
+ sendfile on;
26
+
27
+ # These are good default values.
28
+ tcp_nopush on;
29
+ tcp_nodelay off;
30
+
31
+ # output compression saves bandwidth
32
+ gzip on;
33
+ gzip_http_version 1.0;
34
+ gzip_comp_level 2;
35
+ gzip_proxied any;
36
+ gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
37
+
38
+ server_names_hash_bucket_size 128;
39
+
40
+ # upstream mongrels
41
+ <% Array(config['sites']).each do |name, site| %>
42
+ <% next unless site['upstream'] %>
43
+ upstream <%= name %> {
44
+ <% Array(site['upstream'] || site['upstreams']).each do |server| %>
45
+ server <%= server %>;
46
+ <% end %>
47
+ }
48
+ <% end %>
49
+
50
+ # Sites
51
+ <% %w(sites ssl_sites).each do |site_type| %>
52
+
53
+ <% Array(config[site_type]).each do |name, site| %>
54
+ # the server directive is nginx's virtual host directive.
55
+ server {
56
+
57
+ <% if site['ssl'] %>
58
+ listen <%= site['listen'] || '443' %>
59
+ ssl on;
60
+
61
+ # path to your certificate
62
+ ssl_certificate <%= site['ssl']['cert'] %>;
63
+
64
+ # path to your ssl key
65
+ ssl_certificate_key <%= site['ssl']['key'] %>;
66
+ <% else %>
67
+ # port to listen on. Can also be set to an IP:PORT
68
+ listen 80;
69
+ <% end %>
70
+
71
+ # sets the domain[s] that this vhost server requests for
72
+ <% unless site['www'] == 'no'
73
+ hosts = (site['host'] || site['hosts']).split
74
+ server_names = hosts.inject([]) do |memo, host|
75
+ memo << host
76
+ memo << "www.#{host}" unless host.match(/\Awww\./) or hosts.include?("www.#{host}")
77
+ memo
78
+ end
79
+ else
80
+ server_names = hosts.split
81
+ end
82
+ %>
83
+ server_name <%= server_names.join(' ') %>;
84
+
85
+ # doc root
86
+ root <%= (config['root'][ site['root'] || 'default' ] || site['root']) % name %>;
87
+
88
+ # vhost specific access log
89
+ access_log <%= config['log_path'] || 'logs' %>/<%= name %>.access.log main;
90
+
91
+ #Set the max size for file uploads to 50Mb
92
+ client_max_body_size 50M;
93
+
94
+ # this rewrites all the requests to the maintenance.html
95
+ # page if it exists in the doc root. This is for capistrano's
96
+ # disable web task
97
+ if (-f $document_root/maintenance.html){
98
+ rewrite ^(.*)$ /maintenance.html last;
99
+ break;
100
+ }
101
+
102
+ # Site Aliases
103
+ <% if site['alias'] == 'all' %>
104
+ if ($host != "<%= site['host'].split.first %>") {
105
+ rewrite ^(.*)$ http://<%= site['host'].split.first %>$1 permanent;
106
+ break;
107
+ }
108
+ <% elsif site['alias'] == 'none' %>
109
+ # no redirects for this domain
110
+ <% elsif site['alias'] == 'www' %>
111
+ if ($host !~* "^www") {
112
+ rewrite ^(.*)$ http://www.<%= site['host'].split.first.sub(/^\./, '') %>$1 permanent;
113
+ break;
114
+ }
115
+ <% else # redirect www only %>
116
+ if ($host ~* "^www") {
117
+ rewrite ^(.*)$ http://<%= site['host'].split.first.sub(/^\./, '') %>$1 permanent;
118
+ break;
119
+ }
120
+ <% end %>
121
+
122
+ location / {
123
+ <% Array(site['rewrite'] || site['rewrites']).each do |rewrite, rule| %>
124
+ rewrite <%= rewrite %> <%= rule %> break;
125
+ <% end %>
126
+
127
+ # Redirects
128
+ <% Array(site['redirect'] || site['redirects'] || site['301s']).each do |regex, destination| %>
129
+ rewrite <%= regex %> /<%= destination %> permanent;
130
+ <% end %>
131
+
132
+ <% Array(site['feedburner']).each do |feed, burner| %>
133
+ if ($http_user_agent !~ FeedBurner) {
134
+ rewrite <%= feed %> http://feeds.feedburner.com/<%= burner %> break;
135
+ }
136
+ <% end %>
137
+
138
+ <% if site['auth_file'] %>
139
+ auth_basic "<%= name %> requires credentials.";
140
+ auth_basic_user_file <%= site['auth_file'] %>;
141
+ <% end %>
142
+
143
+ # needed to forward user's IP address to rails
144
+ proxy_set_header X-Real-IP $remote_addr;
145
+
146
+ # needed for HTTPS
147
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
148
+ proxy_set_header Host $http_host;
149
+ proxy_redirect false;
150
+ proxy_max_temp_file_size 0;
151
+ <% if site['ssl'] %>
152
+ # set X-FORWARDED_PROTO so ssl_requirement plugin works
153
+ proxy_set_header X-FORWARDED_PROTO https;
154
+ <% end %>
155
+
156
+ # If the file exists as a static file serve it directly without
157
+ # running all the other rewite tests on it
158
+ if (-f $request_filename) {
159
+ break;
160
+ }
161
+
162
+ # check for index.html for directory index
163
+ # if its there on the filesystem then rewite
164
+ # the url to add /index.html to the end of it
165
+ # and then break to send it to the next config rules.
166
+ if (-f $request_filename/index.html) {
167
+ rewrite (.*) $1/index.html break;
168
+ }
169
+
170
+ # this is the meat of the rails page caching config
171
+ # it adds .html to the end of the url and then checks
172
+ # the filesystem for that file. If it exists, then we
173
+ # rewite the url to have explicit .html on the end
174
+ # and then send it on its way to the next config rule.
175
+ # if there is no file on the fs then it sets all the
176
+ # necessary headers and proxies to our upstream mongrels
177
+ if (-f $request_filename.html) {
178
+ rewrite (.*) $1.html break;
179
+ }
180
+
181
+ <% if site['upstream'] %>
182
+ if (!-f $request_filename) {
183
+ proxy_pass http://<%= name %>;
184
+ break;
185
+ }
186
+ <% end %>
187
+ }
188
+
189
+ <% if site['404_not_found'] %>
190
+ error_page 404 <%= site['404_not_found'] %>;
191
+ <% end %>
192
+
193
+ error_page 500 502 503 504 <%= site['50x_error_page'] || '/50x.html' %>;
194
+ location = /50x.html {
195
+ root html;
196
+ }
197
+
198
+ <% if site['prevent_hotlinking'] %>
199
+ valid_referers none blocked <%= server_names.join(' ') %>;
200
+ <% site['prevent_hotlinking'].split.each do |type| %>
201
+ <% exts = case type
202
+ when 'images'
203
+ %w(jpg jpeg gif png)
204
+ else
205
+ type
206
+ end
207
+ %>
208
+ location ~* \.(<%= exts.join('|') %>)$ {
209
+ if ($invalid_referer) {
210
+ return 403;
211
+ }
212
+ }
213
+ <% end %>
214
+ <% end %>
215
+
216
+ <% Array(site['location'] || site['locations']).each do |path, setting| %>
217
+ location <%= path %> {
218
+ <%= setting %>
219
+ }
220
+ <% end %>
221
+
222
+ }
223
+ <% end # site %>
224
+ <% end # site_type %>
225
+ }
@@ -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,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mattly-nginx_config_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Lyon
8
+ - Chris Wanstrath
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-07-05 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: You got Nginx in my YAML! You got YAML in my Nginx!
18
+ email: matt@flowerpowered.com
19
+ executables:
20
+ - generate_nginx_config
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - LICENSE
27
+ - Manifest
28
+ - Rakefile
29
+ - README
30
+ - bin/generate_nginx_config
31
+ - lib/config.yml.example
32
+ - lib/nginx.erb
33
+ - lib/nginx_config_generator.rb
34
+ has_rdoc: false
35
+ homepage: http://github.com/mattly/nginx_config_generator
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: Creates Nginx config files from YAML options
60
+ test_files: []
61
+