负载均衡

四层负载配置示例

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
cat nginx.conf
http {
...
}
stream {
server_traffic_status_zone;
log_format proxy '$remote_addr - [$time_local] $status "$upstream_bytes_sent" "$upstream_bytes_received" - "$upstream_addr" "$upstream_connect_time"';

access_log logs/upstream-access.log proxy;
open_log_file_cache off;

upstream credit-gateway {
#hash $remote_addr consistent;
server 3.1.19.51:11200;
server 3.1.19.52:11200;
}

server {
listen 11200;
proxy_pass credit-gateway;
proxy_timeout 300;
proxy_connect_timeout 60;
access_log logs/upstream-gateway.log proxy;
}
}

反向代理

完整配置示例

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
user  root;
worker_processes auto;

# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;

error_log logs/error.log warn;
pid logs/nginx.pid;

events {
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;

lua_package_path "/usr/local/openresty/lualib/skywalking-nginx-lua/?.lua;;";

lua_shared_dict tracing_buffer 100M;

init_worker_by_lua_block {
local metadata_buffer = ngx.shared.tracing_buffer
metadata_buffer:set('serviceName', 'Front')
metadata_buffer:set('serviceInstanceName', 'front-risk')
metadata_buffer:set('includeHostInEntrySpan', false)
require("skywalking.util").set_randomseed()
require("skywalking.client"):startBackendTimer("http://3.1.19.157:12800")
skywalking_tracer = require("skywalking.tracer")
}


log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

log_format json escape=json
'{"@timestamp":"$time_iso8601",'
'"host":"$hostname",'
'"server_ip":"$server_addr",'
'"client_ip":"$remote_addr",'
'"xff":"$http_x_forwarded_for",'
'"domain":"$host",'
'"url":"$uri",'
'"referer":"$http_referer",'
'"args":"$args",'
'"upstreamtime":"$upstream_response_time",'
'"responsetime":"$request_time",'
'"request_method":"$request_method",'
'"status":"$status",'
'"size":"$body_bytes_sent",'
# '"request_body":"$request_body",'
'"request_length":"$request_length",'
'"protocol":"$server_protocol",'
'"upstreamhost":"$upstream_addr",'
'"upstreamtime":"$upstream_response_time",'
'"upstreamstatus":"$upstream_status",'
'"file_dir":"$request_filename",'
'"http_user_agent":"$http_user_agent"'
'}';


access_log logs/access.log json;
sendfile on;

#tcp_nopush on;
keepalive_timeout 120;

#gzip on;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

client_header_buffer_size 128k;
client_body_buffer_size 1m;
underscores_in_headers on;

# prometheus metircs: /status/format/prometheus
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
stream_server_traffic_status_zone;

server {
listen 8002;
server_name localhost;
location /vts_metrics {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
location /sts_metrics {
stream_server_traffic_status_display;
stream_server_traffic_status_display_format html;
}
}

server {
listen 8902;
listen [::]:8902;
server_name localhost;
root /data/htdocs/front-bank-risk;
index index.html index.htm;

location / {
proxy_pass http://3.1.19.157:11200/dics/uat/login/;
proxy_redirect off;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "Keep-Alive";
proxy_connect_timeout 60;
proxy_send_timeout 120;
proxy_read_timeout 120;
}
access_log logs/dics-risk-access.log json;
}
}