-
Notifications
You must be signed in to change notification settings - Fork 24
/
nginx_unicorn_web_app.erb
191 lines (160 loc) · 5.12 KB
/
nginx_unicorn_web_app.erb
1
2
3
4
5
6
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
upstream unicorn_<%= @application[:domains].first %> {
server unix:<%= @application[:deploy_to]%>/shared/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name <%= @application[:domains].join(" ") %> <%= node[:hostname] %>;
access_log <%= node[:nginx][:log_dir] %>/<%= @application[:domains].first %>.access.log;
keepalive_timeout 5;
root <%= @application[:absolute_document_root] %>;
<% if @application[:nginx] && @application[:nginx][:client_max_body_size] %>
client_max_body_size <%= @application[:nginx][:client_max_body_size] %>;
<% end %>
# Custom application settings
include <%= @application[:deploy_to] %>/current/config/nginx/*.conf;
if ($request_uri ~ " ") {
return 444;
}
<% if @application[:nginx] && @application[:nginx][:proxy_passes] %>
<% @application[:nginx][:proxy_passes].each do |proxy| %>
location <%= proxy[:location] %> {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass <%= proxy[:upstream] %>;
}
<% end %>
<% end %>
location ~* \.(eot|ttf|woff|ico)$ {
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Access-Control-Allow-Origin *;
}
location ~* ^/(assets|static)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Access-Control-Allow-Origin *;
}
location ~* ^/images/.*\.(jpg|jpeg|png|gif|ico|svg)$ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
<% if @application[:force_ssl] %>
set $https_forward "https://$host$uri";
# Don't disclose private IP addresses when no host specified (HTTP/1.0)
if ($host ~ "\d+\.\d+\.\d+\.\d+") {
set $https_forward "https://<%= @application[:domains].first %>$uri";
}
# Rewrite everything to HTTPS. Except for the health_check.
if ($http_x_forwarded_proto != 'https') {
rewrite ^((?!/health_check).) $https_forward permanent;
}
<% end %>
if (-f $document_root/offline) {
return 503;
}
# If you don't find the filename in the static files
# Then request it from the unicorn server
if (!-f $request_filename) {
proxy_pass http://unicorn_<%= @application[:domains].first %>;
break;
}
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
error_page 500 502 504 /500.html;
location = /500.html {
root <%= @application[:absolute_document_root] %>;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
}
<% if @application[:ssl_support] %>
server {
listen 443;
server_name <%= @application[:domains].join(" ") %> <%= node[:hostname] %>;
access_log <%= node[:nginx][:log_dir] %>/<%= @application[:domains].first %>-ssl.access.log;
ssl on;
ssl_certificate /etc/nginx/ssl/<%= @application[:domains].first %>.crt;
ssl_certificate_key /etc/nginx/ssl/<%= @application[:domains].first %>.key;
<% if @application[:ssl_certificate_ca] -%>
ssl_client_certificate /etc/nginx/ssl/<%= @application[:domains].first %>.ca;
<% end -%>
keepalive_timeout 5;
root <%= @application[:absolute_document_root] %>;
<% if @application[:nginx] && @application[:nginx][:client_max_body_size] %>
client_max_body_size <%= @application[:nginx][:client_max_body_size] %>;
<% end %>
if ($request_uri ~ " ") {
return 444;
}
location ~* ^/(assets|static)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location / {
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (-f $document_root/offline) {
return 503;
}
# If you don't find the filename in the static files
# Then request it from the unicorn server
if (!-f $request_filename) {
proxy_pass http://unicorn_<%= @application[:domains].first %>;
break;
}
}
error_page 500 502 504 /500.html;
location = /500.html {
root <%= @application[:absolute_document_root] %>;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
}
<% end %>
<% if @application[:force_www] %>
<% @application[:domains].each do |domain| %>
<% if domain.start_with?('www.') %>
server {
<% domain.slice!(/\Awww\./) %>
server_name <%= domain %>;
<% if @application[:force_ssl] %>
return 301 https://www.<%= domain %>$request_uri;
<% else %>
return 301 $scheme://www.<%= domain %>$request_uri;
<% end %>
}
<% end %>
<% end %>
<% end %>
<% if @application[:strip_www] %>
server {
<% domain = @application[:domains].first %>
server_name www.<%= domain %>;
return 301 $scheme://<%= domain %>$request_uri;
}
<% end %>
<% if @application[:redirect_domains] %>
server {
<% domain = @application[:domains].first %>
server_name <%= @application[:redirect_domains].join " " %>;
return 301 $scheme://<%= domain %>$request_uri;
}
<% end %>