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; limit_req_zone $binary_remote_addr zone=client_ip_10rs:1m rate=10r/s; limit_req_zone $http_apikey zone=apikey_200rs:1m rate=200r/s; 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 # Dummy location used to populate $request_body for JSON validation location = /_NULL { internal; return 204; } } js_include json_validator.js; js_set $validated json_validator; server { listen 127.0.0.1:10415; # This is the error response of json_validator() return 415; # Unsupported media type include api_json_errors.conf; } # Coalesce request method to READ|WRITE map $request_method $request_type { "GET" "READ"; "HEAD" "READ"; "OPTIONS" "READ"; default "WRITE"; } # vim: syntax=nginx