Setting up Memgraph Lab with a custom base path

Starting from the version 2.19.0, Memgraph Lab allows you to configure a custom base path for the web application when running it in Docker. This configuration is especially beneficial when using a reverse proxy like NGINX to manage access to multiple applications.

To use this feature, you can define the environment variable BASE_PATH when starting Memgraph Lab. For example, you can set BASE_PATH to /mylab, and Memgraph Lab will be accessible at that base path (e.g. localhost:3000/mylab).

Configure Memgraph Lab

When running Memgraph Lab in Docker, specify the BASE_PATH environment variable. For example:

docker run -e BASE_PATH=mylab -p 3000:3000 memgraph/lab

This starts Memgraph Lab on port 3000 and sets the base path to /mylab.

Configure NGINX for reverse proxy

Here is an example nginx.conf for redirecting requests to Memgraph Lab:

# nginx.conf
events {}

http {
  server {
    listen 8080;

    location /mylab/ {
      proxy_pass http://localhost:3000/mylab/;
      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;

      # WebSocket specific headers
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
  }
}

Explanation of key configurations:

  • location: This should match the base path. In the example above, base path is /mylab, and location is /mylab/.
  • proxy_pass: This should match the Memgraph Lab host and include the same base path as defined in BASE_PATH. In the example above, the base path is /mylab, and proxy_pass is http://localhost:3000/mylab/.
  • WebSocket headers: These headers ensure WebSocket communication, such as the Logs feature in the Lab UI, works correctly. Include proxy_http_version 1.1, proxy_set_header Upgrade, and proxy_set_header Connection "upgrade".
💡

Special Note for Docker Networking: If both Memgraph Lab and the NGINX server are running on the same machine, but you access them using Docker, you can use host.docker.internal in proxy_pass instead of localhost.

For example:

proxy_pass http://host.docker.internal:3000/mylab/;

Accessing Memgraph Lab

After configuring the above settings, Memgraph Lab will be accessible via the reverse proxy at:

http://<nginx-server-address>:8080/mylab/

Replace <nginx-server-address> with the actual address of your NGINX server.