I need to solve the CORS policy error in Grafana. For that, I use Nginx as a reverse proxy in Grafana. However, it cannot resolve. I am using Grafana 10.2.2 on Windows, and my Nginx file is:
# Define the number of worker processes
worker_processes 1;
# http events block
events {
# Set the maximum number of simultaneous connections a worker process can accept
worker_connections 512;
}
http {
# Map HTTP Upgrade header to Connection upgrade header for WebSockets
map $http_upgrade $connection_upgrade {
default upgrade;
"" close;
}
# Define an upstream block named "grafana" pointing to your Grafana instance
upstream grafana {
server localhost:3000;
}
# Server block configuration
server {
# Listen on port 80
listen 80;
# Access log configuration (uncomment to enable)
# access_log logs/access.log main;
# Serve static content from the root path if no location matches (optional)
root /path/to/static/content;
#index index.html index.htm;
#root http://localhost:3000;
# Define default index files to serve when no specific file is requested
index index.html index.htm;
# Location block for requests to /grafana/ path
location /grafana/ {
# Set the Host header in the proxied request to the original request host
proxy_set_header Host $host;
# Proxy the request to the "grafana" upstream server
proxy_pass http://grafana;
}
# Location block to handle WebSocket connections for Grafana Live API
location /grafana/api/live/ {
# Use HTTP version 1.1 for proxying
proxy_http_version 1.1;
# Set headers for WebSocket communication
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
# Proxy the request to the "grafana" upstream server
proxy_pass http://grafana;
}
# Wide-open CORS config for Nginx (consider adjusting for security)
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization';
# Tell the client that this pre-flight info is valid for 20 days (optional)
# add_header 'Access-Control-Max-Age' 1728000;
# Optional headers for some browsers
# add_header 'Content-Type' 'text/plain; charset=utf-8';
# add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*'; # Adjust for security
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*'; # Adjust for security
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization';
}
}
# Include mime types configuration file
include mime.types;
# Set default content type for unrecognized files
default_type application/octet-stream;
# Configure various timeouts and buffering options (adjust as needed)
# sendfile on;
# tcp_nopush on;
keepalive_timeout 65;
# gzip on; # Enable gzip compression if desired
# You can add additional server blocks here to serve other virtual hosts
}
}