Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ed228b7fb | |||
| 5e4c9c533e |
@@ -1,7 +1,8 @@
|
|||||||
FROM nginx:alpine
|
FROM nginx:alpine
|
||||||
|
|
||||||
# Copy nginx configuration
|
# Copy nginx configuration and HTML
|
||||||
COPY nginx.conf /etc/nginx/nginx.conf
|
COPY nginx.conf /etc/nginx/nginx.conf
|
||||||
|
COPY index.html /usr/share/nginx/html/index.html
|
||||||
|
|
||||||
# Create data directories
|
# Create data directories
|
||||||
RUN mkdir -p /etc/nginx/data/cache /etc/nginx/data/temp && \
|
RUN mkdir -p /etc/nginx/data/cache /etc/nginx/data/temp && \
|
||||||
|
|||||||
38
README.md
38
README.md
@@ -1,15 +1,12 @@
|
|||||||
# Nginx Cache Proxy
|
# Nginx Cache Proxy
|
||||||
|
|
||||||
An nginx proxy server that caches requests only when explicitly requested via the `X-Cache: 1` header.
|
Navigate to `http://localhost:3000/` for interactive usage with URL builder.
|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Build the image
|
# Build and run
|
||||||
./build.sh
|
./build.sh && docker-compose up
|
||||||
|
|
||||||
# Run with docker-compose (uses the built image)
|
|
||||||
docker-compose up
|
|
||||||
|
|
||||||
# Or run directly
|
# Or run directly
|
||||||
docker run -p 3000:3000 docker.site.quack-lab.dev/nginx-cache-proxy:latest
|
docker run -p 3000:3000 docker.site.quack-lab.dev/nginx-cache-proxy:latest
|
||||||
@@ -20,19 +17,24 @@ docker run -p 3000:3000 docker.site.quack-lab.dev/nginx-cache-proxy:latest
|
|||||||
```bash
|
```bash
|
||||||
# Proxy and cache any URL
|
# Proxy and cache any URL
|
||||||
curl "http://localhost:3000/?url=https://api.example.com/data"
|
curl "http://localhost:3000/?url=https://api.example.com/data"
|
||||||
|
|
||||||
|
# Skip cache for this request
|
||||||
|
curl -H "X-NoCache: 1" "http://localhost:3000/?url=https://api.example.com/data"
|
||||||
|
|
||||||
|
# Cache custom status codes (e.g., 400s)
|
||||||
|
curl -H "X-Status: 400" "http://localhost:3000/?url=https://api.example.com/bad"
|
||||||
|
|
||||||
|
# Purge cache for specific URL
|
||||||
|
curl "http://localhost:3000/purge/?url=https://api.example.com/data"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Always caches** all proxied requests
|
- **Always caches** 2xx status codes for 1 year
|
||||||
- Cache path: `/etc/nginx/data/cache`
|
- Cache size: 100GB max
|
||||||
- Cache zone: `api_cache` (10MB memory)
|
- Request size: 128MB max
|
||||||
- Max cache size: 100GB
|
- X-Cache-Status header shows cache status
|
||||||
- Cache validity: 365 days for 200 responses
|
- X-NoCache header bypasses cache
|
||||||
- Max request size: 128MB
|
- X-Status header caches custom status codes
|
||||||
- Cache status header: `X-Cache-Status` shows cache hit/miss status
|
- Interactive web interface at root path
|
||||||
- HTTPS support with proper SSL/TLS headers
|
- Cache purge endpoint
|
||||||
|
|
||||||
## In n8n
|
|
||||||
|
|
||||||
Add the `X-Cache: 1` header only to requests you want cached. All other requests will bypass the cache completely.
|
|
||||||
|
|||||||
89
index.html
Normal file
89
index.html
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Nginx Cache Proxy</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
|
||||||
|
.usage { background: #f5f5f5; padding: 15px; border-radius: 5px; margin: 10px 0; }
|
||||||
|
.copy-field { margin: 20px 0; }
|
||||||
|
input { width: 100%; padding: 10px; font-size: 16px; }
|
||||||
|
button { background: #007cba; color: white; border: none; padding: 10px 20px; cursor: pointer; }
|
||||||
|
button:hover { background: #005a87; }
|
||||||
|
.status { margin-top: 10px; color: green; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Nginx Cache Proxy</h1>
|
||||||
|
<p>Proxy and cache any HTTP/HTTPS URL with automatic caching.</p>
|
||||||
|
|
||||||
|
<div class="copy-field">
|
||||||
|
<h3>Quick URL Builder</h3>
|
||||||
|
<input type="text" id="urlInput" placeholder="Enter target URL (e.g., https://api.example.com/data)">
|
||||||
|
<button onclick="copyUrl()">Copy Proxy URL</button>
|
||||||
|
<div id="status" class="status"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2>Usage</h2>
|
||||||
|
|
||||||
|
<div class="usage">
|
||||||
|
<h3>Basic Proxy</h3>
|
||||||
|
<pre>curl "http://localhost:3000/?url=https://api.example.com/data"</pre>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="usage">
|
||||||
|
<h3>Skip Cache</h3>
|
||||||
|
<pre>curl -H "X-NoCache: 1" "http://localhost:3000/?url=https://api.example.com/data"</pre>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="usage">
|
||||||
|
<h3>Cache Custom Status Codes</h3>
|
||||||
|
<pre>curl -H "X-Status: 400" "http://localhost:3000/?url=https://api.example.com/bad"</pre>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="usage">
|
||||||
|
<h3>Purge Cache</h3>
|
||||||
|
<pre>curl "http://localhost:3000/purge/?url=https://api.example.com/data"</pre>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2>Features</h2>
|
||||||
|
<ul>
|
||||||
|
<li>Always caches 2xx status codes for 1 year</li>
|
||||||
|
<li>100GB max cache size</li>
|
||||||
|
<li>128MB max per request</li>
|
||||||
|
<li>X-Cache-Status header shows hit/miss</li>
|
||||||
|
<li>X-NoCache header bypasses cache</li>
|
||||||
|
<li>X-Status header caches custom status codes</li>
|
||||||
|
<li>Purge endpoint for cache management</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function copyUrl() {
|
||||||
|
const input = document.getElementById('urlInput');
|
||||||
|
const status = document.getElementById('status');
|
||||||
|
|
||||||
|
if (!input.value.trim()) {
|
||||||
|
status.textContent = 'Please enter a URL first';
|
||||||
|
status.style.color = 'red';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const proxyUrl = window.location.origin + '/?url=' + encodeURIComponent(input.value);
|
||||||
|
|
||||||
|
navigator.clipboard.writeText(proxyUrl).then(() => {
|
||||||
|
status.textContent = 'Proxy URL copied to clipboard!';
|
||||||
|
status.style.color = 'green';
|
||||||
|
}).catch(err => {
|
||||||
|
// Fallback for older browsers
|
||||||
|
const textArea = document.createElement('textarea');
|
||||||
|
textArea.value = proxyUrl;
|
||||||
|
document.body.appendChild(textArea);
|
||||||
|
textArea.select();
|
||||||
|
document.execCommand('copy');
|
||||||
|
document.body.removeChild(textArea);
|
||||||
|
status.textContent = 'Proxy URL copied to clipboard!';
|
||||||
|
status.style.color = 'green';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
18
nginx.conf
18
nginx.conf
@@ -11,18 +11,18 @@ http {
|
|||||||
|
|
||||||
proxy_cache_path /etc/nginx/data/cache levels=1:2 keys_zone=api_cache:10m max_size=100g inactive=365d;
|
proxy_cache_path /etc/nginx/data/cache levels=1:2 keys_zone=api_cache:10m max_size=100g inactive=365d;
|
||||||
|
|
||||||
error_log /dev/stdout debug;
|
error_log /dev/stdout warn;
|
||||||
access_log /dev/stdout;
|
access_log /dev/stdout;
|
||||||
|
|
||||||
# Use Docker's internal DNS
|
resolver 127.0.0.11 valid=60s;
|
||||||
resolver 127.0.0.11 valid=10s;
|
|
||||||
|
|
||||||
server {
|
server {
|
||||||
listen 3000;
|
listen 3000;
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
|
# If no url parameter, serve the readme
|
||||||
if ($arg_url = "") {
|
if ($arg_url = "") {
|
||||||
return 400 "Missing url parameter";
|
rewrite ^ /index.html last;
|
||||||
}
|
}
|
||||||
|
|
||||||
proxy_pass $arg_url;
|
proxy_pass $arg_url;
|
||||||
@@ -31,8 +31,16 @@ http {
|
|||||||
proxy_ssl_server_name on;
|
proxy_ssl_server_name on;
|
||||||
|
|
||||||
proxy_cache api_cache;
|
proxy_cache api_cache;
|
||||||
proxy_cache_valid 200 365d;
|
proxy_cache_valid 200 201 202 203 204 205 206 207 208 226 365d;
|
||||||
|
|
||||||
|
proxy_cache_bypass $http_x_nocache;
|
||||||
|
proxy_no_cache $http_x_nocache;
|
||||||
|
|
||||||
add_header X-Cache-Status $upstream_cache_status always;
|
add_header X-Cache-Status $upstream_cache_status always;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
location = /index.html {
|
||||||
|
root /usr/share/nginx/html;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user