Lamha
Api Home
Home
Api Home
Home
  1. security
  • lamha
    • Welcome ๐Ÿ‘‹
    • Status Order
    • Payment Method
    • Webhook Order
    • Rate Limit
    • API
      • Order
        • Orders List
        • Order Create
        • Order Show
        • Order Activities
        • Order Edit
        • Delete Order
        • Cancel Order
        • Return Order
      • Country
        • Countries
        • City
      • Carrier
        • Carriers
        • Create Shippment
        • Cancel Shippment
        • Label Shipment
        • Carrier City Coverage
        • Carrier Coverage
      • Warehouses
        • All Warehouses
    • security
      • Webhook For Nginx
  • Schemas
    • Payload Webhook Order Update
  1. security

Webhook For Nginx

๐Ÿ” Securing Webhooks with Nginx + njs (SHA-256 HMAC)#

A production-ready guide to validating incoming webhooks at the Nginx layer using njs (Nginx JavaScript) and HMAC SHA-256.

๐Ÿ“Œ Overview#

This setup ensures that:
Only trusted webhook sources are accepted
Payload integrity is verified using HMAC
Unauthorized or tampered requests are rejected before reaching your backend

๐Ÿงฑ Architecture Flow#

Webhook Request โ†’ Nginx (njs validation) โ†’ Backend App
                     โ›” Reject invalid
                     โœ… Forward valid

โš™๏ธ Requirements#

Nginx compiled with ngx_http_js_module (njs)
Backend application (e.g., Laravel running on 127.0.0.1:8000)

๐Ÿ“ Step 1: Create njs Validation Script#

Path:
/etc/nginx/webhook_auth.js
Code:

โš™๏ธ Step 2: Configure Nginx#


๐Ÿ” Security Best Practices#

โœ… Use Constant-Time Comparison#

Use a constant-time comparison method (if available in your njs version) to prevent timing attacks instead of:

โœ… Store Secrets Securely#

Avoid hardcoding secrets. Use:
Environment variables
Secret managers (e.g., Vault, AWS Secrets Manager)

โœ… Enable HTTPS#


โœ… Rate Limiting#


๐Ÿงช Testing#

Generate Signature (Node.js)#


Test with cURL#


โŒ Common Errors#

ErrorCause
Missing Security HeadersHeaders not sent
Invalid Store IDUnknown store
Empty PayloadNo request body
Invalid SignatureSignature mismatch

๐Ÿš€ Summary#

Validation is handled at the Nginx layer
Protects backend from malicious requests
Uses HMAC SHA-256 for verification
High performance and scalable
Modified atย 2026-03-28 08:38:30
Previous
All Warehouses
Next
Payload Webhook Order Update
Built with