Skip to content

Instantly share code, notes, and snippets.

@nginx-gists
Forked from lcrilly/api_backends.conf
Last active September 26, 2025 04:25
Show Gist options
  • Save nginx-gists/37ce65292a06219ff8d35d293c05e0b5 to your computer and use it in GitHub Desktop.
Save nginx-gists/37ce65292a06219ff8d35d293c05e0b5 to your computer and use it in GitHub Desktop.

Revisions

  1. nginx-gists revised this gist Dec 9, 2021. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions api_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -20,10 +20,10 @@ server {
    include api_conf.d/*.conf;

    # Error responses
    error_page 404 = @400; # Invalid paths are treated as bad requests
    proxy_intercept_errors on; # Do not send backend errors to the client
    include api_json_errors.conf; # API client friendly JSON error responses
    default_type application/json; # If no content-type then assume JSON
    error_page 404 = @400; # Treat invalid paths as bad requests
    proxy_intercept_errors on; # Do not send backend errors to client
    include api_json_errors.conf; # API client-friendly JSON errors
    default_type application/json; # If no content-type, assume JSON
    }

    # vim: syntax=nginx
  2. nginx-gists revised this gist Oct 12, 2021. 6 changed files with 8 additions and 6 deletions.
    3 changes: 2 additions & 1 deletion api_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,8 @@ include api_backends.conf;
    include api_keys.conf;

    server {
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a separate file
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a
    # separate file

    listen 443 ssl;
    server_name api.example.com;
    3 changes: 2 additions & 1 deletion api_gateway_apikey.conf
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,8 @@ include api_backends.conf;
    include api_keys.conf;

    server {
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a separate file
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a
    # separate file

    listen 443 ssl;
    server_name api.example.com;
    2 changes: 1 addition & 1 deletion warehouse_api_apikeys.conf
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Warehouse API
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    # Policy configuration here (authentication, rate limiting, logging...)
    #
    access_log /var/log/nginx/warehouse_api.log main;
    auth_request /_validate_apikey;
    2 changes: 1 addition & 1 deletion warehouse_api_precise.conf
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Warehouse API (precise definition)
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    # Policy configuration here (authentication, rate limiting, logging...)
    #
    access_log /var/log/nginx/warehouse_api.log main;

    2 changes: 1 addition & 1 deletion warehouse_api_rewrites.conf
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ rewrite ^/api/warehouse/inventory/item/price/(.*) /api/warehouse/pricing/$1;
    # Warehouse API
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    # Policy configuration here (authentication, rate limiting, logging...)
    #
    access_log /var/log/nginx/warehouse_api.log main;

    2 changes: 1 addition & 1 deletion warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Warehouse API
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    # Policy configuration here (authentication, rate limiting, logging...)
    #
    access_log /var/log/nginx/warehouse_api.log main;

  3. nginx-gists revised this gist Jan 20, 2021. 5 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  4. nginx-gists revised this gist Jan 20, 2021. 7 changed files with 114 additions and 250 deletions.
    33 changes: 0 additions & 33 deletions api_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -1,33 +0,0 @@
    log_format api_main '$remote_addr - $remote_user [$time_local] "$request"'
    '$status $body_bytes_sent "$http_referer" "$http_user_agent"'
    '"$http_x_forwarded_for" "$api_name"';

    include api_backends.conf;
    include api_keys.conf;

    server {
    set $api_name -; # Start with an undefined API name, each API will update this value
    access_log /var/log/nginx/api_access.log api_main; # Each API may also log to a separate file

    listen 443 ssl;
    server_name api.example.com;

    # TLS config
    ssl_certificate /etc/ssl/certs/api.example.com.crt;
    ssl_certificate_key /etc/ssl/private/api.example.com.key;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1.1 TLSv1.2;

    # API definitions, one per file
    include api_conf.d/*.conf;

    # Error responses
    error_page 404 = @400; # Invalid paths are treated as bad requests
    proxy_intercept_errors on; # Do not send backend errors to the client
    include api_json_errors.conf; # API client friendly JSON error responses
    default_type application/json; # If no content-type then assume JSON
    }

    # vim: syntax=nginx
    114 changes: 114 additions & 0 deletions oas2nginx.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    #!/usr/bin/env bash
    #
    # oas2nginx.sh (c) NGINX, Inc. [v0.5 13-Jan-2020] Liam Crilly <[email protected]>
    #
    # Converts OpenAPI/Swagger spec into nginx.conf snippet (server context) as per
    # https://www.nginx.com/blog/deploying-nginx-plus-as-an-api-gateway-part-1/
    # Requires shyaml for YAML processing: https://github.com/0k/shyaml

    # Defaults
    #
    BASEPATH=""
    PREFIX_PATH=""
    UPSTREAM="my_backend"

    if [ $# -lt 1 ]; then
    echo "USAGE: ${0##*/} [options] oas_spec.yaml"
    echo " Converts OpenAPI/Swagger spec into nginx.conf snippet"
    echo " Options:"
    echo " -b | --basepath <basePath> # Override OAS basePath / servers path"
    echo " -p | --prefix <prefix path> # Apply further prefix to basePath"
    echo " -u | --upstream <upstream name> # Specify upstream group (default: $UPSTREAM)"
    exit 1
    fi

    which shyaml > /dev/null
    if [ $? -ne 0 ]; then
    echo "${0##*/} ERROR: shyaml not found, see https://github.com/0k/shyaml"
    exit 1
    fi

    while [ $# -gt 1 ]; do
    case "$1" in
    "-b" | "--basepath")
    BASEPATH=$2
    shift; shift
    ;;
    "-p" | "--prefix")
    PREFIX_PATH=$2
    shift; shift
    ;;
    "-u" | "--upstream")
    UPSTREAM=$2
    shift; shift
    ;;
    *)
    echo "${0##*/} ERROR: Invalid command line option ($1)"
    exit 1
    ;;
    esac
    done

    if [ ! -f $1 ]; then
    echo "${0##*/} ERROR: Cannot open $1"
    exit 1
    fi

    if [ "$BASEPATH" == "" ]; then
    OAS_VERSION=`shyaml -q get-value openapi < $1`
    if [ $? -eq 0 ]; then
    echo "${0##*/} INFO: OpenAPI $OAS_VERSION" > /dev/stderr
    BASEPATH=`shyaml get-value servers < $1 2> /dev/null | grep url: | cut -f2- -d: | tail -1 | tr -d '[:blank:]'`
    else
    echo "${0##*/} INFO: OAS/Swagger v2" > /dev/stderr
    BASEPATH=`shyaml -q get-value basePath < $1`
    fi

    if [ "$BASEPATH" == "" ]; then
    echo "${0##*/}: WARNING: No basePath found in OAS" > /dev/stderr
    BASEPATH=/
    fi
    fi
    if [ "`echo $BASEPATH | grep -c http`" == "1" ]; then
    echo "${0##*/}: INFO: Stripping scheme and hostname from basepath URL" > /dev/stderr
    BASEPATH=/`echo $BASEPATH | cut -f4- -d/`
    fi
    echo "${0##*/}: INFO: Using basePath $BASEPATH"

    if [ "$PREFIX_PATH" != "" ]; then
    echo "# Strip prefix"
    echo "rewrite ^$PREFIX_PATH/\(.*\)$ \1 last;"
    echo ""
    fi

    echo "location $BASEPATH/ {" | sed -e 's_//_/_g'
    echo " # Policy section here"
    echo " #"
    echo " error_page 403 = @405;"
    echo ""

    for SWAGGER_PATH in `shyaml keys paths < $1`; do
    # Convert path templates to regular expressions
    URI=`echo $SWAGGER_PATH | sed -e "s/\({.*}\)/\[\^\/\]\+/g"`

    if [ "$SWAGGER_PATH" == "$URI" ]; then
    # Exact match when no path templates
    echo " location = $BASEPATH$URI {" | sed -e 's_//_/_g'
    else
    # Regex match
    echo " location ~ ^$BASEPATH$URI\$ {" | sed -e 's_//_/_g'
    fi

    ESCAPED_PATH=`echo $SWAGGER_PATH | sed -e 's/\./\\\./g'`
    METHODS=`shyaml keys paths.$ESCAPED_PATH < $1 | grep -v parameters | tr '\n' ' '`
    if [ "$METHODS" != "" ]; then
    echo " limit_except $METHODS{ deny all; }"
    fi

    echo " proxy_pass http://$UPSTREAM;"
    echo " }"
    done

    echo ""
    echo " return 404;"
    echo "}"
    113 changes: 0 additions & 113 deletions swagger2nginx.sh
    Original file line number Diff line number Diff line change
    @@ -1,113 +0,0 @@
    #!/usr/bin/env bash
    #
    # swagger2nginx.sh (c) NGINX, Inc. [v0.2 03-May-2018] Liam Crilly <[email protected]>
    #
    # Requires shyaml for YAML processing: https://github.com/0k/shyaml

    if [ $# -lt 1 ]; then
    echo "### USAGE: `basename $0` [options] swagger_file.yaml"
    echo "### Options:"
    echo "### -b | --basepath <basePath> # Override Swagger basePath"
    echo "### -l | --location # Create policy location (requires -u)"
    echo "### -n | --api-name <API name> # Override Swagger title"
    echo "### -p | --prefix <prefix path> # Apply prefix to basePath"
    echo "### -u | --upstream <upstream name> # Specify upstream group"
    exit 1
    fi

    which shyaml
    if [ $? -ne 0 ]; then
    echo "### `basename $0` ERROR: shyaml not found, see https://github.com/0k/shyaml"
    exit 1
    fi

    API_NAME=""
    DO_LOCATION=0
    BASEPATH=""
    PREFIX_PATH=""
    UPSTREAM=""
    while [ $# -gt 1 ]; do
    case "$1" in
    "-b" | "--basepath")
    BASEPATH=$2
    shift; shift
    ;;
    "-l" | "--location")
    DO_LOCATION=1
    shift
    ;;
    "-n" | "--api-name")
    API_NAME=$2
    shift; shift
    ;;
    "-p" | "--prefix")
    PREFIX_PATH=$2
    shift; shift
    ;;
    "-u" | "--upstream")
    UPSTREAM=$2
    shift; shift
    ;;
    *)
    echo "### `basename $0` ERROR: Invalid command line option ($1)"
    exit 1
    ;;
    esac
    done

    if [ $DO_LOCATION -eq 1 ] && [ "$UPSTREAM" == "" ]; then
    echo "### `basename $0` ERROR: Policy location requires upstream --upstream name"
    exit 1
    fi

    if [ ! -f $1 ]; then
    echo "### `basename $0` ERROR: Cannot open $1"
    exit 1
    fi

    if [ "$API_NAME" == "" ]; then
    # Convert title to NGINX-friendly API name
    API_NAME=`shyaml get-value info.title < $1 | tr '[:space:]' '_' | tr -cd '[:alnum:]_-' 2> /dev/null`
    if [ "$API_NAME" == "" ]; then
    echo "### `basename $0` ERROR: Swagger file has missing/invalid title for API name"
    exit 1
    fi
    fi

    if [ "$BASEPATH" == "" ]; then
    BASEPATH=`shyaml get-value basePath < $1 2> /dev/null`
    if [ "$BASEPATH" == "" ]; then
    echo "### `basename $0` ERROR: No basePath found in Swagger"
    exit 1
    fi
    fi
    BASEPATH=$PREFIX_PATH$BASEPATH

    for SWAGGER_PATH in `shyaml keys paths < $1`; do
    # Convert path templates to regular expressions
    URI=`echo $SWAGGER_PATH | sed -e "s/\({.*}\)/\[\^\/\]\*/g"`

    if [ "$SWAGGER_PATH" == "$URI" ]; then
    echo "location = $BASEPATH$URI {" # Exact match when no path templates
    else
    echo "location ~ ^$BASEPATH$URI\$ {" # Regex match
    fi

    METHODS=`shyaml keys paths.$SWAGGER_PATH < $1 | grep -v parameters | tr '\n' ' '`
    if [ "$METHODS" != "" ]; then
    echo " limit_except $METHODS{}"
    fi

    if [ "$UPSTREAM" != "" ]; then
    echo " set \$upstream $UPSTREAM;"
    fi

    echo " rewrite ^ /_$API_NAME last;"
    echo "}"
    done

    if [ $DO_LOCATION -eq 1 ]; then
    echo ""
    echo "location = /_$API_NAME {"
    echo " proxy_pass http://\$upstream\$request_uri;"
    echo "}"
    17 changes: 0 additions & 17 deletions warehouse_api_apikeys.conf
    Original file line number Diff line number Diff line change
    @@ -1,17 +0,0 @@
    # Policy section
    #
    location = /_warehouse {
    internal;
    set $api_name "Warehouse";

    if ($http_apikey = "") {
    return 401; # Unauthorized (please authenticate)
    }
    if ($api_client_name = "") {
    return 403; # Forbidden (invalid API key)
    }

    proxy_pass http://$upstream$request_uri;
    }

    # vim: syntax=nginx
    34 changes: 0 additions & 34 deletions warehouse_api_precise.conf
    Original file line number Diff line number Diff line change
    @@ -1,34 +0,0 @@
    # API definition (precise)
    #
    location = /api/warehouse/inventory { # Complete inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }

    location ~ ^/api/warehouse/inventory/shelf/[^/]*$ { # Shelf inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }

    location ~ ^/api/warehouse/inventory/shelf/[^/]*/box/[^/]*$ { # Box on shelf
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }

    location ~ ^/api/warehouse/pricing/[^/]*$ { # Price for specific item
    set $upstream pricing_service;
    rewrite ^ /_warehouse last;
    }

    # Policy section
    #
    location = /_warehouse {
    internal;
    set $api_name warehouse_api;

    # Policy configuration here (authentication, rate limiting, logging, more...)

    proxy_pass http://$upstream$request_uri;
    }

    # vim: syntax=nginx
    29 changes: 0 additions & 29 deletions warehouse_api_rewrites.conf
    Original file line number Diff line number Diff line change
    @@ -1,29 +0,0 @@
    # Rewrite rules
    #
    rewrite ^/api/warehouse/inventory/item/price/(.*) /api/warehouse/pricing/$1;

    # API definition
    #
    location /api/warehouse/inventory {
    set $upstream inventory_service;
    rewrite ^(.*)$ /_warehouse$1 last;
    }

    location /api/warehouse/pricing {
    set $upstream pricing_service;
    rewrite ^(.*)$ /_warehouse$1 last;
    }

    # Policy section
    #
    location /_warehouse {
    internal;
    set $api_name "Warehouse";

    # Policy configuration here (authentication, rate limiting, logging, more...)

    rewrite ^/_warehouse/(.*)$ /$1 break; # Remove /_warehouse prefix
    proxy_pass http://$upstream; # Proxy the rewritten URI
    }

    # vim: syntax=nginx
    24 changes: 0 additions & 24 deletions warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -1,24 +0,0 @@
    # API definition
    #
    location /api/warehouse/inventory {
    set $upstream warehouse_inventory;
    rewrite ^ /_warehouse last;
    }

    location /api/warehouse/pricing {
    set $upstream warehouse_pricing;
    rewrite ^ /_warehouse last;
    }

    # Policy section
    #
    location = /_warehouse {
    internal;
    set $api_name "Warehouse";

    # Policy configuration here (authentication, rate limiting, logging, more...)

    proxy_pass http://$upstream$request_uri;
    }

    # vim: syntax=nginx
  5. nginx-gists revised this gist Jan 20, 2021. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions api_gateway_apikey.conf
    Original file line number Diff line number Diff line change
    @@ -39,5 +39,4 @@ server {
    }
    }

    # vim: syntax=nginx
    oas2nginx.sh
    # vim: syntax=nginx
  6. nginx-gists revised this gist Jan 20, 2021. 6 changed files with 168 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions NEWapi_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    include api_backends.conf;
    include api_keys.conf;

    server {
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a separate file

    listen 443 ssl;
    server_name api.example.com;

    # TLS config
    ssl_certificate /etc/ssl/certs/api.example.com.crt;
    ssl_certificate_key /etc/ssl/private/api.example.com.key;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1.2 TLSv1.3;

    # API definitions, one per file
    include api_conf.d/*.conf;

    # Error responses
    error_page 404 = @400; # Invalid paths are treated as bad requests
    proxy_intercept_errors on; # Do not send backend errors to the client
    include api_json_errors.conf; # API client friendly JSON error responses
    default_type application/json; # If no content-type then assume JSON
    }

    # vim: syntax=nginx
    22 changes: 22 additions & 0 deletions NEWwarehouse_api_apikeys.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # Warehouse API
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;
    auth_request /_validate_apikey;

    # URI routing
    #
    location /api/warehouse/inventory {
    proxy_pass http://warehouse_inventory;
    }

    location /api/warehouse/pricing {
    proxy_pass http://warehouse_pricing;
    }

    return 404; # Catch-all
    }

    # vim: syntax=nginx
    29 changes: 29 additions & 0 deletions NEWwarehouse_api_precise.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    # Warehouse API (precise definition)
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;

    # URI routing
    #
    location = /api/warehouse/inventory { # Complete inventory
    proxy_pass http://warehouse_inventory;
    }

    location ~ ^/api/warehouse/inventory/shelf/[^/]+$ { # Shelf inventory
    proxy_pass http://warehouse_inventory;
    }

    location ~ ^/api/warehouse/inventory/shelf/[^/]+/box/[^/]+$ { # Box on shelf
    proxy_pass http://warehouse_inventory;
    }

    location ~ ^/api/warehouse/pricing/[^/]+$ { # Price for specific item
    proxy_pass http://warehouse_pricing;
    }

    return 404; # Catch-all
    }

    # vim: syntax=nginx
    25 changes: 25 additions & 0 deletions NEWwarehouse_api_rewrites.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # Rewrite rules
    #
    rewrite ^/api/warehouse/inventory/item/price/(.*) /api/warehouse/pricing/$1;

    # Warehouse API
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;

    # URI routing
    #
    location /api/warehouse/inventory {
    proxy_pass http://warehouse_inventory;
    }

    location /api/warehouse/pricing {
    proxy_pass http://warehouse_pricing;
    }

    return 404; # Catch-all
    }

    # vim: syntax=nginx
    21 changes: 21 additions & 0 deletions NEWwarehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # Warehouse API
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;

    # URI routing
    #
    location /api/warehouse/inventory {
    proxy_pass http://warehouse_inventory;
    }

    location /api/warehouse/pricing {
    proxy_pass http://warehouse_pricing;
    }

    return 404; # Catch-all
    }

    # vim: syntax=nginx
    43 changes: 43 additions & 0 deletions api_gateway_apikey.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    include api_backends.conf;
    include api_keys.conf;

    server {
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a separate file

    listen 443 ssl;
    server_name api.example.com;

    # TLS config
    ssl_certificate /etc/ssl/certs/api.example.com.crt;
    ssl_certificate_key /etc/ssl/private/api.example.com.key;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1.2 TLSv1.3;

    # API definitions, one per file
    include api_conf.d/*.conf;

    # Error responses
    error_page 404 = @400; # Invalid paths are treated as bad requests
    proxy_intercept_errors on; # Do not send backend errors to the client
    include api_json_errors.conf; # API client friendly JSON error responses
    default_type application/json; # If no content-type then assume JSON

    # API key validation
    location = /_validate_apikey {
    internal;

    if ($http_apikey = "") {
    return 401; # Unauthorized
    }
    if ($api_client_name = "") {
    return 403; # Forbidden
    }

    return 204; # OK (no content)
    }
    }

    # vim: syntax=nginx
    oas2nginx.sh
  7. nginx-gists revised this gist Jan 20, 2021. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions warehouse_api_apikeys.conf
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,4 @@ location = /_warehouse {
    proxy_pass http://$upstream$request_uri;
    }

    # vim: syntax=nginx

    return 404; # Catch-all
    }

    # vim: syntax=nginx
  8. nginx-gists revised this gist Jan 20, 2021. 8 changed files with 193 additions and 219 deletions.
    9 changes: 7 additions & 2 deletions api_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,13 @@
    log_format api_main '$remote_addr - $remote_user [$time_local] "$request"'
    '$status $body_bytes_sent "$http_referer" "$http_user_agent"'
    '"$http_x_forwarded_for" "$api_name"';

    include api_backends.conf;
    include api_keys.conf;

    server {
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a separate file
    set $api_name -; # Start with an undefined API name, each API will update this value
    access_log /var/log/nginx/api_access.log api_main; # Each API may also log to a separate file

    listen 443 ssl;
    server_name api.example.com;
    @@ -13,7 +18,7 @@ server {
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_protocols TLSv1.1 TLSv1.2;

    # API definitions, one per file
    include api_conf.d/*.conf;
    42 changes: 0 additions & 42 deletions api_gateway_apikey.conf
    Original file line number Diff line number Diff line change
    @@ -1,42 +0,0 @@
    include api_backends.conf;
    include api_keys.conf;

    server {
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a separate file

    listen 443 ssl;
    server_name api.example.com;

    # TLS config
    ssl_certificate /etc/ssl/certs/api.example.com.crt;
    ssl_certificate_key /etc/ssl/private/api.example.com.key;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1.2 TLSv1.3;

    # API definitions, one per file
    include api_conf.d/*.conf;

    # Error responses
    error_page 404 = @400; # Invalid paths are treated as bad requests
    proxy_intercept_errors on; # Do not send backend errors to the client
    include api_json_errors.conf; # API client friendly JSON error responses
    default_type application/json; # If no content-type then assume JSON

    # API key validation
    location = /_validate_apikey {
    internal;

    if ($http_apikey = "") {
    return 401; # Unauthorized
    }
    if ($api_client_name = "") {
    return 403; # Forbidden
    }

    return 204; # OK (no content)
    }
    }

    # vim: syntax=nginx
    114 changes: 0 additions & 114 deletions oas2nginx.sh
    Original file line number Diff line number Diff line change
    @@ -1,114 +0,0 @@
    #!/usr/bin/env bash
    #
    # oas2nginx.sh (c) NGINX, Inc. [v0.5 13-Jan-2020] Liam Crilly <[email protected]>
    #
    # Converts OpenAPI/Swagger spec into nginx.conf snippet (server context) as per
    # https://www.nginx.com/blog/deploying-nginx-plus-as-an-api-gateway-part-1/
    # Requires shyaml for YAML processing: https://github.com/0k/shyaml

    # Defaults
    #
    BASEPATH=""
    PREFIX_PATH=""
    UPSTREAM="my_backend"

    if [ $# -lt 1 ]; then
    echo "USAGE: ${0##*/} [options] oas_spec.yaml"
    echo " Converts OpenAPI/Swagger spec into nginx.conf snippet"
    echo " Options:"
    echo " -b | --basepath <basePath> # Override OAS basePath / servers path"
    echo " -p | --prefix <prefix path> # Apply further prefix to basePath"
    echo " -u | --upstream <upstream name> # Specify upstream group (default: $UPSTREAM)"
    exit 1
    fi

    which shyaml > /dev/null
    if [ $? -ne 0 ]; then
    echo "${0##*/} ERROR: shyaml not found, see https://github.com/0k/shyaml"
    exit 1
    fi

    while [ $# -gt 1 ]; do
    case "$1" in
    "-b" | "--basepath")
    BASEPATH=$2
    shift; shift
    ;;
    "-p" | "--prefix")
    PREFIX_PATH=$2
    shift; shift
    ;;
    "-u" | "--upstream")
    UPSTREAM=$2
    shift; shift
    ;;
    *)
    echo "${0##*/} ERROR: Invalid command line option ($1)"
    exit 1
    ;;
    esac
    done

    if [ ! -f $1 ]; then
    echo "${0##*/} ERROR: Cannot open $1"
    exit 1
    fi

    if [ "$BASEPATH" == "" ]; then
    OAS_VERSION=`shyaml -q get-value openapi < $1`
    if [ $? -eq 0 ]; then
    echo "${0##*/} INFO: OpenAPI $OAS_VERSION" > /dev/stderr
    BASEPATH=`shyaml get-value servers < $1 2> /dev/null | grep url: | cut -f2- -d: | tail -1 | tr -d '[:blank:]'`
    else
    echo "${0##*/} INFO: OAS/Swagger v2" > /dev/stderr
    BASEPATH=`shyaml -q get-value basePath < $1`
    fi

    if [ "$BASEPATH" == "" ]; then
    echo "${0##*/}: WARNING: No basePath found in OAS" > /dev/stderr
    BASEPATH=/
    fi
    fi
    if [ "`echo $BASEPATH | grep -c http`" == "1" ]; then
    echo "${0##*/}: INFO: Stripping scheme and hostname from basepath URL" > /dev/stderr
    BASEPATH=/`echo $BASEPATH | cut -f4- -d/`
    fi
    echo "${0##*/}: INFO: Using basePath $BASEPATH"

    if [ "$PREFIX_PATH" != "" ]; then
    echo "# Strip prefix"
    echo "rewrite ^$PREFIX_PATH/\(.*\)$ \1 last;"
    echo ""
    fi

    echo "location $BASEPATH/ {" | sed -e 's_//_/_g'
    echo " # Policy section here"
    echo " #"
    echo " error_page 403 = @405;"
    echo ""

    for SWAGGER_PATH in `shyaml keys paths < $1`; do
    # Convert path templates to regular expressions
    URI=`echo $SWAGGER_PATH | sed -e "s/\({.*}\)/\[\^\/\]\+/g"`

    if [ "$SWAGGER_PATH" == "$URI" ]; then
    # Exact match when no path templates
    echo " location = $BASEPATH$URI {" | sed -e 's_//_/_g'
    else
    # Regex match
    echo " location ~ ^$BASEPATH$URI\$ {" | sed -e 's_//_/_g'
    fi

    ESCAPED_PATH=`echo $SWAGGER_PATH | sed -e 's/\./\\\./g'`
    METHODS=`shyaml keys paths.$ESCAPED_PATH < $1 | grep -v parameters | tr '\n' ' '`
    if [ "$METHODS" != "" ]; then
    echo " limit_except $METHODS{ deny all; }"
    fi

    echo " proxy_pass http://$UPSTREAM;"
    echo " }"
    done

    echo ""
    echo " return 404;"
    echo "}"
    113 changes: 113 additions & 0 deletions swagger2nginx.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    #!/usr/bin/env bash
    #
    # swagger2nginx.sh (c) NGINX, Inc. [v0.2 03-May-2018] Liam Crilly <[email protected]>
    #
    # Requires shyaml for YAML processing: https://github.com/0k/shyaml

    if [ $# -lt 1 ]; then
    echo "### USAGE: `basename $0` [options] swagger_file.yaml"
    echo "### Options:"
    echo "### -b | --basepath <basePath> # Override Swagger basePath"
    echo "### -l | --location # Create policy location (requires -u)"
    echo "### -n | --api-name <API name> # Override Swagger title"
    echo "### -p | --prefix <prefix path> # Apply prefix to basePath"
    echo "### -u | --upstream <upstream name> # Specify upstream group"
    exit 1
    fi

    which shyaml
    if [ $? -ne 0 ]; then
    echo "### `basename $0` ERROR: shyaml not found, see https://github.com/0k/shyaml"
    exit 1
    fi

    API_NAME=""
    DO_LOCATION=0
    BASEPATH=""
    PREFIX_PATH=""
    UPSTREAM=""
    while [ $# -gt 1 ]; do
    case "$1" in
    "-b" | "--basepath")
    BASEPATH=$2
    shift; shift
    ;;
    "-l" | "--location")
    DO_LOCATION=1
    shift
    ;;
    "-n" | "--api-name")
    API_NAME=$2
    shift; shift
    ;;
    "-p" | "--prefix")
    PREFIX_PATH=$2
    shift; shift
    ;;
    "-u" | "--upstream")
    UPSTREAM=$2
    shift; shift
    ;;
    *)
    echo "### `basename $0` ERROR: Invalid command line option ($1)"
    exit 1
    ;;
    esac
    done

    if [ $DO_LOCATION -eq 1 ] && [ "$UPSTREAM" == "" ]; then
    echo "### `basename $0` ERROR: Policy location requires upstream --upstream name"
    exit 1
    fi

    if [ ! -f $1 ]; then
    echo "### `basename $0` ERROR: Cannot open $1"
    exit 1
    fi

    if [ "$API_NAME" == "" ]; then
    # Convert title to NGINX-friendly API name
    API_NAME=`shyaml get-value info.title < $1 | tr '[:space:]' '_' | tr -cd '[:alnum:]_-' 2> /dev/null`
    if [ "$API_NAME" == "" ]; then
    echo "### `basename $0` ERROR: Swagger file has missing/invalid title for API name"
    exit 1
    fi
    fi

    if [ "$BASEPATH" == "" ]; then
    BASEPATH=`shyaml get-value basePath < $1 2> /dev/null`
    if [ "$BASEPATH" == "" ]; then
    echo "### `basename $0` ERROR: No basePath found in Swagger"
    exit 1
    fi
    fi
    BASEPATH=$PREFIX_PATH$BASEPATH

    for SWAGGER_PATH in `shyaml keys paths < $1`; do
    # Convert path templates to regular expressions
    URI=`echo $SWAGGER_PATH | sed -e "s/\({.*}\)/\[\^\/\]\*/g"`

    if [ "$SWAGGER_PATH" == "$URI" ]; then
    echo "location = $BASEPATH$URI {" # Exact match when no path templates
    else
    echo "location ~ ^$BASEPATH$URI\$ {" # Regex match
    fi

    METHODS=`shyaml keys paths.$SWAGGER_PATH < $1 | grep -v parameters | tr '\n' ' '`
    if [ "$METHODS" != "" ]; then
    echo " limit_except $METHODS{}"
    fi

    if [ "$UPSTREAM" != "" ]; then
    echo " set \$upstream $UPSTREAM;"
    fi

    echo " rewrite ^ /_$API_NAME last;"
    echo "}"
    done

    if [ $DO_LOCATION -eq 1 ]; then
    echo ""
    echo "location = /_$API_NAME {"
    echo " proxy_pass http://\$upstream\$request_uri;"
    echo "}"
    26 changes: 13 additions & 13 deletions warehouse_api_apikeys.conf
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,21 @@
    # Warehouse API
    # Policy section
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;
    auth_request /_validate_apikey;
    location = /_warehouse {
    internal;
    set $api_name "Warehouse";

    # URI routing
    #
    location /api/warehouse/inventory {
    proxy_pass http://warehouse_inventory;
    if ($http_apikey = "") {
    return 401; # Unauthorized (please authenticate)
    }

    location /api/warehouse/pricing {
    proxy_pass http://warehouse_pricing;
    if ($api_client_name = "") {
    return 403; # Forbidden (invalid API key)
    }

    proxy_pass http://$upstream$request_uri;
    }

    # vim: syntax=nginx

    return 404; # Catch-all
    }

    45 changes: 25 additions & 20 deletions warehouse_api_precise.conf
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,34 @@
    # Warehouse API (precise definition)
    # API definition (precise)
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;
    location = /api/warehouse/inventory { # Complete inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }

    location ~ ^/api/warehouse/inventory/shelf/[^/]*$ { # Shelf inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }

    # URI routing
    #
    location = /api/warehouse/inventory { # Complete inventory
    proxy_pass http://warehouse_inventory;
    }
    location ~ ^/api/warehouse/inventory/shelf/[^/]*/box/[^/]*$ { # Box on shelf
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }

    location ~ ^/api/warehouse/inventory/shelf/[^/]+$ { # Shelf inventory
    proxy_pass http://warehouse_inventory;
    }
    location ~ ^/api/warehouse/pricing/[^/]*$ { # Price for specific item
    set $upstream pricing_service;
    rewrite ^ /_warehouse last;
    }

    location ~ ^/api/warehouse/inventory/shelf/[^/]+/box/[^/]+$ { # Box on shelf
    proxy_pass http://warehouse_inventory;
    }
    # Policy section
    #
    location = /_warehouse {
    internal;
    set $api_name warehouse_api;

    location ~ ^/api/warehouse/pricing/[^/]+$ { # Price for specific item
    proxy_pass http://warehouse_pricing;
    }
    # Policy configuration here (authentication, rate limiting, logging, more...)

    return 404; # Catch-all
    proxy_pass http://$upstream$request_uri;
    }

    # vim: syntax=nginx
    32 changes: 18 additions & 14 deletions warehouse_api_rewrites.conf
    Original file line number Diff line number Diff line change
    @@ -2,24 +2,28 @@
    #
    rewrite ^/api/warehouse/inventory/item/price/(.*) /api/warehouse/pricing/$1;

    # Warehouse API
    # API definition
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;
    location /api/warehouse/inventory {
    set $upstream inventory_service;
    rewrite ^(.*)$ /_warehouse$1 last;
    }

    location /api/warehouse/pricing {
    set $upstream pricing_service;
    rewrite ^(.*)$ /_warehouse$1 last;
    }

    # URI routing
    #
    location /api/warehouse/inventory {
    proxy_pass http://warehouse_inventory;
    }
    # Policy section
    #
    location /_warehouse {
    internal;
    set $api_name "Warehouse";

    location /api/warehouse/pricing {
    proxy_pass http://warehouse_pricing;
    }
    # Policy configuration here (authentication, rate limiting, logging, more...)

    return 404; # Catch-all
    rewrite ^/_warehouse/(.*)$ /$1 break; # Remove /_warehouse prefix
    proxy_pass http://$upstream; # Proxy the rewritten URI
    }

    # vim: syntax=nginx
    31 changes: 17 additions & 14 deletions warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,24 @@
    # Warehouse API
    # API definition
    #
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;
    location /api/warehouse/inventory {
    set $upstream warehouse_inventory;
    rewrite ^ /_warehouse last;
    }

    location /api/warehouse/pricing {
    set $upstream warehouse_pricing;
    rewrite ^ /_warehouse last;
    }

    # URI routing
    #
    location /api/warehouse/inventory {
    proxy_pass http://warehouse_inventory;
    }
    # Policy section
    #
    location = /_warehouse {
    internal;
    set $api_name "Warehouse";

    location /api/warehouse/pricing {
    proxy_pass http://warehouse_pricing;
    }
    # Policy configuration here (authentication, rate limiting, logging, more...)

    return 404; # Catch-all
    proxy_pass http://$upstream$request_uri;
    }

    # vim: syntax=nginx
  9. nginx-gists revised this gist Jan 19, 2021. 8 changed files with 220 additions and 203 deletions.
    9 changes: 2 additions & 7 deletions api_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,8 @@
    log_format api_main '$remote_addr - $remote_user [$time_local] "$request"'
    '$status $body_bytes_sent "$http_referer" "$http_user_agent"'
    '"$http_x_forwarded_for" "$api_name"';

    include api_backends.conf;
    include api_keys.conf;

    server {
    set $api_name -; # Start with an undefined API name, each API will update this value
    access_log /var/log/nginx/api_access.log api_main; # Each API may also log to a separate file
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a separate file

    listen 443 ssl;
    server_name api.example.com;
    @@ -18,7 +13,7 @@ server {
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_protocols TLSv1.2 TLSv1.3;

    # API definitions, one per file
    include api_conf.d/*.conf;
    42 changes: 42 additions & 0 deletions api_gateway_apikey.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    include api_backends.conf;
    include api_keys.conf;

    server {
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a separate file

    listen 443 ssl;
    server_name api.example.com;

    # TLS config
    ssl_certificate /etc/ssl/certs/api.example.com.crt;
    ssl_certificate_key /etc/ssl/private/api.example.com.key;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1.2 TLSv1.3;

    # API definitions, one per file
    include api_conf.d/*.conf;

    # Error responses
    error_page 404 = @400; # Invalid paths are treated as bad requests
    proxy_intercept_errors on; # Do not send backend errors to the client
    include api_json_errors.conf; # API client friendly JSON error responses
    default_type application/json; # If no content-type then assume JSON

    # API key validation
    location = /_validate_apikey {
    internal;

    if ($http_apikey = "") {
    return 401; # Unauthorized
    }
    if ($api_client_name = "") {
    return 403; # Forbidden
    }

    return 204; # OK (no content)
    }
    }

    # vim: syntax=nginx
    114 changes: 114 additions & 0 deletions oas2nginx.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    #!/usr/bin/env bash
    #
    # oas2nginx.sh (c) NGINX, Inc. [v0.5 13-Jan-2020] Liam Crilly <[email protected]>
    #
    # Converts OpenAPI/Swagger spec into nginx.conf snippet (server context) as per
    # https://www.nginx.com/blog/deploying-nginx-plus-as-an-api-gateway-part-1/
    # Requires shyaml for YAML processing: https://github.com/0k/shyaml

    # Defaults
    #
    BASEPATH=""
    PREFIX_PATH=""
    UPSTREAM="my_backend"

    if [ $# -lt 1 ]; then
    echo "USAGE: ${0##*/} [options] oas_spec.yaml"
    echo " Converts OpenAPI/Swagger spec into nginx.conf snippet"
    echo " Options:"
    echo " -b | --basepath <basePath> # Override OAS basePath / servers path"
    echo " -p | --prefix <prefix path> # Apply further prefix to basePath"
    echo " -u | --upstream <upstream name> # Specify upstream group (default: $UPSTREAM)"
    exit 1
    fi

    which shyaml > /dev/null
    if [ $? -ne 0 ]; then
    echo "${0##*/} ERROR: shyaml not found, see https://github.com/0k/shyaml"
    exit 1
    fi

    while [ $# -gt 1 ]; do
    case "$1" in
    "-b" | "--basepath")
    BASEPATH=$2
    shift; shift
    ;;
    "-p" | "--prefix")
    PREFIX_PATH=$2
    shift; shift
    ;;
    "-u" | "--upstream")
    UPSTREAM=$2
    shift; shift
    ;;
    *)
    echo "${0##*/} ERROR: Invalid command line option ($1)"
    exit 1
    ;;
    esac
    done

    if [ ! -f $1 ]; then
    echo "${0##*/} ERROR: Cannot open $1"
    exit 1
    fi

    if [ "$BASEPATH" == "" ]; then
    OAS_VERSION=`shyaml -q get-value openapi < $1`
    if [ $? -eq 0 ]; then
    echo "${0##*/} INFO: OpenAPI $OAS_VERSION" > /dev/stderr
    BASEPATH=`shyaml get-value servers < $1 2> /dev/null | grep url: | cut -f2- -d: | tail -1 | tr -d '[:blank:]'`
    else
    echo "${0##*/} INFO: OAS/Swagger v2" > /dev/stderr
    BASEPATH=`shyaml -q get-value basePath < $1`
    fi

    if [ "$BASEPATH" == "" ]; then
    echo "${0##*/}: WARNING: No basePath found in OAS" > /dev/stderr
    BASEPATH=/
    fi
    fi
    if [ "`echo $BASEPATH | grep -c http`" == "1" ]; then
    echo "${0##*/}: INFO: Stripping scheme and hostname from basepath URL" > /dev/stderr
    BASEPATH=/`echo $BASEPATH | cut -f4- -d/`
    fi
    echo "${0##*/}: INFO: Using basePath $BASEPATH"

    if [ "$PREFIX_PATH" != "" ]; then
    echo "# Strip prefix"
    echo "rewrite ^$PREFIX_PATH/\(.*\)$ \1 last;"
    echo ""
    fi

    echo "location $BASEPATH/ {" | sed -e 's_//_/_g'
    echo " # Policy section here"
    echo " #"
    echo " error_page 403 = @405;"
    echo ""

    for SWAGGER_PATH in `shyaml keys paths < $1`; do
    # Convert path templates to regular expressions
    URI=`echo $SWAGGER_PATH | sed -e "s/\({.*}\)/\[\^\/\]\+/g"`

    if [ "$SWAGGER_PATH" == "$URI" ]; then
    # Exact match when no path templates
    echo " location = $BASEPATH$URI {" | sed -e 's_//_/_g'
    else
    # Regex match
    echo " location ~ ^$BASEPATH$URI\$ {" | sed -e 's_//_/_g'
    fi

    ESCAPED_PATH=`echo $SWAGGER_PATH | sed -e 's/\./\\\./g'`
    METHODS=`shyaml keys paths.$ESCAPED_PATH < $1 | grep -v parameters | tr '\n' ' '`
    if [ "$METHODS" != "" ]; then
    echo " limit_except $METHODS{ deny all; }"
    fi

    echo " proxy_pass http://$UPSTREAM;"
    echo " }"
    done

    echo ""
    echo " return 404;"
    echo "}"
    114 changes: 0 additions & 114 deletions swagger2nginx.sh
    Original file line number Diff line number Diff line change
    @@ -1,114 +0,0 @@
    #!/usr/bin/env bash
    #
    # swagger2nginx.sh (c) NGINX, Inc. [v0.2 03-May-2018] Liam Crilly <[email protected]>
    #
    # Requires shyaml for YAML processing: https://github.com/0k/shyaml

    if [ $# -lt 1 ]; then
    echo "### USAGE: `basename $0` [options] swagger_file.yaml"
    echo "### Options:"
    echo "### -b | --basepath <basePath> # Override Swagger basePath"
    echo "### -l | --location # Create policy location (requires -u)"
    echo "### -n | --api-name <API name> # Override Swagger title"
    echo "### -p | --prefix <prefix path> # Apply prefix to basePath"
    echo "### -u | --upstream <upstream name> # Specify upstream group"
    exit 1
    fi

    which shyaml
    if [ $? -ne 0 ]; then
    echo "### `basename $0` ERROR: shyaml not found, see https://github.com/0k/shyaml"
    exit 1
    fi

    API_NAME=""
    DO_LOCATION=0
    BASEPATH=""
    PREFIX_PATH=""
    UPSTREAM=""
    while [ $# -gt 1 ]; do
    case "$1" in
    "-b" | "--basepath")
    BASEPATH=$2
    shift; shift
    ;;
    "-l" | "--location")
    DO_LOCATION=1
    shift
    ;;
    "-n" | "--api-name")
    API_NAME=$2
    shift; shift
    ;;
    "-p" | "--prefix")
    PREFIX_PATH=$2
    shift; shift
    ;;
    "-u" | "--upstream")
    UPSTREAM=$2
    shift; shift
    ;;
    *)
    echo "### `basename $0` ERROR: Invalid command line option ($1)"
    exit 1
    ;;
    esac
    done

    if [ $DO_LOCATION -eq 1 ] && [ "$UPSTREAM" == "" ]; then
    echo "### `basename $0` ERROR: Policy location requires upstream --upstream name"
    exit 1
    fi

    if [ ! -f $1 ]; then
    echo "### `basename $0` ERROR: Cannot open $1"
    exit 1
    fi

    if [ "$API_NAME" == "" ]; then
    # Convert title to NGINX-friendly API name
    API_NAME=`shyaml get-value info.title < $1 | tr '[:space:]' '_' | tr -cd '[:alnum:]_-' 2> /dev/null`
    if [ "$API_NAME" == "" ]; then
    echo "### `basename $0` ERROR: Swagger file has missing/invalid title for API name"
    exit 1
    fi
    fi

    if [ "$BASEPATH" == "" ]; then
    BASEPATH=`shyaml get-value basePath < $1 2> /dev/null`
    if [ "$BASEPATH" == "" ]; then
    echo "### `basename $0` ERROR: No basePath found in Swagger"
    exit 1
    fi
    fi
    BASEPATH=$PREFIX_PATH$BASEPATH

    for SWAGGER_PATH in `shyaml keys paths < $1`; do
    # Convert path templates to regular expressions
    URI=`echo $SWAGGER_PATH | sed -e "s/\({.*}\)/\[\^\/\]\*/g"`

    if [ "$SWAGGER_PATH" == "$URI" ]; then
    echo "location = $BASEPATH$URI {" # Exact match when no path templates
    else
    echo "location ~ ^$BASEPATH$URI\$ {" # Regex match
    fi

    METHODS=`shyaml keys paths.$SWAGGER_PATH < $1 | grep -v parameters | tr '\n' ' '`
    if [ "$METHODS" != "" ]; then
    echo " limit_except $METHODS{}"
    fi

    if [ "$UPSTREAM" != "" ]; then
    echo " set \$upstream $UPSTREAM;"
    fi

    echo " rewrite ^ /_$API_NAME last;"
    echo "}"
    done

    if [ $DO_LOCATION -eq 1 ]; then
    echo ""
    echo "location = /_$API_NAME {"
    echo " proxy_pass http://\$upstream\$request_uri;"
    echo "}"
    fi
    36 changes: 14 additions & 22 deletions warehouse_api_apikeys.conf
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,22 @@
    # API definition
    # Warehouse API
    #
    location /api/warehouse/pricing {
    limit_except GET POST {}
    set $upstream pricing_service;
    rewrite ^ /_warehouse last;
    }
    location /api/warehouse/inventory {
    limit_except GET {}
    set $upstream inventory_service;
    rewrite ^(.*)$ /_warehouse$1 last;
    }
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;
    auth_request /_validate_apikey;

    # Policy section
    #
    location = /_warehouse {
    internal;
    set $api_name "Warehouse";

    if ($http_apikey = "") {
    return 401; # Unauthorized (please authenticate)
    # URI routing
    #
    location /api/warehouse/inventory {
    proxy_pass http://warehouse_inventory;
    }
    if ($api_client_name = "") {
    return 403; # Forbidden (invalid API key)

    location /api/warehouse/pricing {
    proxy_pass http://warehouse_pricing;
    }

    proxy_pass http://$upstream$request_uri;
    return 404; # Catch-all
    }

    # vim: syntax=nginx
    45 changes: 20 additions & 25 deletions warehouse_api_precise.conf
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,29 @@
    # API definition (precise)
    # Warehouse API (precise definition)
    #
    location = /api/warehouse/inventory { # Complete inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }

    location ~ ^/api/warehouse/inventory/shelf/[^/]*$ { # Shelf inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;

    location ~ ^/api/warehouse/inventory/shelf/[^/]*/box/[^/]*$ { # Box on shelf
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }
    # URI routing
    #
    location = /api/warehouse/inventory { # Complete inventory
    proxy_pass http://warehouse_inventory;
    }

    location ~ ^/api/warehouse/pricing/[^/]*$ { # Price for specific item
    set $upstream pricing_service;
    rewrite ^ /_warehouse last;
    }
    location ~ ^/api/warehouse/inventory/shelf/[^/]+$ { # Shelf inventory
    proxy_pass http://warehouse_inventory;
    }

    # Policy section
    #
    location = /_warehouse {
    internal;
    set $api_name warehouse_api;
    location ~ ^/api/warehouse/inventory/shelf/[^/]+/box/[^/]+$ { # Box on shelf
    proxy_pass http://warehouse_inventory;
    }

    # Policy configuration here (authentication, rate limiting, logging, more...)
    location ~ ^/api/warehouse/pricing/[^/]+$ { # Price for specific item
    proxy_pass http://warehouse_pricing;
    }

    proxy_pass http://$upstream$request_uri;
    return 404; # Catch-all
    }

    # vim: syntax=nginx
    32 changes: 14 additions & 18 deletions warehouse_api_rewrites.conf
    Original file line number Diff line number Diff line change
    @@ -2,28 +2,24 @@
    #
    rewrite ^/api/warehouse/inventory/item/price/(.*) /api/warehouse/pricing/$1;

    # API definition
    # Warehouse API
    #
    location /api/warehouse/inventory {
    set $upstream inventory_service;
    rewrite ^(.*)$ /_warehouse$1 last;
    }

    location /api/warehouse/pricing {
    set $upstream pricing_service;
    rewrite ^(.*)$ /_warehouse$1 last;
    }
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;

    # Policy section
    #
    location /_warehouse {
    internal;
    set $api_name "Warehouse";
    # URI routing
    #
    location /api/warehouse/inventory {
    proxy_pass http://warehouse_inventory;
    }

    # Policy configuration here (authentication, rate limiting, logging, more...)
    location /api/warehouse/pricing {
    proxy_pass http://warehouse_pricing;
    }

    rewrite ^/_warehouse/(.*)$ /$1 break; # Remove /_warehouse prefix
    proxy_pass http://$upstream; # Proxy the rewritten URI
    return 404; # Catch-all
    }

    # vim: syntax=nginx
    31 changes: 14 additions & 17 deletions warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,21 @@
    # API definition
    # Warehouse API
    #
    location /api/warehouse/inventory {
    set $upstream warehouse_inventory;
    rewrite ^ /_warehouse last;
    }

    location /api/warehouse/pricing {
    set $upstream warehouse_pricing;
    rewrite ^ /_warehouse last;
    }
    location /api/warehouse/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;

    # Policy section
    #
    location = /_warehouse {
    internal;
    set $api_name "Warehouse";
    # URI routing
    #
    location /api/warehouse/inventory {
    proxy_pass http://warehouse_inventory;
    }

    # Policy configuration here (authentication, rate limiting, logging, more...)
    location /api/warehouse/pricing {
    proxy_pass http://warehouse_pricing;
    }

    proxy_pass http://$upstream$request_uri;
    return 404; # Catch-all
    }

    # vim: syntax=nginx
  10. nginx-gists revised this gist Jun 21, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion warehouse_api_rewrites.conf
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ location /api/warehouse/inventory {

    location /api/warehouse/pricing {
    set $upstream pricing_service;
    rewrite ^(.*) /_warehouse$1 last;
    rewrite ^(.*)$ /_warehouse$1 last;
    }

    # Policy section
  11. nginx-gists revised this gist May 25, 2018. No changes.
  12. nginx-gists revised this gist May 16, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions api_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ log_format api_main '$remote_addr - $remote_user [$time_local] "$request"'
    '$status $body_bytes_sent "$http_referer" "$http_user_agent"'
    '"$http_x_forwarded_for" "$api_name"';

    include api_endpoints.conf;
    include api_backends.conf;
    include api_keys.conf;

    server {
    @@ -25,7 +25,7 @@ server {

    # Error responses
    error_page 404 = @400; # Invalid paths are treated as bad requests
    proxy_intercept_errors on; # Do not send endpoint errors to the client
    proxy_intercept_errors on; # Do not send backend errors to the client
    include api_json_errors.conf; # API client friendly JSON error responses
    default_type application/json; # If no content-type then assume JSON
    }
  13. nginx-gists renamed this gist May 16, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  14. nginx-gists revised this gist May 15, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion api_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    log_format api_main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$api_name"';
    log_format api_main '$remote_addr - $remote_user [$time_local] "$request"'
    '$status $body_bytes_sent "$http_referer" "$http_user_agent"'
    '"$http_x_forwarded_for" "$api_name"';

    include api_endpoints.conf;
    include api_keys.conf;
  15. nginx-gists revised this gist May 14, 2018. 3 changed files with 5 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions warehouse_api_precise.conf
    Original file line number Diff line number Diff line change
    @@ -4,14 +4,17 @@ location = /api/warehouse/inventory { # Complete inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }

    location ~ ^/api/warehouse/inventory/shelf/[^/]*$ { # Shelf inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }

    location ~ ^/api/warehouse/inventory/shelf/[^/]*/box/[^/]*$ { # Box on shelf
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }

    location ~ ^/api/warehouse/pricing/[^/]*$ { # Price for specific item
    set $upstream pricing_service;
    rewrite ^ /_warehouse last;
    1 change: 1 addition & 0 deletions warehouse_api_rewrites.conf
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ location /api/warehouse/inventory {
    set $upstream inventory_service;
    rewrite ^(.*)$ /_warehouse$1 last;
    }

    location /api/warehouse/pricing {
    set $upstream pricing_service;
    rewrite ^(.*) /_warehouse$1 last;
    1 change: 1 addition & 0 deletions warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@ location /api/warehouse/inventory {
    set $upstream warehouse_inventory;
    rewrite ^ /_warehouse last;
    }

    location /api/warehouse/pricing {
    set $upstream warehouse_pricing;
    rewrite ^ /_warehouse last;
  16. @lcrilly lcrilly revised this gist May 11, 2018. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions api_json_errors.conf
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,15 @@ location @426 { return 426 '{"status":426,"message":"HTTP request was sent to HT
    error_page 429 = @429;
    location @429 { return 429 '{"status":429,"message":"API rate limit exceeded"}\n'; }

    error_page 495 = @495;
    location @495 { return 495 '{"status":495,"message":"Client certificate authentication error"}\n'; }

    error_page 496 = @496;
    location @496 { return 496 '{"status":496,"message":"Client certificate not presented"}\n'; }

    error_page 497 = @497;
    location @497 { return 497 '{"status":497,"message":"HTTP request was sent to mutual TLS port"}\n'; }

    error_page 500 = @500;
    location @500 { return 500 '{"status":500,"message":"Server error"}\n'; }

  17. @lcrilly lcrilly revised this gist May 3, 2018. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions warehouse_api_apikeys.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    # API definition
    #
    location /api/warehouse/pricing {
    limit_except GET POST {}
    set $upstream pricing_service;
    rewrite ^ /_warehouse last;
    }
    location /api/warehouse/inventory {
    limit_except GET {}
    set $upstream inventory_service;
    rewrite ^(.*)$ /_warehouse$1 last;
    }

    # Policy section
    #
    location = /_warehouse {
    internal;
    set $api_name "Warehouse";

    if ($http_apikey = "") {
    return 401; # Unauthorized (please authenticate)
    }
    if ($api_client_name = "") {
    return 403; # Forbidden (invalid API key)
    }

    proxy_pass http://$upstream$request_uri;
    }

    # vim: syntax=nginx
  18. @lcrilly lcrilly revised this gist May 3, 2018. 1 changed file with 18 additions and 5 deletions.
    23 changes: 18 additions & 5 deletions swagger2nginx.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,26 @@
    #!/usr/bin/env bash
    #
    # swagger2nginx.sh (c) NGINX, Inc. [v0.1 02-May-2018] Liam Crilly <[email protected]>
    # swagger2nginx.sh (c) NGINX, Inc. [v0.2 03-May-2018] Liam Crilly <[email protected]>
    #
    # Requires shyaml for YAML processing: https://github.com/0k/shyaml

    if [ $# -lt 1 ]; then
    echo "### USAGE: `basename $0` [options] <swagger_file.yaml>"
    echo "### USAGE: `basename $0` [options] swagger_file.yaml"
    echo "### Options:"
    echo "### -b | --basepath <basePath> # Override Swagger basePath"
    echo "### -l | --location # Create policy location"
    echo "### -l | --location # Create policy location (requires -u)"
    echo "### -n | --api-name <API name> # Override Swagger title"
    echo "### -p | --prefix <prefix path> # Apply prefix to basePath"
    echo "### -u | --upstream <upstream name> # Specify upstream group"
    exit 1
    fi

    which shyaml
    if [ $? -ne 0 ]; then
    echo "### `basename $0` ERROR: shyaml not found, see https://github.com/0k/shyaml"
    exit 1
    fi

    API_NAME=""
    DO_LOCATION=0
    BASEPATH=""
    @@ -47,6 +55,11 @@ while [ $# -gt 1 ]; do
    esac
    done

    if [ $DO_LOCATION -eq 1 ] && [ "$UPSTREAM" == "" ]; then
    echo "### `basename $0` ERROR: Policy location requires upstream --upstream name"
    exit 1
    fi

    if [ ! -f $1 ]; then
    echo "### `basename $0` ERROR: Cannot open $1"
    exit 1
    @@ -93,9 +106,9 @@ for SWAGGER_PATH in `shyaml keys paths < $1`; do
    echo "}"
    done

    if [ $DO_LOCATION ]; then
    if [ $DO_LOCATION -eq 1 ]; then
    echo ""
    echo "location = /_$API_NAME {"
    echo " proxy_pass http://\$upstream\$request_uri;"
    echo "}"
    fi
    fi
  19. @lcrilly lcrilly revised this gist May 2, 2018. 1 changed file with 101 additions and 0 deletions.
    101 changes: 101 additions & 0 deletions swagger2nginx.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    #!/usr/bin/env bash
    #
    # swagger2nginx.sh (c) NGINX, Inc. [v0.1 02-May-2018] Liam Crilly <[email protected]>

    if [ $# -lt 1 ]; then
    echo "### USAGE: `basename $0` [options] <swagger_file.yaml>"
    echo "### Options:"
    echo "### -b | --basepath <basePath> # Override Swagger basePath"
    echo "### -l | --location # Create policy location"
    echo "### -n | --api-name <API name> # Override Swagger title"
    echo "### -p | --prefix <prefix path> # Apply prefix to basePath"
    echo "### -u | --upstream <upstream name> # Specify upstream group"
    exit 1
    fi

    API_NAME=""
    DO_LOCATION=0
    BASEPATH=""
    PREFIX_PATH=""
    UPSTREAM=""
    while [ $# -gt 1 ]; do
    case "$1" in
    "-b" | "--basepath")
    BASEPATH=$2
    shift; shift
    ;;
    "-l" | "--location")
    DO_LOCATION=1
    shift
    ;;
    "-n" | "--api-name")
    API_NAME=$2
    shift; shift
    ;;
    "-p" | "--prefix")
    PREFIX_PATH=$2
    shift; shift
    ;;
    "-u" | "--upstream")
    UPSTREAM=$2
    shift; shift
    ;;
    *)
    echo "### `basename $0` ERROR: Invalid command line option ($1)"
    exit 1
    ;;
    esac
    done

    if [ ! -f $1 ]; then
    echo "### `basename $0` ERROR: Cannot open $1"
    exit 1
    fi

    if [ "$API_NAME" == "" ]; then
    # Convert title to NGINX-friendly API name
    API_NAME=`shyaml get-value info.title < $1 | tr '[:space:]' '_' | tr -cd '[:alnum:]_-' 2> /dev/null`
    if [ "$API_NAME" == "" ]; then
    echo "### `basename $0` ERROR: Swagger file has missing/invalid title for API name"
    exit 1
    fi
    fi

    if [ "$BASEPATH" == "" ]; then
    BASEPATH=`shyaml get-value basePath < $1 2> /dev/null`
    if [ "$BASEPATH" == "" ]; then
    echo "### `basename $0` ERROR: No basePath found in Swagger"
    exit 1
    fi
    fi
    BASEPATH=$PREFIX_PATH$BASEPATH

    for SWAGGER_PATH in `shyaml keys paths < $1`; do
    # Convert path templates to regular expressions
    URI=`echo $SWAGGER_PATH | sed -e "s/\({.*}\)/\[\^\/\]\*/g"`

    if [ "$SWAGGER_PATH" == "$URI" ]; then
    echo "location = $BASEPATH$URI {" # Exact match when no path templates
    else
    echo "location ~ ^$BASEPATH$URI\$ {" # Regex match
    fi

    METHODS=`shyaml keys paths.$SWAGGER_PATH < $1 | grep -v parameters | tr '\n' ' '`
    if [ "$METHODS" != "" ]; then
    echo " limit_except $METHODS{}"
    fi

    if [ "$UPSTREAM" != "" ]; then
    echo " set \$upstream $UPSTREAM;"
    fi

    echo " rewrite ^ /_$API_NAME last;"
    echo "}"
    done

    if [ $DO_LOCATION ]; then
    echo ""
    echo "location = /_$API_NAME {"
    echo " proxy_pass http://\$upstream\$request_uri;"
    echo "}"
    fi
  20. @lcrilly lcrilly revised this gist May 1, 2018. No changes.
  21. @lcrilly lcrilly revised this gist May 1, 2018. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions api_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    log_format api_main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent '
    '"$http_referer" '"$http_user_agent" "$http_x_forwarded_for" "$api_name"';
    log_format api_main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$api_name"';

    include api_endpoints.conf;
    include api_keys.conf;
  22. @lcrilly lcrilly revised this gist May 1, 2018. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions warehouse_api_rewrites.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    # Rewrite rules
    #
    rewrite ^/api/warehouse/inventory/item/price/(.*) /api/warehouse/pricing/$1;

    # API definition
    #
    location /api/warehouse/inventory {
    set $upstream inventory_service;
    rewrite ^(.*)$ /_warehouse$1 last;
    }
    location /api/warehouse/pricing {
    set $upstream pricing_service;
    rewrite ^(.*) /_warehouse$1 last;
    }

    # Policy section
    #
    location /_warehouse {
    internal;
    set $api_name "Warehouse";

    # Policy configuration here (authentication, rate limiting, logging, more...)

    rewrite ^/_warehouse/(.*)$ /$1 break; # Remove /_warehouse prefix
    proxy_pass http://$upstream; # Proxy the rewritten URI
    }

    # vim: syntax=nginx
  23. @lcrilly lcrilly revised this gist May 1, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions warehouse_api_precise.conf
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,18 @@
    # API definition (precise)
    #
    location ~ ^/api/warehouse/inventory { # Complete inventory
    location = /api/warehouse/inventory { # Complete inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }
    location ~ ^/api/warehouse/inventory/shelf/[^/].* { # Shelf inventory
    location ~ ^/api/warehouse/inventory/shelf/[^/]*$ { # Shelf inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }
    location ~ ^/api/warehouse/inventory/shelf/[^/].*/box/[^/].* { # Box on shelf
    location ~ ^/api/warehouse/inventory/shelf/[^/]*/box/[^/]*$ { # Box on shelf
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }
    location ~ ^/api/warehouse/pricing/[^/].*$ { # Price for specific item
    location ~ ^/api/warehouse/pricing/[^/]*$ { # Price for specific item
    set $upstream pricing_service;
    rewrite ^ /_warehouse last;
    }
  24. @lcrilly lcrilly revised this gist May 1, 2018. 1 changed file with 31 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions warehouse_api_precise.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    # API definition (precise)
    #
    location ~ ^/api/warehouse/inventory { # Complete inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }
    location ~ ^/api/warehouse/inventory/shelf/[^/].* { # Shelf inventory
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }
    location ~ ^/api/warehouse/inventory/shelf/[^/].*/box/[^/].* { # Box on shelf
    set $upstream inventory_service;
    rewrite ^ /_warehouse last;
    }
    location ~ ^/api/warehouse/pricing/[^/].*$ { # Price for specific item
    set $upstream pricing_service;
    rewrite ^ /_warehouse last;
    }

    # Policy section
    #
    location = /_warehouse {
    internal;
    set $api_name warehouse_api;

    # Policy configuration here (authentication, rate limiting, logging, more...)

    proxy_pass http://$upstream$request_uri;
    }

    # vim: syntax=nginx
  25. @lcrilly lcrilly revised this gist May 1, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -2,18 +2,18 @@
    #
    location /api/warehouse/inventory {
    set $upstream warehouse_inventory;
    rewrite ^(.*)$ /_warehouse$1 last;
    rewrite ^ /_warehouse last;
    }
    location /api/warehouse/pricing {
    set $upstream warehouse_pricing;
    rewrite ^(.*)$ /_warehouse$1 last;
    rewrite ^ /_warehouse last;
    }

    # Policy section
    #
    location /_warehouse {
    location = /_warehouse {
    internal;
    set $api_name Warehouse;
    set $api_name "Warehouse";

    # Policy configuration here (authentication, rate limiting, logging, more...)

  26. @lcrilly lcrilly revised this gist May 1, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ location /api/warehouse/pricing {
    rewrite ^(.*)$ /_warehouse$1 last;
    }

    # Policy block
    # Policy section
    #
    location /_warehouse {
    internal;
  27. @lcrilly lcrilly revised this gist May 1, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    # API definition
    #
    location /api/warehouse/pricing {
    set $upstream warehouse_pricing;
    rewrite ^(.*)$ /_warehouse$1 last;
    }
    location /api/warehouse/inventory {
    set $upstream warehouse_inventory;
    rewrite ^(.*)$ /_warehouse$1 last;
    }
    location /api/warehouse/pricing {
    set $upstream warehouse_pricing;
    rewrite ^(.*)$ /_warehouse$1 last;
    }

    # Policy block
    #
  28. @lcrilly lcrilly revised this gist May 1, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ location /_warehouse {
    internal;
    set $api_name Warehouse;

    # Policy configuration here (authentication, rate limiting, logging, more...
    # Policy configuration here (authentication, rate limiting, logging, more...)

    proxy_pass http://$upstream$request_uri;
    }
  29. @lcrilly lcrilly revised this gist May 1, 2018. 1 changed file with 2 additions and 7 deletions.
    9 changes: 2 additions & 7 deletions warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -13,14 +13,9 @@ location /api/warehouse/inventory {
    #
    location /_warehouse {
    internal;
    set $api_name warehouse_api;
    set $api_name Warehouse;

    if ($http_apikey = "") {
    return 401; # Unauthorized (please authenticate)
    }
    if ($api_client_name = "") {
    return 403; # Forbidden (invalid API key)
    }
    # Policy configuration here (authentication, rate limiting, logging, more...

    proxy_pass http://$upstream$request_uri;
    }
  30. @lcrilly lcrilly revised this gist May 1, 2018. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions api_endpoints.conf
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    upstream inventory_service {
    upstream warehouse_inventory {
    zone inventory_service 64k;
    server 10.0.0.1:80;
    server 10.0.0.2:80;
    server 10.0.0.3:80;
    }

    upstream pricing_service {
    upstream warehouse_pricing {
    zone pricing_service 64k;
    server 10.0.0.7:80;
    server 10.0.0.8:80;
    4 changes: 2 additions & 2 deletions warehouse_api_simple.conf
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    # API definition
    #
    location /api/warehouse/pricing {
    set $upstream pricing_service;
    set $upstream warehouse_pricing;
    rewrite ^(.*)$ /_warehouse$1 last;
    }
    location /api/warehouse/inventory {
    set $upstream inventory_service;
    set $upstream warehouse_inventory;
    rewrite ^(.*)$ /_warehouse$1 last;
    }