Skip to content

Instantly share code, notes, and snippets.

@polarnik
Last active November 15, 2023 17:54
Show Gist options
  • Save polarnik/101880aba4c722ab3dc31748fb2cea3d to your computer and use it in GitHub Desktop.
Save polarnik/101880aba4c722ab3dc31748fb2cea3d to your computer and use it in GitHub Desktop.
Nginx config for connecting Grafana with InfluxDB (COSR settings problem) with cache (perf improvment)
#user nobody
worker_processes 4;
error_log /var/log/nginx/error.log warn;
events {
worker_connections 1024;
}
http {
proxy_cache_path /data/nginx/cache keys_zone=mycache:20m max_size=1g inactive=60m;
limit_conn_zone $server_name zone=perserver:10m;
upstream backend {
server host.docker.internal:8086;
keepalive 50;
}
server {
listen 8096;
client_max_body_size 20m;
location /query {
proxy_cache mycache;
proxy_cache_key "$host$request_uri";
proxy_cache_min_uses 1;
proxy_cache_methods GET;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_background_update on;
proxy_cache_revalidate on;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
proxy_ignore_headers Cache-Control;
proxy_hide_header 'Access-Control-Allow-Origin';
if ($http_origin = ''){
set $http_origin "*";
}
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Pragma,Accept,Authorization,Content-Type,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
add_header 'Access-Control-Allow-Credentials' 'true';
return 204;
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Pragma,Accept,Authorization,Content-Type,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
#add_header 'Access-Control-Expose-Headers' 'Accept,Authorization,Content-Type,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
proxy_pass http://backend;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Pragma,Accept,Authorization,Content-Type,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
proxy_pass http://backend;
}
}
location / {
proxy_pass http://backend;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment