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, package_info_hash, #path_to_boot_script, #path_to_boot_script_defaults, #path_to_gemdir, #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
# 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}#{app_config[:ssl_key] ? ' ssl' : ''};
#{(app_config[:ssl_key] && app_config[:non_ssl_port] != 0) ? "listen #{app_config[:non_ssl_port]};" : "" }
charset utf-8;
#{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;
error_page 404 /404.html;
error_page 500 502 503 504 /500.html;
try_files $uri @rails;

location ~ ^/assets/ {
  try_files $uri @rails;
  gzip_static on;
  expires max;
  add_header Cache-Control public;

  open_file_cache max=1000 inactive=500s;
  open_file_cache_valid 600s;
  open_file_cache_errors on;
}

location @rails {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  proxy_connect_timeout 2;
  proxy_read_timeout 86400;
  proxy_pass http://#{app_name}_#{instance_name};
}
}
NGINX_CONFIG
  end
end

#control_all(action) ⇒ Object



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

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



94
95
96
97
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 94

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



72
73
74
75
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 72

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)



78
79
80
81
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 78

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



83
84
85
86
87
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 83

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



89
90
91
92
# File 'lib/rails_pwnerer/app/nginx_config.rb', line 89

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