0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Grafana + Image-Renderer 環境のNginxリバースプロキシ設定

Last updated at Posted at 2025-12-12

Prometheus + Grafana + Image-Renderer を利用している環境で
Nginxを挟んで80番(443番)にアクセスさせたときのNignxの設定。

単純に3000番に通すだけでは、CORSエラーなどでうまくいかなかった。

最終的に以下の設定で動作させている。

server {
    listen 80;
    server_name _;
    
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,x-grafana-device-id,x-grafana-org-id,x-grafana-user,x-grafana-auth,x-dashboard-uid,x-datasource-uid,x-panel-id' always;
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;

    location @cors_options {
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,x-grafana-device-id,x-grafana-org-id,x-grafana-user,x-grafana-auth,x-dashboard-uid,x-datasource-uid,x-panel-id' always;
        add_header 'Access-Control-Max-Age' 1728000 always;
        add_header 'Content-Type' 'text/plain; charset=utf-8' always;
        add_header 'Content-Length' 0 always;
        return 204;
    }

    # Image Renderer の直接エンドポイント(Grafanaからの内部呼び出し用)
    # 注意: これは通常外部からアクセスされるべきではない
    location = /render {
        if ($request_method = 'OPTIONS') {
            return 204;
        }

        proxy_pass http://192.168.100.100:8081/render;
        proxy_set_header Host $host;
        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_connect_timeout 120s;
        proxy_send_timeout 120s;
        proxy_read_timeout 120s;

        proxy_max_temp_file_size 0;
        proxy_buffering off;

        access_log /var/log/nginx/render_access.log;
        error_log /var/log/nginx/render_error.log debug;
    }

    # その他の Grafana リクエスト
    location / {
        if ($request_method = 'OPTIONS') {
            return 204;
        }
        
        proxy_pass http://192.168.100.100:3000;
        proxy_set_header Host $host;
        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    # 静的ファイル
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        if ($request_method = 'OPTIONS') {
            return 204;
        }
        
        proxy_pass http://192.168.100.100:3000;
        proxy_set_header Host $host;
        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;
        
        expires 1d;
        add_header Cache-Control "public, immutable";
    }
}

server {
    listen 443 ssl;
    server_name _;

    ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;

    return 301 http://192.168.100.100$request_uri;
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?