/
nginx.conf.erb
169 lines (144 loc) · 4.5 KB
/
nginx.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
## MAIN ADDRESS wwW REMOVAL
server {
listen <%= @site.server.proxy.port %>;
<% if @site.server.ssl %>
listen 443 ssl;
ssl_protocols <%= @site.server.ssl.protocols %>;
<% end %>
server_name www.<%= @site.server_name %>;
rewrite ^ $scheme://<%= @site.server_name %>$request_uri?;
}
## CUSTOM DOMAINS WWW REMOVAL
<% @site.custom_domains.each do |domain| %>
server {
listen <%= @site.server.proxy.port %>;
<% if @site.server.ssl %>
listen 443 ssl;
ssl_protocols <%= @site.server.ssl.protocols %>;
<% end %>
server_name www.<%= domain %>;
rewrite ^ $scheme://<%= domain %>$request_uri?;
}
<% end %>
## REDIRECTS
<% @site.redirects.each do |source, destination| %>
server {
listen <%= @site.server.proxy.port %>;
<% if @site.server.ssl %>
listen 443 ssl;
ssl_protocols <%= @site.server.ssl.protocols %>;
<% end %>
server_name <%=source%> <%= "www.#{source}" unless source.start_with? 'www.' %>;
return 302 $scheme://<%=destination%>$request_uri;
}
<% end %>
upstream <%= @site.service_name %> {
<% if @site.server.unicorn? %>
server unix:<%= @site.run_path %>/unicorn.sock;
<% elsif @site.server.thin? %>
<% @site.server.workers.times.each do |i| %>
server http://127.0.0.1:<%= @site.server.port + i %>;
<% end %>
<% end %>
keepalive 64;
}
<% if @site.server.cache %>
upstream <%= @site.service_name %>_cache {
server <%= @site.server.cache.address %>:<%= @site.server.cache.port %>;
keepalive 64;
}
<% end %>
<% if @site.chat %>
upstream <%= @site.service_name %>_ejabberd {
server 127.0.0.1:<%= @site.chat.port %>;
}
<% end %>
server {
listen <%= @site.server.proxy.port %>;
<% if @site.server.proxy.to_cache %>
listen 127.0.0.1:<%= @site.server.proxy.backend_port %>;
<% end %>
<% if @site.server.ssl %>
listen 443 ssl <%= 'default_server' if @site.server.ssl.default %>;
ssl_protocols <%= @site.server.ssl.protocols %>;
ssl_certificate <%= @site.server.ssl.certificate %>;
ssl_certificate_key <%= @site.server.ssl.certificate_key %>;
<% end %>
server_name <%= @site.server_name %> <%= @site.custom_domains.join ' ' %>;
port_in_redirect off;
root <%= @site.code_path %>/public;
<% if @site.server.ssl and @site.server.ssl.redirect_http %>
if ( $scheme = http ) {
rewrite ^ https://$host$uri last;
}
<% end %>
if (-f $document_root/maintenance.html) {
return 503;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
access_log <%= @site.access_log_path %> combined;
error_log <%= @site.error_log_path %>;
<% if @site.chat %>
location /http-bind {
proxy_buffers 4 256k;
proxy_buffer_size 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://<%= @site.service_name %>_ejabberd;
}
<% end %>
location ~ '/assets/.+-[^\.]{64}\..+$' {
add_header Cache-Control public;
expires 1y;
try_files $uri @proxy;
}
location / {
<% if not @site.server.block_bots.empty? %>
if ($http_user_agent = "") {
return 403;
}
if ($http_user_agent = "-") {
return 403;
}
if ($http_user_agent ~ (<%= @site.server.block_bots.join '|' %>) ) {
return 403;
}
<% end %>
<% if @site.server.proxy.to_cache %>
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
# check if request comes from the user and not from cache server back to the backend
if ( $server_port = <%= @site.server.proxy.port %> ) {
proxy_pass http://<%= @site.service_name %>_cache;
}
<% end %>
try_files index.html $uri @proxy;
}
location @proxy {
<% if @site.server.unicorn? %>
proxy_buffers 4 256k;
proxy_buffer_size 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
<% end %>
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://<%= @site.service_name %>;
<% if @site.server.cache and @site.server.cache.with == 'proxy' %>
proxy_cache <%= @site.server.cache.key_zone %>;
proxy_cache_valid 200 <%= @site.server.cache.expires %>;
<% end %>
}
}