Class: ReverseProxy::Config::Nginx
- Inherits:
-
Generic
- Object
- Generic
- ReverseProxy::Config::Nginx
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
Instance Method Details
#assets ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/reverse_proxy/config/nginx.rb', line 123
def assets
return unless proxy.application.send_assets?
<<-ASSETS.strip_heredoc
location ~ ^#{proxy.application.assets}/ {
root #{Rails.root.join("public")};
gzip_static on;
expires max;
add_header Cache-Control public;
access_log /dev/null;
}
ASSETS
end
|
#charset_types ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/reverse_proxy/config/nginx.rb', line 72
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
|
#chroot ⇒ Object
Also known as:
dir
141
142
143
144
145
|
# File 'lib/reverse_proxy/config/nginx.rb', line 141
def chroot
prepare :dir do
Pathname.new "/tmp/#{proxy.id}"
end
end
|
#clean ⇒ Object
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
|
#content ⇒ Object
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
|
# File 'lib/reverse_proxy/config/nginx.rb', line 32
def content
ERB.new(<<-CONTENT.strip_heredoc).result(binding).gsub(/\n/,'').gsub(/\s{2,}/, ' ')
daemon off;
worker_processes 2;
worker_rlimit_nofile 8192;
pid <%= pid.relative_path_from chroot %>;
error_log error.log;
events {
worker_connections 2048;
}
http {
<%= types %>
<%= upstream %>
default_type <%= default_type %>;
charset_types <%= charset_types %>;
keepalive_timeout 5;
access_log access.log;
tcp_nopush on;
sendfile on;
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types <%= gzip_types %>;
server {
<%= listeners %>
<%= assets %>
<%= root %>
<%= redirect %>
}
}
CONTENT
end
|
#default_type ⇒ Object
69
70
71
|
# File 'lib/reverse_proxy/config/nginx.rb', line 69
def default_type
"application/octet-stream"
end
|
#file ⇒ Object
138
139
140
|
# File 'lib/reverse_proxy/config/nginx.rb', line 138
def file
chroot.join("nginx.cfg")
end
|
#gzip_types ⇒ Object
87
88
89
90
91
|
# File 'lib/reverse_proxy/config/nginx.rb', line 87
def gzip_types
proxy.application.mime_types.keys.select do |type|
type.match(/((font)|(^text)|(css)|(script)|(bmp$)|(ml$))/)
end.reject{|x|x=="text/html"}.join(" ")
end
|
#listeners ⇒ Object
97
98
99
100
101
|
# File 'lib/reverse_proxy/config/nginx.rb', line 97
def listeners
proxy.binds.collect do |bind|
"listen #{bind.tcp.to_nginx};\n"
end.join
end
|
#pid ⇒ Object
135
136
137
|
# File 'lib/reverse_proxy/config/nginx.rb', line 135
def pid
chroot.join("nginx.pid")
end
|
#redirect ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/reverse_proxy/config/nginx.rb', line 107
def redirect
<<-REDIRECT.strip_heredoc
location @#{proxy.id} {
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://#{upstream_label};
}
REDIRECT
end
|
#root ⇒ Object
102
103
104
105
106
|
# File 'lib/reverse_proxy/config/nginx.rb', line 102
def root
"location / {
try_files $uri $uri/index.html $uri.html @#{proxy.id};
}"
end
|
#types ⇒ Object
92
93
94
95
96
|
# File 'lib/reverse_proxy/config/nginx.rb', line 92
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
21
22
23
|
# File 'lib/reverse_proxy/config/nginx.rb', line 21
def updated?
Digest::MD5.hexdigest(content) != @hash
end
|
#upstream ⇒ Object
66
67
68
|
# File 'lib/reverse_proxy/config/nginx.rb', line 66
def upstream
"upstream #{upstream_label} { server #{proxy.server.socket.unix.to_upstream} fail_timeout=0; }"
end
|
#upstream_label ⇒ Object
84
85
86
|
# File 'lib/reverse_proxy/config/nginx.rb', line 84
def upstream_label
"application_#{proxy.id}"
end
|
#write ⇒ Object
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
28
29
30
|
# File 'lib/reverse_proxy/config/nginx.rb', line 28
def written?
File.exists? file
end
|