Class: ReverseProxy::Config::Nginx

Inherits:
Generic
  • Object
show all
Defined in:
lib/reverse_proxy/config/nginx.rb

Instance Attribute Summary

Attributes inherited from Generic

#proxy

Instance Method Summary collapse

Methods inherited from Generic

#initialize, #prepare

Methods included from Logger

#log

Constructor Details

This class inherits a constructor from ReverseProxy::Config::Generic

Instance Method Details

#assetsObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/reverse_proxy/config/nginx.rb', line 122

def assets
  return unless proxy.application.send_assets?
  "  location ~ ^\#{proxy.application.assets}/  {\n      root \#{Rails.root.join(\"public\")};\n      gzip_static on;\n      expires max;\n      add_header Cache-Control public;\n      access_log /dev/null;\n  }\n  ASSETS\nend\n".strip_heredoc

#charset_typesObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/reverse_proxy/config/nginx.rb', line 71

def charset_types
  %w{
    text/xml
    text/plain
    text/vnd.wap.wml
    application/x-javascript
    application/rss+xml
    text/css
    application/javascript
    application/json
  }.join(" ")
end

#chrootObject Also known as: dir



140
141
142
143
144
# File 'lib/reverse_proxy/config/nginx.rb', line 140

def chroot
  prepare :dir do
    Pathname.new "/tmp/#{proxy.id}"
  end
end

#cleanObject Also known as: delete



10
11
12
13
14
# File 'lib/reverse_proxy/config/nginx.rb', line 10

def clean
  File.unlink(file) rescue nil
  File.unlink(pid)  rescue nil
  FileUtils.rmdir(dir) rescue nil
end

#contentObject



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
# File 'lib/reverse_proxy/config/nginx.rb', line 32

def content
  ERB.new("    daemon off;\n    worker_processes 2;\n    worker_rlimit_nofile 8192;\n    pid <%= pid.relative_path_from chroot %>;\n    error_log error.log;\n    events {\n      worker_connections 2048;\n    }\n    http {\n      <%= types %>\n      <%= upstream %>\n      default_type  <%= default_type %>;\n      charset_types <%= charset_types %>;\n      keepalive_timeout 5;\n      tcp_nopush on;\n      sendfile on;\n      gzip on;\n      gzip_comp_level    5;\n      gzip_min_length    256;\n      gzip_proxied       any;\n      gzip_vary          on;\n      gzip_types <%= gzip_types %>;\n      server {\n        <%= listeners %>\n        <%= assets %>\n        <%= root %>\n        <%= redirect %>\n      }\n    }\n  CONTENT\nend\n".strip_heredoc).result(binding).gsub(/\n/,'').gsub(/\s{2,}/, ' ')

#default_typeObject



68
69
70
# File 'lib/reverse_proxy/config/nginx.rb', line 68

def default_type
  "application/octet-stream"
end

#fileObject



137
138
139
# File 'lib/reverse_proxy/config/nginx.rb', line 137

def file
  chroot.join("nginx.cfg")
end

#gzip_typesObject



86
87
88
89
90
# File 'lib/reverse_proxy/config/nginx.rb', line 86

def gzip_types
  proxy.application.mime_types.keys.select do |type|type
    type.match(/((font)|(^text)|(css)|(script)|(bmp$)|(ml$))/)
  end.reject{|x|x=="text/html"}.join(" ")
end

#listenersObject



96
97
98
99
100
# File 'lib/reverse_proxy/config/nginx.rb', line 96

def listeners
  proxy.binds.collect do |bind|
    "listen #{bind.tcp.to_nginx};\n"
  end.join
end

#pidObject



134
135
136
# File 'lib/reverse_proxy/config/nginx.rb', line 134

def pid
  chroot.join("nginx.pid")
end

#redirectObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/reverse_proxy/config/nginx.rb', line 106

def redirect
  "  location @\#{proxy.id} {\n    proxy_read_timeout    300;\n    proxy_connect_timeout 300;\n    proxy_redirect        off;\n\n    proxy_set_header  X-Forwarded-Proto https;\n    proxy_set_header  X-Forwarded-Ssl   on;\n    proxy_set_header  Host              $http_host;\n    proxy_set_header  X-Real-IP         $remote_addr;\n\n    proxy_pass http://\#{upstream_label};\n  }\n  REDIRECT\nend\n".strip_heredoc

#rootObject



101
102
103
104
105
# File 'lib/reverse_proxy/config/nginx.rb', line 101

def root
  "location / {
    try_files $uri $uri/index.html $uri.html @#{proxy.id};
  }"
end

#typesObject



91
92
93
94
95
# File 'lib/reverse_proxy/config/nginx.rb', line 91

def types
  @types   = proxy.application.mime_types.collect{|type,extensions|"#{type} #{extensions};"}
  @types ||= ["text/plain txt;"]
  "types {" + @types.join + "}"
end

#updated!Object



24
25
26
# File 'lib/reverse_proxy/config/nginx.rb', line 24

def updated!
  @hash = Digest::MD5.hexdigest(content)
end

#updated?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/reverse_proxy/config/nginx.rb', line 21

def updated?
  Digest::MD5.hexdigest(content) != @hash
end

#upstreamObject



65
66
67
# File 'lib/reverse_proxy/config/nginx.rb', line 65

def upstream
  "upstream #{upstream_label} { server #{proxy.server.socket.unix.to_upstream} fail_timeout=0; }"
end

#upstream_labelObject



83
84
85
# File 'lib/reverse_proxy/config/nginx.rb', line 83

def upstream_label
  "application_#{proxy.id}"
end

#writeObject



17
18
19
# File 'lib/reverse_proxy/config/nginx.rb', line 17

def write
  File.write(file, content) and updated! if updated?
end

#written?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/reverse_proxy/config/nginx.rb', line 28

def written?
  File.exists? file
end