class NginxUtils::VirtualHost

Attributes

access_log_format[RW]
auth_basic[RW]
auth_basic_user_file[RW]
destination[RW]
error_log_level[RW]
http[RW]
https[RW]
index[RW]
log_dir[RW]
prefix[RW]
root[RW]
server_name[RW]
ssl_certificate[RW]
ssl_certificate_key[RW]
vhost_type[RW]

Public Class Methods

new(options={}) click to toggle source
# File lib/nginx_utils/virtual_host.rb, line 7
def initialize(options={})
  options = options.inject({}){|r,(k,v)| r.store(k.to_sym, v); r}
  set_vhost_type(options)
  set_common_params(options)
  set_protocols(options)
  set_log_params(options)
end

Public Instance Methods

config() click to toggle source
# File lib/nginx_utils/virtual_host.rb, line 70
def config
  content = open(File.expand_path("../../../template/virtual_host.erb", __FILE__)).read
  ERB.new(content).result(binding).gsub(/^\s+$/, "").gsub(/\n+/, "\n")
end
set_common_params(options) click to toggle source
# File lib/nginx_utils/virtual_host.rb, line 39
def set_common_params(options)
  # Arguments: prefix, server_name, root, index, auth_basic, auth_basic_user_file in options
  @prefix = options.fetch(:prefix, "/usr/local/nginx")
  @server_name = options.fetch(:server_name, "example.com")
  @root = options.fetch(:root, File.join(@prefix, "vhosts", @server_name, "html"))
  @index = options.fetch(:index, ["index.html", "index.htm"].join(" "))
  @auth_basic = options[:auth_basic]
  if @auth_basic
    @auth_basic_user_file = options.fetch(:auth_basic_user_file, File.join(@prefix, "vhosts", @server_name, "etc", "users"))
  end
end
set_log_params(options) click to toggle source
# File lib/nginx_utils/virtual_host.rb, line 63
def set_log_params(options)
  # Arguments: log_dir, access_log_format, error_log_level in options
  @log_dir = options.fetch(:log_dir, File.join(@prefix, "vhosts", @server_name, "logs"))
  @access_log_format = options.fetch(:access_log_format, :ltsv)
  @error_log_level = options.fetch(:error_log_level, :info)
end
set_protocols(options) click to toggle source
# File lib/nginx_utils/virtual_host.rb, line 51
def set_protocols(options)
  # Arguments: http, https, ssl_certificate, ssl_certificate_key in options
  @http = options[:http].nil? ? true : options[:http]
  @https = options[:https].nil? ? true : options[:https]
  @http = false if options[:only_https]
  @https = false if options[:only_http]
  if @https
    @ssl_certificate = options.fetch(:ssl_certificate, File.join(@prefix, "vhosts", @server_name, "ssl.crt", "server.crt"))
    @ssl_certificate_key = options.fetch(:ssl_certificate_key, File.join(@prefix, "vhosts", @server_name, "ssl.key", "server.key"))
  end
end
set_vhost_type(options) click to toggle source
# File lib/nginx_utils/virtual_host.rb, line 15
def set_vhost_type(options)
  # Arguments: vhost_type, destination in options
  if options[:vhost_type].nil?
    @vhost_type = :normal
  else
    case options[:vhost_type].to_sym
    when :unicorn then
      @vhost_type = :unicorn
      @destination = options.fetch(:destination, "127.0.0.1:8080")
    when :proxy then
      @vhost_type = :proxy
      @destination = options.fetch(:destination, "127.0.0.1:8080")
    when :passenger then
      @vhost_type = :passenger
    else
      @vhost_type = :normal
    end
  end

  if @destination =~ /\.sock$/ && @destination !~ /^unix:/
    @destination = "unix:#{@destination}"
  end
end