Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 256c14fece | |||
| 3b202226a2 |
@@ -3,9 +3,9 @@ FROM nginx:alpine
|
|||||||
# Copy nginx configuration
|
# Copy nginx configuration
|
||||||
COPY nginx.conf /etc/nginx/nginx.conf
|
COPY nginx.conf /etc/nginx/nginx.conf
|
||||||
|
|
||||||
# Create cache directory
|
# Create data directories
|
||||||
RUN mkdir -p /var/cache/nginx && \
|
RUN mkdir -p /etc/nginx/data/cache /etc/nginx/data/temp && \
|
||||||
chown nginx:nginx /var/cache/nginx
|
chown -R nginx:nginx /etc/nginx/data
|
||||||
|
|
||||||
# Expose port
|
# Expose port
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|||||||
19
README.md
19
README.md
@@ -15,28 +15,21 @@ docker-compose up
|
|||||||
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
|
||||||
```
|
```
|
||||||
|
|
||||||
## Configuration
|
|
||||||
|
|
||||||
The nginx configuration implements an inverted caching logic:
|
|
||||||
- **Default behavior**: Requests are NOT cached
|
|
||||||
- **Cached behavior**: Only requests with `X-Cache: 1` header are cached
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Proxy a 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"
|
||||||
|
|
||||||
# Enable caching with X-Cache header
|
|
||||||
curl -H "X-Cache: 1" "http://localhost:3000/?url=https://api.example.com/data"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Cache path: `/var/cache/nginx`
|
- **Always caches** all proxied requests
|
||||||
- Cache zone: `api_cache` (10MB)
|
- Cache path: `/etc/nginx/data/cache`
|
||||||
- Max cache size: 1GB
|
- Cache zone: `api_cache` (10MB memory)
|
||||||
|
- Max cache size: 100GB
|
||||||
- Cache validity: 365 days for 200 responses
|
- Cache validity: 365 days for 200 responses
|
||||||
|
- Max request size: 128MB
|
||||||
- Cache status header: `X-Cache-Status` shows cache hit/miss status
|
- Cache status header: `X-Cache-Status` shows cache hit/miss status
|
||||||
- HTTPS support with proper SSL/TLS headers
|
- HTTPS support with proper SSL/TLS headers
|
||||||
|
|
||||||
|
|||||||
11
build.sh
11
build.sh
@@ -9,16 +9,11 @@ DOCKER_REPO="docker.site.quack-lab.dev"
|
|||||||
IMAGE_NAME="nginx-cache-proxy"
|
IMAGE_NAME="nginx-cache-proxy"
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# VALIDATE CONFIGURATION
|
# BUILD PROJECT
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|
||||||
echo "Validating nginx configuration..."
|
echo "Building application..."
|
||||||
docker run --rm -v "$(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro" nginx:alpine nginx -t
|
# No build step needed for nginx
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Error validating nginx configuration"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# DOCKER BUILD AND TAG
|
# DOCKER BUILD AND TAG
|
||||||
|
|||||||
11
nginx.conf
11
nginx.conf
@@ -9,7 +9,7 @@ http {
|
|||||||
uwsgi_temp_path /etc/nginx/data/temp;
|
uwsgi_temp_path /etc/nginx/data/temp;
|
||||||
scgi_temp_path /etc/nginx/data/temp;
|
scgi_temp_path /etc/nginx/data/temp;
|
||||||
|
|
||||||
proxy_cache_path /etc/nginx/data/cache levels=1:2 keys_zone=api_cache:10m max_size=1g 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 debug;
|
||||||
access_log /dev/stdout;
|
access_log /dev/stdout;
|
||||||
@@ -18,7 +18,7 @@ http {
|
|||||||
resolver 127.0.0.11 valid=10s;
|
resolver 127.0.0.11 valid=10s;
|
||||||
|
|
||||||
server {
|
server {
|
||||||
listen 3001;
|
listen 3000;
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
if ($arg_url = "") {
|
if ($arg_url = "") {
|
||||||
@@ -32,13 +32,6 @@ http {
|
|||||||
|
|
||||||
proxy_cache api_cache;
|
proxy_cache api_cache;
|
||||||
proxy_cache_valid 200 365d;
|
proxy_cache_valid 200 365d;
|
||||||
proxy_cache_bypass $http_x_cache_inverse;
|
|
||||||
proxy_no_cache $http_x_cache_inverse;
|
|
||||||
|
|
||||||
set $http_x_cache_inverse 1;
|
|
||||||
if ($http_x_cache) {
|
|
||||||
set $http_x_cache_inverse 0;
|
|
||||||
}
|
|
||||||
add_header X-Cache-Status $upstream_cache_status always;
|
add_header X-Cache-Status $upstream_cache_status always;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user