Overview
Nx Caching Server is a custom remote caching server for Nx, written in Go. This implementation follows the Nx OpenAPI specification (v20.8+) and provides a lightweight, self-hosted remote cache solution.
Since Nx 20.8, you can use your own caching server by implementing their OpenAPI spec. This server handles storage, retrieval, and optional bearer-token authentication.
Key Features
- ✅ Lightweight - Built with Go for performance and efficiency
- ✅ Self-hosted - Full control over your cache infrastructure
- ✅ Secure - Optional bearer token authentication
- ✅ Automatic Cleanup - Configurable cache expiration
- ✅ Docker Ready - Pre-built images available
- ✅ Nx Compatible - Follows Nx OpenAPI specification v20.8+
Configuration
Configure the server using environment variables. All configuration options are optional and have sensible defaults.
| Variable | Description | Default | Example |
|---|---|---|---|
STORAGE_DIR |
Directory for storing cache artifacts | System temp dir | /data/nx-cache |
CLEANUP_THRESHOLD |
Duration after which unused cache entries are removed (hours only) | 1h |
1h, 24h, 168h |
PORT |
Port the server listens on | 8090 |
8080 |
AUTH_TOKEN |
Bearer token required for all requests (optional) | (unset = no auth) | my-secure-token |
📝 Note
CLEANUP_THRESHOLD uses hour-based durations (e.g., 24h = 1 day,
168h = 1 week).
The cleanup process runs automatically at the specified interval and removes cache entries that
haven't been accessed
within the threshold period.
Getting Started
Build from Source
Build and run the server from source code:
1. Clone the repository
git clone https://github.com/enxtur/nx-caching-server.git
cd nx-caching-server
2. Build the binary
go build -o nx-caching-server ./main.go
3. Run it
./nx-caching-server
4. With custom settings
STORAGE_DIR=./cache-data \
PORT=8090 \
AUTH_TOKEN=super-secret-123 \
CLEANUP_THRESHOLD=24h \
./nx-caching-server
Configuring Nx Workspace
Point your Nx workspace to the caching server by setting these environment variables
(or add them to .env / CI config):
Without Authentication
export NX_SELF_HOSTED_REMOTE_CACHE_SERVER=http://localhost:8090
With Authentication
export NX_SELF_HOSTED_REMOTE_CACHE_SERVER=http://localhost:8090
export NX_SELF_HOSTED_REMOTE_CACHE_ACCESS_TOKEN=your-secure-token-here
Then run your tasks as usual:
npx nx run-many --target=build --all
Nx will automatically read from / write to your self-hosted cache, significantly speeding up your build times across different machines and CI/CD pipelines.
API Reference
The server implements the Nx OpenAPI specification (v20.8+). All endpoints support optional
bearer token authentication when AUTH_TOKEN is configured.
Health Check
Returns 200 OK with body "OK" if the
server is running.
Cache Operations
All cache operations use the same endpoint with different HTTP methods:
Uploads a cache artifact. Requires Content-Length header.
Returns 200 OK on success, 409 Conflict if already exists.
Checks if a cache artifact exists. Returns 200 OK if found,
404 Not Found if missing.
Downloads a cache artifact. Returns 200 OK with the file content,
404 Not Found if missing.
Authentication
When AUTH_TOKEN is set, all requests must include a Bearer token in the
Authorization header:
Authorization: Bearer your-secure-token-here
Requests without a valid token will receive 401 Unauthorized.
Docker Deployment
Using Pre-built Image
Pull the pre-built image from Docker Hub:
docker pull enxtur/nx-caching-server
Run with Docker
Example with persistent storage and authentication:
docker run -d \
--name nx-cache \
-p 8090:8090 \
-v $(pwd)/nx-cache-data:/data \
-e STORAGE_DIR=/data \
-e AUTH_TOKEN=your-secure-token-here \
-e CLEANUP_THRESHOLD=48h \
enxtur/nx-caching-server
Build from Source
Or build your own image from source:
docker build -t nx-caching-server .
Using Docker Compose
Create a docker-compose.yml file:
version: '3.8'
services:
nx-cache:
image: enxtur/nx-caching-server:latest
container_name: nx-caching-server
restart: unless-stopped
ports:
- "8090:8090"
volumes:
- ./nx-cache-data:/data
environment:
- STORAGE_DIR=/data
- PORT=8090
- AUTH_TOKEN=your-secure-token-here # remove this line if you want no authentication
- CLEANUP_THRESHOLD=24h
Start it:
docker compose up -d
🐳 Docker Image Details
The Docker image is built using a multi-stage build process:
- Builder stage: Uses
golang:1.25.6-alpineto compile the Go application - Runtime stage: Uses
alpine:3.23for a minimal final image - Size: Optimized for minimal footprint
License
MIT License
Copyright (c) 2025 Interview Aloud Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.