Class: RailsPwnerer::App::NginxConfig

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/rails_pwnerer/app/nginx_config.rb

Overview

builds the nginx configuration

Instance Method Summary collapse

Methods included from Base

_setup_unix, _setup_windows, all_packages, all_packages_without_caching, #atomic_erase, #atomic_read, #atomic_write, #best_package_matching, #check_rails_root, #control_boot_script, #cpu_cores, #current_user, #gem_exists?, #gid_for_username, #group_for_username, #hook_boot_script, #install_gem, #install_gems, #install_package, #install_package_impl, #install_package_matching, #install_packages, #kill_tree, #os_distro, #path_to_boot_script, #path_to_boot_script_defaults, #path_to_gem, #process_info, #prompt_user_for_password, #remove_package, #remove_packages, #search_packages, #uid_for_username, #unroll_collection, #update_all_packages, #update_all_packages_impl, #update_gems, #update_package_metadata, #upgrade_gem, #upgrade_gems, #upgrade_package, #upgrade_package_impl, #upgrade_packages, #with_package_source, #with_temp_dir

Instance Method Details

#config_nginx(app_name, instance_name) ⇒ Object

writes the nginx configuration for this server



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 7

def config_nginx(app_name, instance_name)
  app_config = RailsPwnerer::Config[app_name, instance_name]
  first_port = app_config[:port0]
  
  # Can be specified as comma-separated string or array.
  if app_config[:dns_name].respond_to? :to_str
    dns_names = app_config[:dns_name].split(',')
  else
    dns_names = app_config[:dns_name]
  end
  
  default_app_port = app_config[:ssl_key] ? 443 : 80
  app_port = app_config[:port] || default_app_port
  
  nginx_config = File.join(RailsPwnerer::Config.path_to(:nginx_configs),
                           app_name + '.' + instance_name) 
  File.open(nginx_config, 'w') do |f|
    # link to the frontends
    f << "  upstream #{app_name}_#{instance_name} {\n"
    RailsPwnerer::Config.app_frontends(app_name, instance_name).times do |instance|
      f << "    server 127.0.0.1:#{first_port + instance};\n"
    end
    f << "  }\n\n"
    
    # server configuration -- big and ugly
    f << <<NGINX_CONFIG
server {
  listen #{app_port};
  charset utf-8;
  #{app_config[:ssl_key] ? 'ssl on;' : ''}
  #{app_config[:ssl_key] ? "ssl_certificate #{app_config[:ssl_cert]};" : ''}
  #{app_config[:ssl_key] ? "ssl_certificate_key #{app_config[:ssl_key]};" : ''}
  #{(dns_names.empty? ? '' : "server_name " + dns_names.join(' ') + ";")}
  root #{app_config[:app_path]}/public;
  client_max_body_size #{app_config[:max_request_mb]}M;
  location / {
    if (-f $request_filename) {
      expires max;
      break;
    }

    if (-f $request_filename/index.html) {
      expires max;
      rewrite (.*) $1/index.html break;
    }
    if (-f $request_filename.html) {
      expires max;
      rewrite (.*) $1.html break;
    }

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #{app_config[:ssl_key] ? 'proxy_set_header X-Forwarded-Proto https;' : ''}
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_connect_timeout 2;
    proxy_read_timeout 86400;
    if (!-f $request_filename) {
      proxy_pass http://#{app_name}_#{instance_name};
      break;
    }
  }
}      
NGINX_CONFIG
  end    
end

#control_all(action) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 101

def control_all(action)
  case action
  when :start
    control_boot_script('nginx', :reload)
    control_boot_script('nginx', :start)
  when :stop
    control_boot_script('nginx', :stop)
  when :reload
    control_boot_script('nginx', :reload)
  end
end

#remove(app_name, instance_name) ⇒ Object



96
97
98
99
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 96

def remove(app_name, instance_name)
  remove_nginx_config app_name, instance_name
  control_boot_script('nginx', :reload)
end

#remove_nginx_config(app_name, instance_name) ⇒ Object



74
75
76
77
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 74

def remove_nginx_config(app_name, instance_name)
  nginx_config = File.join(RailsPwnerer::Config.path_to(:nginx_configs), app_name + '.' + instance_name) 
  File.delete nginx_config if File.exists? nginx_config    
end

#remove_nginx_stubObject

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



80
81
82
83
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 80

def remove_nginx_stub
  stub_file = File.join(RailsPwnerer::Config.path_to(:nginx_configs), 'default')
  File.delete stub_file  if File.exists?(stub_file)
end

#setup(app_name, instance_name) ⇒ Object



85
86
87
88
89
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 85

def setup(app_name, instance_name)
  config_nginx app_name, instance_name
  remove_nginx_stub
  control_boot_script('nginx', :reload)
end

#update(app_name, instance_name) ⇒ Object



91
92
93
94
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 91

def update(app_name, instance_name)
  config_nginx app_name, instance_name
  control_boot_script('nginx', :reload)
end