diff --git a/ansible/roles/stack-proxy/templates/nginx.conf b/ansible/roles/stack-proxy/templates/nginx.conf index baa921306d769b04152a5d98d5fcb3b6515da768..3b23cbf904a43c3e587ed2b8312368a3bc5bc29a 100644 --- a/ansible/roles/stack-proxy/templates/nginx.conf +++ b/ansible/roles/stack-proxy/templates/nginx.conf @@ -13,29 +13,66 @@ http { include /etc/nginx/mime.types; default_type application/octet-stream; - log_format main '$http_x_forwarded_for - $remote_addr - $remote_user [$time_local] ' + lua_load_resty_core off; + log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '$request_time $upstream_response_time $pipe' '"$http_referer" "$http_user_agent"'; access_log /var/log/nginx/access.log main; + # Shared dictionary to store metrics + lua_shared_dict prometheus_metrics 10M; + lua_package_path "/etc/nginx/lua_modules/?.lua"; + # Defining metrics + init_by_lua ' + prometheus = require("prometheus").init("prometheus_metrics") + metric_requests = prometheus:counter( + "nginx_http_requests_total", "Number of HTTP requests", {"host", "status", "request_method"}) + metric_latency = prometheus:histogram( + "nginx_http_request_duration_seconds", "HTTP request latency", {"host"}) + metric_connections = prometheus:gauge( + "nginx_http_connections", "Number of HTTP connections", {"state"}) + '; + + # Collecting metrics + log_by_lua ' + metric_requests:inc(1, {ngx.var.server_name, ngx.var.status, ngx.var.request_method}) + metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name}) + '; + sendfile on; #tcp_nopush on; client_max_body_size 60M; - keepalive_timeout 65; + keepalive_timeout 500s; + keepalive_requests 200; + + # Nginx connection limit per ip + limit_conn_zone $binary_remote_addr zone=limitbyaddr:10m; + limit_conn_status 429; upstream kong { server api-manager_kong:8000; keepalive 1000; } - #gzip on; upstream player { server player_player:3000; keepalive 1000; } include /etc/nginx/conf.d/*.conf; + + server { + listen 9145; + location /metrics { + content_by_lua ' + metric_connections:set(ngx.var.connections_reading, {"reading"}) + metric_connections:set(ngx.var.connections_waiting, {"waiting"}) + metric_connections:set(ngx.var.connections_writing, {"writing"}) + prometheus:collect() + '; + } + } }