class RailsPwnerer::App::NginxConfig

builds the nginx configuration

Public Instance Methods

config_nginx(app_name, instance_name) click to toggle source

writes the nginx configuration for this server

   # File lib/rails_pwnerer/app/nginx_config.rb
 7   def config_nginx(app_name, instance_name)
 8     app_config = RailsPwnerer::Config[app_name, instance_name]
 9     first_port = app_config[:port0]
10 
11     # Can be specified as comma-separated string or array.
12     if app_config[:dns_name].respond_to? :to_str
13       dns_names = app_config[:dns_name].split(',')
14     else
15       dns_names = app_config[:dns_name]
16     end
17 
18     default_app_port = app_config[:ssl_key] ? 443 : 80
19     app_port = app_config[:port] || default_app_port
20 
21     nginx_config = File.join(RailsPwnerer::Config.path_to(:nginx_configs),
22                              app_name + '.' + instance_name)
23     File.open(nginx_config, 'w') do |f|
24       # link to the frontends
25       f << "upstream #{app_name}_#{instance_name} {\n"
26       RailsPwnerer::Config.app_frontends(app_name, instance_name).times do |instance|
27         f << "  server 127.0.0.1:#{first_port + instance};\n"
28       end
29       f << "}\n\n"
30 
31       # server configuration -- big and ugly
32       f << <<NGINX_CONFIG
33 server {
34   listen #{app_port}#{app_config[:ssl_key] ? ' ssl' : ''};
35   #{(app_config[:ssl_key] && app_config[:non_ssl_port] != 0) ? "listen #{app_config[:non_ssl_port]};" : "" }
36   charset utf-8;
37   #{app_config[:ssl_key] ? "ssl_certificate #{app_config[:ssl_cert]};" : ''}
38   #{app_config[:ssl_key] ? "ssl_certificate_key #{app_config[:ssl_key]};" : ''}
39   #{(dns_names.empty? ? '' : "server_name " + dns_names.join(' ') + ";")}
40   root #{app_config[:app_path]}/public;
41   client_max_body_size #{app_config[:max_request_mb]}M;
42   error_page 404 /404.html;
43   error_page 500 502 503 504 /500.html;
44   try_files $uri @rails;
45 
46   location ~ ^/assets/ {
47     try_files $uri @rails;
48     gzip_static on;
49     expires max;
50     add_header Cache-Control public;
51 
52     open_file_cache max=1000 inactive=500s;
53     open_file_cache_valid 600s;
54     open_file_cache_errors on;
55   }
56 
57   location @rails {
58     proxy_set_header X-Real-IP $remote_addr;
59     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
60     proxy_set_header X-Forwarded-Proto $scheme;
61     proxy_set_header Host $http_host;
62     proxy_redirect off;
63     proxy_connect_timeout 2;
64     proxy_read_timeout 86400;
65     proxy_pass http://#{app_name}_#{instance_name};
66   }
67 }
68 NGINX_CONFIG
69     end
70   end
control_all(action) click to toggle source
    # File lib/rails_pwnerer/app/nginx_config.rb
 99 def control_all(action)
100   case action
101   when :start
102     control_boot_script('nginx', :reload)
103     control_boot_script('nginx', :start)
104   when :stop
105     control_boot_script('nginx', :stop)
106   when :reload
107     control_boot_script('nginx', :reload)
108   end
109 end
remove(app_name, instance_name) click to toggle source
   # File lib/rails_pwnerer/app/nginx_config.rb
94 def remove(app_name, instance_name)
95   remove_nginx_config app_name, instance_name
96   control_boot_script('nginx', :reload)
97 end
remove_nginx_config(app_name, instance_name) click to toggle source
   # File lib/rails_pwnerer/app/nginx_config.rb
72 def remove_nginx_config(app_name, instance_name)
73   nginx_config = File.join(RailsPwnerer::Config.path_to(:nginx_configs), app_name + '.' + instance_name)
74   File.delete nginx_config if File.exists? nginx_config
75 end
remove_nginx_stub() click to toggle source

removes the default configuration stub (so nginx doesn’t stumble upon it)

   # File lib/rails_pwnerer/app/nginx_config.rb
78 def remove_nginx_stub
79   stub_file = File.join(RailsPwnerer::Config.path_to(:nginx_configs), 'default')
80   File.delete stub_file  if File.exists?(stub_file)
81 end
setup(app_name, instance_name) click to toggle source
   # File lib/rails_pwnerer/app/nginx_config.rb
83 def setup(app_name, instance_name)
84   config_nginx app_name, instance_name
85   remove_nginx_stub
86   control_boot_script('nginx', :reload)
87 end
update(app_name, instance_name) click to toggle source
   # File lib/rails_pwnerer/app/nginx_config.rb
89 def update(app_name, instance_name)
90   config_nginx app_name, instance_name
91   control_boot_script('nginx', :reload)
92 end