Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Updated the nginx cookbook to allow for multiple app servers and medi…
Browse files Browse the repository at this point in the history
…a servers per project.

Also updated the example configs to take advantage of this.
  • Loading branch information
d0ugal committed Sep 19, 2011
1 parent d57c5b3 commit 52c55ef
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 54 deletions.
6 changes: 1 addition & 5 deletions cookbooks/nginx/files/default/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ http {

access_log /var/log/nginx/access.log;

sendfile on;
#tcp_nopush on;

keepalive_timeout 5;
tcp_nodelay on;

gzip on;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml
application/xml+rss text/javascript;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

include /etc/nginx/sites-enabled/*;

Expand Down
40 changes: 22 additions & 18 deletions cookbooks/nginx/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,31 @@
notifies :restart, resources(:service => "nginx")
end

if node.has_key?("domain_names") and node.has_key?("project_name")
if node.has_key?("servers")

directory "/var/log/nginx/#{node[:project_name]}" do
owner "root"
group "root"
mode 0775
recursive true
end
node.servers.each do |name, server_info|

template "/etc/nginx/sites-available/#{node[:project_name]}" do
source "site.erb"
mode 0640
owner "root"
group "root"
notifies :restart, resources(:service => "nginx")
end
directory "/var/log/nginx/#{name}" do
owner "root"
group "root"
mode 0775
recursive true
end

template "/etc/nginx/sites-available/#{node[:project_name]}" do
source "site.erb"
mode 0640
owner "root"
group "root"
notifies :restart, resources(:service => "nginx")
end

execute "nginx-symlink" do
command "sudo ln -s /etc/nginx/sites-available/#{node[:project_name]} /etc/nginx/sites-enabled/#{node[:project_name]}"
not_if "sudo ls /etc/nginx/sites-enabled/#{node[:project_name]}"
notifies :restart, resources(:service => "nginx")
end

execute "nginx-symlink" do
command "sudo ln -s /etc/nginx/sites-available/#{node[:project_name]} /etc/nginx/sites-enabled/#{node[:project_name]}"
not_if "sudo ls /etc/nginx/sites-enabled/#{node[:project_name]}"
notifies :restart, resources(:service => "nginx")
end

end
56 changes: 32 additions & 24 deletions cookbooks/nginx/templates/default/site.erb
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
upstream app_server_<%= node[:project_name] %> {
server unix:/tmp/<%= node[:project_name] %>.gunicorn.sock fail_timeout=0;
#server 127.0.0.1:8001 fail_timeout=0;
# For a TCP configuration:
# server 192.168.0.7:8000 fail_timeout=0;
}

server {
listen 80;
client_max_body_size 4G;
server_name <%= node[:domain_names] %>;
#server_name <%= node[:domain_names].each do |d| print "#{d} " end %>;
<% node[:servers].each do |name, server_info| %>
<% if server_info[:app_servers] %>
<% server_info[:app_servers].each do |app_server| %>
upstream app_server_<%= name %>_<%= app_server[:name] %> {
<% if app_server[:socket_path] %>
server unix:<%= app_server[:socket_path] %> fail_timeout=0;
<% else %>
server unix:/tmp/<%= app_server[:name] %>.gunicorn.sock fail_timeout=0;
<% end %>
}
<% end %>
<% end %>
<% end %>
access_log /var/log/nginx/<%= node[:project_name] %>/access.log;
error_log /var/log/nginx/<%= node[:project_name] %>/error.log;
sendfile on;
<% node[:servers].each do |name, server_info| %>
server {

keepalive_timeout 5;
server_name <%= server_info[:domains] %>;

# path for static files
root /var/www/<%= node[:project_name] %>;
access_log /var/log/nginx/<%= name %>/access.log;
error_log /var/log/nginx/<%= name %>/error.log;

location / {
<% if server_info[:app_servers] %>
<% server_info[:app_servers].each do |app_server| %>
location <%= app_server[:location] %> {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

if (!-f $request_filename) {
proxy_pass http://app_server_<%= node[:project_name] %>;
proxy_pass http://app_server_<%= name %>_<%= app_server[:name] %>;
break;
}
}
<% end %>
<% end %>
error_page 500 502 503 504 /500.html;
location = /500.html {
root /path/to/app/current/public;
<% if server_info[:static_dirs] %>
<% server_info[:static_dirs].each do |static_dir| %>
location <%= static_dir[:location] %> {
root <%= static_dir[:path] %>;
}
}
<% end %>
<% end %>

}
<% end %>
3 changes: 2 additions & 1 deletion site_configs/local_example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"run_list": [ "main", "python", "postgres"],
"run_list": [ "main", "python", "postgres", "nginx"],
"project_name": "local",
"development_environment": true,
"postgres_password": "postgres"
}
35 changes: 33 additions & 2 deletions site_configs/prod_example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"run_list": [ "main", "python", "postgres"],
"run_list": [ "main", "python", "postgres", "nginx"],

"project_name": "Bliki",

"user": {
"username": "pug_user",
Expand All @@ -25,6 +27,35 @@
"password": "wiki_password",
"gis": true
}
}
},

"servers" : {
"bliki" : {
"domains" : "bliki.com *.bliki.com",
"app_servers": [
{
"name": "blog",
"location": "/"
},
{
"name": "wiki",
"location": "/wiki",
"socket_path": "/tmp/my_socket.gunicorn.sock"
}
]
},
"bliki_media" : {
"domains" : "media.bliki.com",
"static_dirs": [
{
"location": "/media",
"path": "/sites/media"
},
{
"location": "/static",
"path": "/ites/static"
}
]
}
}
}
32 changes: 30 additions & 2 deletions site_configs/prod_example2.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"run_list": [ "main", "python", "postgres"],
"run_list": [ "main", "python", "postgres", "nginx"],

"project_name": "enterprise",

"user": {
"username": "enterprise_user",
Expand Down Expand Up @@ -39,6 +41,32 @@
"password": "password",
"gis": true
}
}
},

"servers" : {
"enterprise_app1" : {
"domains" : "ncc1701.com *.ncc1701.com",
"app_servers": [
{
"name": "blog",
"location": "/"
},
{
"name": "wiki",
"location": "/wiki",
"socket_path": "/tmp/my_socket.gunicorn.sock"
}
],
"static_dirs": [
{
"location": "/media",
"path": "/sites/media"
},
{
"location": "/static",
"path": "/ites/static"
}
]
}
}
}
6 changes: 4 additions & 2 deletions vagrant_files/python_postgres
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ Vagrant::Config.run do |config|
chef.add_recipe "main"
chef.add_recipe "python"
chef.add_recipe "postgres"
chef.add_recipe "nginx"

chef.json.merge!({
:project_name => "test_project",
:development_environment => true
:development_environment => true,
:project_name => "local",
:postgres_password => "postgres"
})

end
Expand Down

0 comments on commit 52c55ef

Please sign in to comment.