51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Grafana的API Key+Nginx反向代理实现Grafana免登录访问

利用Grafana的API Key+Nginx反向代理实现Grafana免登录访问

需求背景:

  • 1、无需提供密码给用户,可以让用户直接浏览器免登录访问Grafana大屏
  • 2、并且用户只有浏览的权限,无法配置Grafana及修改配置
  • 3、直接80端口访问grafana,无需访问grafana默认的3000端口

基于以上几个要求,通过搜索引擎查询相关文章,总结出具体的实现步骤

一、修改/etc/grafana/grafana.ini

/etc/grafana/grafana.ini配置文件修改,允许嵌入

cat /etc/grafana/grafana.ini  | grep allow_embedding
sed -i "s/;allow_embedding = false/allow_embedding = true/g" /etc/grafana/grafana.ini 
cat /etc/grafana/grafana.ini  | grep allow_embedding
systemctl restart grafana-server

二、Granfana添加API Key

拷贝一下生成的API Key

eyJrIjoiRnJjVmNURW1vdnlxQkdOTExqM29DcnJJV3g4TnQ0SEwiLCJuIjoid2Vidmlld2VyIiwiaWQiOjF9

curl -H "Authorization: Bearer eyJrIjoiRnJjVmNURW1vdnlxQkdOTExqM29DcnJJV3g4TnQ0SEwiLCJuIjoid2Vidmlld2VyIiwiaWQiOjF9" http://192.168.31.170:3000/api/dashboards/home

三、配置nginx的yum源并安装配置nginx

1、配置nginx的yum源并安装nginx

cat > /etc/yum.repos.d/nginx.repo  << \EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF

yum install nginx -y

2、【可选】修改默认的nginx配置文件nginx.conf

可以自行修改为json格式的格式日志数据输出

[root@centos nginx]# cat nginx.conf

user nginx; worker_processes auto;

error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid;

events { worker_connections 1024; }

