Skip to content

๐Ÿš€ API KEY AUTHENTICATION IN NGINX REVERSE PROXY (END-TO-END)

API keys are commonly used for:

  • Protecting internal APIs

  • Allowing only specific clients to access an API

  • Blocking unauthorized calls

  • Securing microservices

NGINX can check API keys before forwarding the request to your backend.


๐Ÿ”ฅ 1. How API Key Authentication Works

Client โ†’ NGINX Reverse Proxy โ†’ Backend Server

NGINX checks:
    - Header:   X-API-KEY
    - or Query: ?api_key=

If key is valid โ†’ request is forwarded.
If key is invalid โ†’ 401 Unauthorized.


๐Ÿงฑ 2. Create a Secret API Key

Example:

API_KEY="my-super-secret-key-123"

You will verify this in NGINX.


โš™๏ธ 3. Reverse Proxy with API Key Authentication

Create or edit your config:

sudo nano /etc/nginx/sites-available/myapp

Paste this:

server {
    listen 80;
    server_name example.com;

    # Expected API key
    set $api_key "my-super-secret-key-123";

    location / {
        # Extract API key from header
        set $client_key $http_x_api_key;

        # If header is empty, check query ?api_key=
        if ($client_key = "") {
            set $client_key $arg_api_key;
        }

        # Validate
        if ($client_key != $api_key) {
            return 401;
        }

        # Forward request to backend
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

๐Ÿงช 4. Test the API Key

โŒ Without API Key:

curl http://example.com

Response:

401 Unauthorized

โœ… With API Key (Header):

curl -H "X-API-KEY: my-super-secret-key-123" http://example.com

โœ… With API Key (Query):

curl "http://example.com?api_key=my-super-secret-key-123"

๐Ÿ” 5. Hide API Key in Separate File (More Secure)

Store the key in its own file:

sudo nano /etc/nginx/keys/api_key.conf

Add:

set $api_key "my-super-secret-key-123";

Protect the file:

sudo chmod 600 /etc/nginx/keys/api_key.conf

Then include it:

include /etc/nginx/keys/api_key.conf;

location / {
    โ€ฆ
}

๐Ÿ›ก 6. Allow Only Specific Endpoints to Require API Key

Example: Protect /admin but not /public.

location /admin/ {
    if ($http_x_api_key != "my-super-secret-key-123") {
        return 401;
    }
    proxy_pass http://127.0.0.1:3000;
}

location /public/ {
    proxy_pass http://127.0.0.1:3000;
}

๐Ÿ“Œ 7. Return JSON Instead of Plain 401

if ($client_key != $api_key) {
    return 401 '{"error":"invalid api key"}';
}

Add proper headers:

add_header Content-Type application/json;

โšก 8. Reject Requests Missing API Key (No Proxy)

This saves backend resources.

if ($client_key = "") {
    return 400 '{"error":"missing api key"}';
}

๐Ÿงฐ 9. Advanced Method โ€” Using Map (Cleaner, No โ€œifโ€ inside location)

map $http_x_api_key $key_ok {
    default 0;
    "my-super-secret-key-123" 1;
}

server {
    listen 80;

    location / {
        if ($key_ok = 0) { return 401; }
        proxy_pass http://127.0.0.1:3000;
    }
}

๐Ÿ’ก 10. API Key Rotation (Multiple Keys Support)

map $http_x_api_key $key_ok {
    default 0;
    "keyA-123" 1;
    "keyB-456" 1;
    "keyC-789" 1;
}

Supports multiple clients.


๐Ÿš€ DONE! API Key Authentication Added to Reverse Proxy

You now have:

โœ” API Key verification
โœ” Header + query param support
โœ” Secure key storage
โœ” JSON error response
โœ” Multiple keys
โœ” Key rotation
โœ” Endpoint-based protection