This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 351
/
router-nginx.conf.erb
146 lines (116 loc) · 4.62 KB
/
router-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
user root root;
worker_processes 1;
error_log <%= node[:nginx][:log_home] %>/nginx_router_error.log debug;
pid /var/run/nginx_router.pid;
events {
worker_connections <%= node[:nginx][:worker_connections] %>;
use epoll;
# multi_accept on;
}
http {
include mime.types;
default_type text/html;
server_tokens off;
log_format main '$host - [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'$remote_addr response_time:$upstream_response_time app_id:$app_id';
access_log <%= node[:nginx][:log_home] %>/nginx_router_main_access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 75 20;
gzip on;
gzip_min_length 1250;
gzip_buffers 16 8k;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
client_max_body_size 256M;
lua_package_path "<%= node[:lua][:module_path] %>/?.lua;;";
lua_package_cpath "<%= node[:lua][:module_path] %>/?.so;;";
upstream router_status {
server <%= node[:nginx][:uls_ip] %>:<%= node[:nginx][:uls_port] %>;
}
server {
listen 80;
server_name _;
server_name_in_redirect off;
#TODO: how to make this internal location totally transparent to outside
location = /vcapuls {
internal;
# We should use rewrite_by_lua to scrub subrequest headers
# as uls doesn't care those headers at all.
# Given there are some exceptions to clear some headers,
# we just leave them as is.
proxy_pass http://unix:/tmp/router.sock:/;
}
location / {
access_log <%= node[:nginx][:log_home] %>/nginx_router_access.log main;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_connect_timeout 10;
proxy_send_timeout 120;
proxy_read_timeout 120;
set $health_monitor '';
set $app_id 0;
if ($http_user_agent = "HTTP-Monitor/1.1") {
set $health_monitor T;
}
if ($http_host = "") {
set $health_monitor "${health_monitor}T";
}
if ($health_monitor = TT) {
# Trigger a subrequest to sync the latest few stats of the worker to uls,
# if we have multiple workers, there will be still few stats not synced for
# the workers which don't get this monitor request.
access_by_lua '
local uls = require ("uls")
ngx.log(ngx.DEBUG, "monitor trigger stats syncup")
local req = uls.generate_stats_request()
-- generate one subrequest to uls to update stats
ngx.location.capture(
"/vcapuls", { body = req }
)
';
more_set_input_headers "Authorization: Basic <%= Base64.encode64("#{node[:nginx][:status_user]}:#{node[:nginx][:status_passwd]}").strip %>";
rewrite ^.*$ /healthz break;
proxy_pass http://router_status;
}
# We intend to have one "if" block to avoid the above monitor location
# to twist with below upstream locator server handling.
# ("if" block effectively creates a nested location and will inherit
# all the rewrite/access phase handlers of outer location)
if ($health_monitor != TT) {
# The following variables are used by lua module code.
# DO NOT remove or rename any of them!
set $backend_addr ''; # Backend server address returned from uls for this request
set $uls_req_tags ''; # Request tags returned from uls for this request to catalog statistics
set $router_ip '';
set $timestamp 0;
set $trace '';
set $sticky '';
access_by_lua '
local uls = require ("uls")
uls.pre_process_subrequest(ngx, "<%= node[:router][:trace_key] %>")
local req = uls.generate_uls_request(ngx)
-- generate one subrequest to uls for querying
local res = ngx.location.capture(
"/vcapuls", { body = req }
)
uls.post_process_subrequest(ngx, res)
';
proxy_pass http://$backend_addr;
# Handling response from backend servers
header_filter_by_lua '
local uls = require ("uls")
uls.post_process_response(ngx)
';
}
}
}
}