This content originally appeared on DEV Community and was authored by terngr
NGINX ทั้ง Open Source และ Plus สามารถทำ Authentication/Authorization ได้
ในตัวอย่าง จะมีตั้ง Service ชื่อ cat-api ขึ้นมา เพื่อให้บริการ api แบบ unauthenticated

จะเห็นได้ว่า เราสามารถเรียกใช้บริการ ถามชื่อแมว ได้ผ่าน /cat ก็จะได้ชื่อแมว โดยไม่ต้อง Authenticate
เรารัน NGINX ขึ้นมา เพื่อทำหน้าที่ Authentication (ในทางปฏิบัติ สามารถใช้ MAP เพื่อทำ authorization หรือทำ RBAC ได้ด้วย)

จะเห็นได้ว่า ตอนนี้ถ้าเรียกใช้งานผ่าน NGINX จำเป็นต้อง Provide valid API key เสียก่อน จึงจะใช้บริการ ถามชื่อแมว ได้
สิ่งที่ต้องทำต่อ ก็คือจำกัดการเข้าใช้บริการ cat-api โดยตรง โดยเราจะ bind port เฉพาะ NGINX ที่ทำหน้าที่ Authentication/Authorization เท่านั้น แล้วให้ NGINX เรียกผ่าน network: f1-net ไปยัง cat-api อีกต่อหนึ่ง
สิ่งที่ได้ ก็จะทำ Authentication ให้กับ cat-api ได้นั่นเองครับ
ทั้งนี้ NGINX ยังสามารถใช้ map directive ช่วยทำ Authorization และ RBAC ได้ด้วย,
สำหรับ NGINX Plus จะมีรองรับ JWT, รวมถึง OIDC, SAML ก็รองรับ และเร็วๆ นี้ยังมี Module เฉพาะสำหรับ LDAP ด้วย ทำให้ NGINX คุยกับ LDAP ได้โดยตรงครับ (เดิมต้องผ่าน 3rd party)
This content originally appeared on DEV Community and was authored by terngr
terngr | Sciencx (2025-10-21T04:42:51+00:00) NGINX API Authentication/Authorization. Retrieved from https://www.scien.cx/2025/10/21/nginx-api-authentication-authorization/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.