http { include /etc/nginx/mime.types; default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

log_format json_analytics escape=json '{'
                '&quot;msec&quot;: &quot;$msec&quot;, ' # request unixtime in seconds with a milliseconds resolution
                '&quot;connection&quot;: &quot;$connection&quot;, ' # connection serial number
                '&quot;connection_requests&quot;: &quot;$connection_requests&quot;, ' # number of requests made in connection
                '&quot;pid&quot;: &quot;$pid&quot;, ' # process pid
                                    '&quot;host&quot;: &quot;$host&quot;, '
                '&quot;remote_addr&quot;: &quot;$remote_addr&quot;, ' # client IP
                '&quot;remote_user&quot;: &quot;$remote_user&quot;, ' # client HTTP username
                '&quot;remote_port&quot;: &quot;$remote_port&quot;, ' # client port
                '&quot;time_local&quot;: &quot;$time_local&quot;, '
                '&quot;time_iso8601&quot;: &quot;$time_iso8601&quot;, ' # local time in the ISO 8601 standard format
                                    '&quot;request_method&quot;: &quot;$request_method&quot;, ' # request method
                '&quot;request&quot;: &quot;$request&quot;, ' # full path no arguments if the request
                '&quot;request_uri&quot;: &quot;$request_uri&quot;, ' # full path and arguments if the request
                                    '&quot;request_id&quot;: &quot;$request_id&quot;, ' # the unique request id
                '&quot;request_length&quot;: &quot;$request_length&quot;, ' # request length (including headers and body)
                                    '&quot;request_time&quot;: $request_time, '
                '&quot;args&quot;: &quot;$args&quot;, ' # args
                '&quot;response_status&quot;: &quot;$status&quot;, ' # response status code
                '&quot;body_bytes_sent&quot;: &quot;$body_bytes_sent&quot;, ' # the number of body bytes exclude headers sent to a client
                '&quot;bytes_sent&quot;: &quot;$bytes_sent&quot;, ' # the number of bytes sent to a client
                                    '&quot;http_version&quot;: &quot;$server_protocol&quot;, '
                '&quot;http_referer&quot;: &quot;$http_referer&quot;, ' # HTTP referer
                '&quot;http_user_agent&quot;: &quot;$http_user_agent&quot;, ' # user agent
                '&quot;http_x_forwarded_for&quot;: &quot;$http_x_forwarded_for&quot;, ' # http_x_forwarded_for
                                    '&quot;http_x_forwarded_proto&quot;: &quot;$http_x_forwarded_proto&quot;, '
                '&quot;http_host&quot;: &quot;$http_host&quot;, ' # the request Host: header
                '&quot;server_name&quot;: &quot;$server_name&quot;, ' # the name of the vhost serving the request
                '&quot;request_time&quot;: &quot;$request_time&quot;, ' # request processing time in seconds with msec resolution
                '&quot;upstream&quot;: &quot;$upstream_addr&quot;, ' # upstream backend server for proxied requests
                '&quot;upstream_connect_time&quot;: &quot;$upstream_connect_time&quot;, ' # upstream handshake time incl. TLS
                '&quot;upstream_header_time&quot;: &quot;$upstream_header_time&quot;, ' # time spent receiving upstream headers
                '&quot;upstream_response_time&quot;: &quot;$upstream_response_time&quot;, ' # time spend receiving upstream body
                '&quot;upstream_response_length&quot;: &quot;$upstream_response_length&quot;, ' # upstream response length
                '&quot;upstream_cache_status&quot;: &quot;$upstream_cache_status&quot;, ' # cache HIT/MISS where applicable
                '&quot;ssl_protocol&quot;: &quot;$ssl_protocol&quot;, ' # TLS protocol
                '&quot;ssl_cipher&quot;: &quot;$ssl_cipher&quot;, ' # TLS cipher
                '&quot;scheme&quot;: &quot;$scheme&quot;, ' # http or https
                '&quot;server_protocol&quot;: &quot;$server_protocol&quot;, ' # request protocol, like HTTP/1.1 or HTTP/2.0
                '&quot;pipe&quot;: &quot;$pipe&quot;, ' # &quot;p&quot; if request was pipelined, &quot;.&quot; otherwise
                '&quot;gzip_ratio&quot;: &quot;$gzip_ratio&quot;, '
                '&quot;http_cf_ray&quot;: &quot;$http_cf_ray&quot;'
                '}';
access_log /var/log/nginx/access.log json_analytics;



sendfile        on;
#tcp_nopush on;

keepalive_timeout  65;

#gzip on;

include /etc/nginx/conf.d/*.conf;

}

输出的json日志美化后的效果如下

3、添加grafana反向代理配置

cd /etc/nginx/conf.d/
mv default.conf /opt/
vim backend_grafana.conf
# 添加如下配置,其中API Key为上一步中的grafana api_key
upstream grafana_server {
    server 127.0.0.1:3000; 
    }

server { listen 80; server_name localhost;

location / {
    proxy_buffer_size 128k;
    proxy_buffers   32 128k;
    proxy_busy_buffers_size 128k;

    add_header Access-Control-Allow-Origin '*';
    add_header Access-Control-Allow-Methods '*';
    add_header Access-Control-Allow-Credentials true;
    #add_header Access-Control-Allow-Headers Authorization;
    set $auth 'Bearer eyJrIjoiRnJjVmNURW1vdnlxQkdOTExqM29DcnJJV3g4TnQ0SEwiLCJuIjoid2Vidmlld2VyIiwiaWQiOjF9';
   
    proxy_set_header     Host $host;
    proxy_set_header     Authorization $auth;
    proxy_set_header     X-Real-IP $remote_addr;
    proxy_set_header     X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header     X-Forwarded-Proto $scheme;
    proxy_pass           http://grafana_server/;
}

}

systemctl enable nginx systemctl start nginx firewall-cmd --permanent --zone=public --add-port=80/tcp firewall-cmd --reload

四、测试免登录效果

直接80端口访问grafana且无需输入账号密码

http://192.168.31.170

五、Tips

1、当然你也可以使用firewalld的rich-rule来控制访问80端口的来源IP

具体步骤

firewall-cmd --permanent --zone=public --remove-port=80/tcp
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address='192.168.31.100' port port="80" protocol="tcp" accept'
firewall-cmd --reload

(图片点击放大查看)

例如192.168.31.60 访问grafana 80端口直接拒绝

curl -H "Authorization: Bearer eyJrIjoiRnJjVmNURW1vdnlxQkdOTExqM29DcnJJV3g4TnQ0SEwiLCJuIjoid2Vidmlld2VyIiwiaWQiOjF9" http://192.168.31.170/api/dashboards/home

(图片点击放大查看)

2、本文参考如下链接实现

https://blog.csdn.net/qq_24985201/article/details/122670276
赞(9)
未经允许不得转载:工具盒子 » Grafana的API Key+Nginx反向代理实现Grafana免登录访问