-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathservers.conf.erb
172 lines (143 loc) · 5.08 KB
/
servers.conf.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
log_format keyvalue
'method=$request_method'
' path="$request_uri"'
' host=$host'
' request_id=$http_x_request_id'
' from="$remote_addr"'
' protocol=$scheme'
' status=$status'
' duration=${request_time}s'
' bytes=$bytes_sent'
' referer="$http_referer"'
' user_agent="$http_user_agent"'<%#
To allow dynamic logging format for nginx,
create a json that contains the key/value pairs
you want to add to nginx logging.
For the logs to be correctly parsed, use the nginx_logger_version parameter.
For example:
ADDITIONAL_NGINX_LOGS='{"nginx_logger_version":"1", "my_custom_header":"$http_my_custom_header"}'
%><%
require 'json';
JSON.parse(ENV['ADDITIONAL_NGINX_LOGS']||'{}').each do |nginx_key,value| %>
' <%= nginx_key %>="<%= value %>"'<% end %>;
# In order to avoid logging access twice per request
# it is necessary to turn off the top-level (e.g. http) buildpack default access_log
# as we are about to override it in the server directive here below
access_log off;
<%
canary_enabled = ENV['NGINX_UPSTREAM_CANARY'] == 'enabled' && ENV['NGINX_UPSTREAM_CANARY_WEIGHT'] && ENV['NGINX_UPSTREAM_CANARY_APP_NAME']
canary_weight = ENV['NGINX_UPSTREAM_CANARY_WEIGHT'].to_i
server_options = "max_fails=#{ENV['NGINX_UPSTREAM_MAX_FAILS'] || 3} fail_timeout=#{ENV['NGINX_UPSTREAM_FAIL_TIMEOUT'] || '5s'}"
%>
upstream api {
<%
# We compute the API host from the front app name, examples:
# pix-orga-integration -> pix-api-integration.scalingo.io
# pix-orga-integration-pr123 -> pix-api-integration-pr123.scalingo.io
# pix-orga-production -> pix-api-production.scalingo.io
%>
server <%= ENV['APP'].gsub(/^pix-[^-]+-/, "pix-api-") %>.<%= ENV['API_HOST_SUFFIX'] || 'scalingo.io' %>:443 <%= server_options %>;
}
<% if canary_enabled %>
upstream api-canary {
server <%= ENV['NGINX_UPSTREAM_CANARY_APP_NAME'] %>.<%= ENV['API_HOST_SUFFIX'] || 'scalingo.io' %>:443 <%= server_options %>;
}
<% end %>
split_clients "${request_id}" $upstream {
<% if canary_enabled %>
<%= canary_weight %>% api-canary;
<% end %>
* api;
}
split_clients "${request_id}" $upstream_host {
<% if canary_enabled %>
<%= canary_weight %>% <%= ENV['NGINX_UPSTREAM_CANARY_APP_NAME'] %>.<%= ENV['API_HOST_SUFFIX'] || 'scalingo.io' %>;
<% end %>
* <%= ENV['APP'].gsub(/^pix-[^-]+-/, "pix-api-") %>.<%= ENV['API_HOST_SUFFIX'] || 'scalingo.io' %>;
}
#add a catch all on http port to forward to the convenient https
server {
listen 80 default_server;
server_name _;
add_header Strict-Transport-Security "max-age=31536001; includeSubDomains; preload";
return 301 https://$host$request_uri;
}
server {
access_log logs/access.log keyvalue;
server_name localhost;
listen <%= ENV['PORT'] %>;
charset utf-8;
# Disable compression that is performed by the Scalingo router anyway
gzip off;
root /app/dist/;
location = /maintenance_page.html {
# maintenance page is at the root of the project
root /app/;
}
location = /maintenance_scheduled.html {
# maintenance page is at the root of the project
root /app/;
}
<% if ENV['MAINTENANCE_PLANIFIEE'] == 'enabled' %>
# in case of 503, serve this URI
error_page 503 /maintenance_scheduled.html;
location / {
return 503;
}
<% elsif ENV['MAINTENANCE'] == 'enabled'%>
# in case of 503, serve this URI
error_page 503 /maintenance_page.html;
location / {
return 503;
}
<% else %>
# in case of 503, serve this URI
error_page 503 /maintenance_page.html;
location = /index.html {
# index.html should never be cached
expires -1;
}
location /assets/ {
# Files in /assets/ are hash-suffixed, so it's safe to cache them indefinitely
expires max;
}
location /@1024pix/pix-ui/fonts/ {
# Design System fonts are not hash-suffixed but can be cached for a long time
expires 1y;
add_header Cache-Control "public";
add_header ETag "";
}
location /images/ {
expires 24h;
}
location / {
# Fall back to index.html for routes that don't match an existing file
try_files $uri /index.html;
# Let clients cache these files for a bit
expires 24h;
}
location /api/ {
proxy_pass https://$upstream;
proxy_set_header Host $upstream_host;
proxy_set_header X-Source-Front <%= ENV['APP'] %>;
# Set X-Forwarded-xxx headers for the API to compute getForwardedOrigin
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
# Set X-Forwarded-For header to be able to know the remote client IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
<% end %>
add_header X-Content-Type-Options "nosniff";
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection 1;
add_header Strict-Transport-Security "max-age=31536001; includeSubDomains; preload";
<% ENV.each do |key,value|
if key.start_with? 'ADD_HTTP_HEADER' %>
add_header <%=
key.sub(/^ADD_HTTP_HEADER_/, '').split("_").map(&:capitalize).join("-")
%> "<%=
value.gsub('\\', '\\\\\\\\').gsub('"','\\"').gsub('$','\\$')
%>" ;
<% end
end %>
}