Grafana is an open source data visualization and monitoring suite. It offers support for Graphite, Elasticsearch, Prometheus, Zabbix and many more databases. Grafana allows you to query, visualize, alert on and understand your metrics with the ability to manage and create your own dashboard for your apps or infrastructure performance monitoring.
In this tutorial, we are going to explain how to configure Nginx as reverse proxy for Grafana Server. For the purpose of this tutorial we are going to install Nginx on the same server where Grafana server is installed, Grafana will run behind the Nginx as a reverse proxy and it will listen on the port 80 and will redirect all the request to Grafana on the port 3000. If you didn’t yet installed Zabbix server or Grafana Check our previous tutorials:
- How to Install Grafana using MySQL/MariaDB database on CentOS 7 / RHEL 7
- How to Integrate Grafana with Zabbix 3.4
1° Option – Running Grafana behind a reverse proxy as Subpath
Step1./ Grafana configuration
– In grafana.ini file add the below line
[root@ylclgrfas01 ~]# vi /etc/grafana/grafana.ini [...] # The full public facing url you use in browser, used for redirects and emails # If you use reverse proxy and sub path specify full url (with sub path) root_url = http://localhost:3000/grafana/ [...]
– After any modification to the file grafana.ini you should restart Grafana Server
[root@ylclgrfas01 ~]# systemctl restart grafana-server
Step2./ Nginx configuration
– Create a new directories where we gonna create our nginx block files:
[root@ylclgrfas01 ~]# mkdir /etc/nginx/sites-available/ /etc/nginx/sites-enabled
– Open now nginx.conf file and Add the following line to the end of the http {} block:
[root@ylclgrfas01 ~]# /etc/nginx/nginx.conf [...] include /etc/nginx/sites-enabled/*.conf;
– Let’s create a new nginx block named yallalabs.local.conf:
[root@ylclgrfas01 ~]# vi /etc/nginx/sites-available/yallalabs.local.conf server { listen 80; listen [::]:80 ipv6only=on; server_name yallalabs.local www.yallalabs.local; root /usr/share/nginx/www; index index.html index.htm; location /grafana/ { proxy_pass http://localhost:3000/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; 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; } }
– Now that we have created our server block file, we need to enable it. To do this, we have to create a symbolic link for the server block in the sites-enabled directory.
[root@ylclgrfas01 ~]# ln -s /etc/nginx/sites-available/yallalabs.local.conf /etc/nginx/sites-enabled/yallalabs.local.conf
– To check the use the bolow command:
[root@ylclgrfas01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
– Make sure to reload Nginx using the following command:
[root@ylclgrfas01 ~]# systemctl reload nginx
2° Option – Running Grafana behind a reverse proxy as Subdomain
Step1./ Grafana configuration
– In grafana.ini file add the below line
[root@ylclgrfas01 ~]# vi /etc/grafana/grafana.ini [...] # The full public facing url you use in browser, used for redirects and emails # If you use reverse proxy and sub path specify full url (with sub path) root_url = http://localhost:3000 [...]
– After any modification to the file grafana.ini you should restart Grafana Server
[root@ylclgrfas01 ~]# systemctl restart grafana-server
Step2./ Nginx configuration
– Let’s create a new nginx block file as below:
[root@ylclgrfas01 ~]# vi /etc/nginx/sites-available/grafana.yallalabs.local.conf server { listen 80; listen [::]:80 ipv6only=on; server_name grafana.yallalabs.local www.grafana.yallalabs.local; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; 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; } }
– Now that we have created our server block file, we need to enable it.
[root@ylclgrfas01 ~]# ln -s /etc/nginx/sites-available/grafana.yallalabs.local.conf /etc/nginx/sites-enabled/grafana.yallalabs.local.conf
– Make sure to reload Nginx using the following command:
[root@ylclgrfas01 ~]# systemctl reload nginx
– If you installed Grafana and Nginx on CentOS 7 or RHEL 7 and the Selinux is enabled you need to execute the following command:
[root@ylclgrfas01 ~]# setsebool -P httpd_can_network_connect=1
We hope this tutorial was enough Helpful. If you need more information, or have any questions, just comment below and we will be glad to assist you!
1 comment
Might have to set this in Grafana.ini
serve_from_sub_path = true
At least I set it and it works behind a proxy. Thanks for the guide.