I am trying to integrate Grafana dashboards in a web application. The use of grafana URLs in the web app was failing for CORS errors. After searching on grafana community website found that Grafana as not support and suggested to use a reverse proxy to get rid of the CORS. So added nginx. I could get rid of the CORS errors after adding the required config in nginx.conf file but the Grafana is still failing to load. Following is the config and the problem I am facing -
- Using nginx-1.15.5 on windows 10
Following are the configs on my machine -
grafana custom.ini
http_port = 3000
domain = localhost
root_url = %(protocol)s://%(domain)s/grafana/
nginx.conf
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
server {
listen 80;
root C:\\installables\\nginx-1.15.5\\www;
index index.html index.htm;
server_name localhost;
location /grafana/ {
proxy_pass http://grafana.staged-by-discourse.com/;
rewrite ^/grafana/(.*) /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' "*";
#add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers'
'Accept,
Authorization,
Cache-Control,
Content-Type,
DNT,
If-Modified-Since,
Keep-Alive,
Origin,
User-Agent,
X-Requested-With' always;
return 204;
}
add_header 'Access-Control-Allow-Origin' "*";
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers'
'Accept,
Authorization,
Cache-Control,
Content-Type,
DNT,
If-Modified-Since,
Keep-Alive,
Origin,
User-Agent,
X-Requested-With' always;
add_header 'Access-Control-Expose-Headers' 'Content-Type,Content-Length,Content-Range';
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
Application running on
has following JSP file calling code to invoke the Grafana and show it in an iframe.
<script language="javaScript">
$.ajax({
type: "GET",
url: "http://localhost/grafana/",
contentType: "application/json",
xhrFields: {
withCredentials: false
},
headers: {
// Set any custom headers here.
},
success: function(data){
$("#monitoringframe").attr('srcdoc',data)
},
error : function(err) {
console.log('Error!', err)
}
});
</script>
<iframe name="monitoringframe" id="monitoringframe" src="about:blank" sandbox="allow-forms allow-popups allow-same-origin allow-scripts" width=100% height=600 border="0" frameborder="0" />
Grafana URL works with no CORS issues but fails while loading the grafana javascript files. I am not sure why the js files are being looked over
Need your help to understand what is wrong here and how to make it work.