Running Metabase with a URL Prefix (using NGINX)

Figured id make this post to help anyone else who was struggling with this...

To run Metabase with a url prefix (eg. you need to do a little extra configuring beyond just setting the MB_SITE_URL env var.

This can be done pretty easily with nginx by rewriting all all urls with /metabase before passing to the backend application so metabase can continue to run on the root path.

Here is a sample of an nginx.conf to do this:

http {
     default_type none;
    disable_symlinks off;
     sendfile        on;
     keepalive_timeout  65;
     gzip  on;

# upstream pointing to our docker container running metabase on port 3000
upstream metabase-application {
         server metabase_app:3000;

server {
       # have our nginx server listen on port 4000 and pass traffic to metabase on port 3000
       listen       4000;
       client_max_body_size 0;

       # rewrite all requests to /metabase to remove this prefix

       location /metabase {
           rewrite ^/metabase(/.*)$ $1 last;
       # route all rewritten traffic to the metabase application 
          location / {
               set $stored_real_location $upstream_http_x_real_location;
               proxy_request_buffering off;
               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "Upgrade";
               proxy_pass http://metabase-application$stored_real_location;

For our usecase, we are running Metabase under an AWS ALB, so the ALB sends all /metabase/* traffic to the nginx container. The nginx container rewites the path by stripping the /metabase prefix, then sends it to the Metabase application.

Hope this helps!

1 Like