diff --git a/README.md b/README.md index 8e46c8f..8167d6b 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,17 @@ The nginx configuration implements an inverted caching logic: ## Usage -```bash -# Normal request - NOT cached (default) -curl http://localhost:3000/?url=https://api.example.com/data +Both URL formats are supported: -# Cached request - add the header -curl -H "X-Cache: 1" http://localhost:3000/?url=https://api.example.com/data +```bash +# Path-based URL format (recommended) +curl http://localhost:3001/https://api.example.com/data + +# Query parameter format (legacy) +curl "http://localhost:3001/?url=https://api.example.com/data" + +# Enable caching with X-Cache header +curl -H "X-Cache: 1" http://localhost:3001/https://api.example.com/data ``` ## Features diff --git a/docker-compose.yml b/docker-compose.yml index e1c9f61..6a95a0a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,15 +1,10 @@ -version: '3.8' - services: nginx-cache-proxy: - image: docker.site.quack-lab.dev/nginx-cache-proxy:latest - expose: - - 3000 + # image: docker.site.quack-lab.dev/nginx-cache-proxy:latest + image: nginx:alpine + ports: + - "3001:3001" volumes: - - ./nginx_cache:/var/cache/nginx - healthcheck: - test: ["CMD-SHELL", "curl -f http://localhost:3000/health || exit 1"] - interval: 30s - timeout: 10s - retries: 3 - start_period: 10s + - ./nginx.conf:/etc/nginx/nginx.conf:ro + - ./nginx-data:/etc/nginx/data + restart: unless-stopped \ No newline at end of file diff --git a/nginx.conf b/nginx.conf index a276435..a772f99 100644 --- a/nginx.conf +++ b/nginx.conf @@ -1,26 +1,46 @@ +events { + worker_connections 1024; +} + http { - proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=api_cache:10m max_size=1g inactive=365d; + client_body_temp_path /etc/nginx/data/temp; + proxy_temp_path /etc/nginx/data/temp; + fastcgi_temp_path /etc/nginx/data/temp; + uwsgi_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; + + error_log /dev/stdout debug; + access_log /dev/stdout; + + # Use Docker's internal DNS + resolver 127.0.0.11 valid=10s; + server { - listen 3000; - location /health { - return 200 "healthy\n"; - add_header Content-Type text/plain; - } + listen 3001; location / { + if ($arg_url = "") { + return 400 "Missing url parameter"; + } + + proxy_pass $arg_url; + proxy_http_version 1.1; + proxy_set_header Host $proxy_host; + proxy_ssl_server_name on; + proxy_cache api_cache; proxy_cache_valid 200 365d; - # Only cache if X-Cache header is present - proxy_cache_bypass $http_x_cache_inverse; - proxy_no_cache $http_x_cache_inverse; - # Set inverse variable (cache disabled by default) - set $http_x_cache_inverse 1; - if ($http_x_cache) { - set $http_x_cache_inverse 0; + proxy_cache_bypass $http_x_cache_this_inverse; + proxy_no_cache $http_x_cache_this_inverse; + + set $http_x_cache_this_inverse 1; + if ($http_x_cache_this) { + set $http_x_cache_this_inverse 0; } - proxy_cache_key $scheme$proxy_host$uri$is_args$args; - proxy_pass $arg_url; - add_header X-Cache-Status $upstream_cache_status; + + add_header X-Cache-Status $upstream_cache_status always; } } -} +} \ No newline at end of file