Web application တွေကို AWS မှာ တင်တဲ့အခါမှာ server တစ်ခုတည်းကို အင်တာနက်နဲ့ တိုက်ရိုက်ချိတ်ဆက်တာဟာ လုံခြုံရေးအရရော၊ စွမ်းဆောင်ရည်အရပါ မသင့်တော်ဘူးဆိုတာ ကျွန်တော်တို့ လေ့လာခဲ့ပြီးပါပြီ။ AWS မှာ Application Load Balancer (ALB) လိုမျိုး Load Balancer တွေရှိပေမယ့်၊ တစ်ခါတစ်ရံမှာ ပိုမိုပြောင်းလွယ်ပြင်လွယ်ရှိတဲ့ architecture တွေအတွက် Nginx လိုမျိုး reverse proxy တွေက မရှိမဖြစ်လိုအပ်လာပါတယ်။
ဒီနေ့မှာတော့ Nginx reverse proxy ကို AWS ecosystem ထဲမှာ ဘယ်လိုအသုံးပြုပြီး ပိုမိုလုံခြုံ၊ ပိုမိုမြန်ဆန်တဲ့ architecture ကို တည်ဆောက်နိုင်မလဲဆိုတာကို အသေးစိတ် လေ့လာသွားပါမယ်။
Reverse Proxy နဲ့ AWS တို့ ဘယ်လို အတူတူ အလုပ်လုပ်သလဲ။
AWS မှာ Load Balancing ကို အဓိကအားဖြင့် ELB (Elastic Load Balancing) က လုပ်ဆောင်ပါတယ်။ ဒါပေမယ့် ELB က EC2 instance တွေကြားမှာသာ traffic ကို မျှဝေပေးနိုင်ပါတယ်။ အဲဒီ EC2 instance တစ်ခုချင်းစီပေါ်မှာရှိတဲ့ application တွေကို ဘယ်လိုကိုင်တွယ်မလဲဆိုတာကတော့ သင့်တာဝန်ဖြစ်ပါတယ်။
ဒီနေရာမှာ Nginx က ဝင်ရောက်လာပါတယ်။ Nginx ဟာ ELB နောက်ကွယ်မှာရှိတဲ့ EC2 instance ပေါ်မှာ ထိုင်နေပြီး၊ အဲဒီ instance ကို ရောက်လာတဲ့ traffic ကို အတွင်းပိုင်းရှိ applications တွေဆီကို ထပ်မံခွဲခြား လမ်းညွှန်ပေးတဲ့ proxy တစ်ခုအဖြစ် လုပ်ဆောင်ပါတယ်။
AWS Architecture ထဲမှာ Nginx သုံးခြင်းရဲ့ အကျိုးကျေးဇူးများ
- URL Based Routing: EC2 instance တစ်ခုတည်းပေါ်မှာ applications အများအပြား (ဥပမာ- port 3000 မှာ run နေတဲ့ API နဲ့ port 4000 မှာ run နေတဲ့ website) ကို တင်ထားတဲ့အခါ Nginx က URL လမ်းကြောင်း (path) အပေါ်မူတည်ပြီး သင့်တော်တဲ့ application ဆီကို လမ်းညွှန်ပေးနိုင်ပါတယ်။
- Performance Optimization: Nginx ဟာ static content (ပုံတွေ၊ CSS ဖိုင်တွေ) ကို cache လုပ်နိုင်တာကြောင့် backend applications တွေရဲ့ load ကို သက်သာစေပါတယ်။
- Security Layer: Nginx ဟာ သင့်ရဲ့ applications တွေရှေ့မှာ firewall လိုမျိုး နောက်ထပ်ကာကွယ်ရေး အလွှာတစ်ခုအဖြစ် လုပ်ဆောင်နိုင်ပါတယ်။
- Local Load Balancing: တစ်ခါတစ်ရံမှာ EC2 instance တစ်ခုတည်းမှာ applications ပေါင်းများစွာကို တင်ထားပြီး Nginx ကနေ internal load balancing လုပ်တာမျိုးလည်း ရှိပါတယ်။
AWS Architecture နဲ့ Nginx Reverse Proxy ပုံစံ
ဒီ architecture မှာ သင်ဟာ ကျွန်တော်တို့ရဲ့ အရင် post တွေမှာ လေ့လာခဲ့တဲ့ ဝန်ဆောင်မှုတွေကို အတူတကွ ပေါင်းစပ်အသုံးပြုမှာ ဖြစ်ပါတယ်။
- Route 53: သင့်ရဲ့ domain name (
yourwebsite.com
) ကို စီမံခန့်ခွဲပါတယ်။ - Elastic Load Balancer (ELB): ဝင်လာတဲ့ traffic ကို စုစည်းပြီး သင့်ရဲ့ EC2 instance တွေဆီကို မျှဝေပေးပါတယ်။ ELB မှာ HTTPS/SSL Termination ကိုလည်း လုပ်ဆောင်နိုင်ပါတယ်။
- Auto Scaling Group: တူညီတဲ့ EC2 instance တွေကို အုပ်စုဖွဲ့ထားပြီး၊ traffic ပေါ်မူတည်ပြီး instances တွေကို အလိုအလျောက် တိုးချဲ့ပေးပါတယ်။
- EC2 Instances: EC2 instance တစ်ခုစီမှာ Nginx ကို install လုပ်ပြီး local reverse proxy အဖြစ် အသုံးပြုပါတယ်။
- Backend Applications: အမှန်တကယ် အလုပ်လုပ်မယ့် သင့်ရဲ့ applications တွေ (ဥပမာ- Node.js, Python, Java applications) ကို Nginx ရဲ့ နောက်ကွယ်မှာ run ထားပါတယ်။
ဒီအစီအစဉ်ရဲ့ traffic flow ကတော့ User -> Route 53 -> ELB -> EC2 instance (Nginx) -> Backend Applications ဖြစ်ပါတယ်။
AWS နှင့် Nginx Configuration ဥပမာ
ဒီဥပမာမှာ Nginx ကို EC2 instance တစ်ခုပေါ်မှာ install လုပ်ပြီး URL လမ်းကြောင်းပေါ်မူတည်ပြီး ဝင်လာတဲ့ request တွေကို မတူညီတဲ့ applications တွေဆီ လမ်းညွှန်ပေးပုံကို ပြထားပါတယ်။
http://yourwebsite.com/blog
ကို ဝင်လာရင် Blog Application (port 4000) ဆီကို လမ်းညွှန်မယ်။http://yourwebsite.com/api
ကို ဝင်လာရင် REST API (port 5000) ဆီကို လမ်းညွှန်မယ်။
# This configuration is for an Nginx server running on an EC2 instance,
# receiving traffic from an AWS Elastic Load Balancer.
server {
listen 80; # Listen for traffic from the ELB on port 80
# Example 1: Route traffic to a blog application
# ဥပမာ ၁- Blog application ဆီသို့ traffic လမ်းညွှန်ခြင်း
location /blog/ {
proxy_pass http://localhost:4000/; # Pass to the blog app running on port 4000
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Example 2: Route traffic to a REST API
# ဥပမာ ၂- REST API ဆီသို့ traffic လမ်းညွှန်ခြင်း
location /api/ {
proxy_pass http://localhost:5000/; # Pass to the API app running on port 5000
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Example 3: Forward all other traffic to the main website application
# ဥပမာ ၃- ကျန်ရှိသော traffic အားလုံးကို main website ဆီသို့ ပို့ခြင်း
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
နိဂုံး
Nginx ကို AWS နဲ့ ပေါင်းစပ်အသုံးပြုခြင်းဟာ သင့်ရဲ့ application တွေကို ပိုမိုပြောင်းလွယ်ပြင်လွယ်ရှိပြီး စွမ်းဆောင်ရည်မြင့်မားတဲ့ architecture တစ်ခုကို ပေးနိုင်ပါတယ်။ AWS ရဲ့ ELB က ဝင်လာတဲ့ traffic ကို server တွေဆီ ခွဲပေးနိုင်ပေမယ့် Nginx ကတော့ အဲဒီ server တစ်ခုချင်းစီအတွင်းမှာပဲ traffic ကို ပိုမိုတိကျစွာ စီမံခန့်ခွဲနိုင်ပါတယ်။
ဒါဟာ သင့်ရဲ့ လုပ်ငန်းလိုအပ်ချက်အပေါ်မူတည်ပြီး အကောင်းဆုံးဖြစ်အောင် ချိန်ညှိနိုင်တဲ့ ပုံစံတစ်ခုဖြစ်တာကြောင့် ခေတ်မီတဲ့ cloud architecture တွေအတွက် မရှိမဖြစ်လိုအပ်တဲ့ အစိတ်အပိုင်းတစ်ခုပဲ ဖြစ်ပါတယ်။
English Version
The Internet’s Gatekeeper: Nginx Reverse Proxy in an AWS Architecture
When deploying web applications on AWS, directly exposing a single server to the internet is not always the best practice for security or performance. While AWS provides services like the Application Load Balancer (ALB), for more flexible architectures, a reverse proxy like Nginx is often essential.
Today, we’ll explore how to use Nginx as a reverse proxy within an AWS ecosystem to build a more secure, faster, and more scalable architecture.
How Reverse Proxy and AWS Work Together
In AWS, load balancing is primarily handled by the ELB (Elastic Load Balancing). However, the ELB only distributes traffic among your EC2 instances. It’s up to you to manage how traffic is handled within each of those instances.
This is where Nginx comes in. Nginx sits on each EC2 instance behind the ELB and acts as a local reverse proxy, receiving traffic from the ELB and intelligently routing it to the appropriate internal applications.
Benefits of Using Nginx in an AWS Architecture
- URL-Based Routing: When you have multiple applications on a single EC2 instance (e.g., an API on port 3000 and a website on port 4000), Nginx can route requests to the correct application based on the URL path.
- Performance Optimization: Nginx can cache static content (images, CSS files) at the instance level, which reduces the load on your backend applications.
- Security Layer: Nginx provides an additional layer of security, acting as a buffer between your backend services and the public internet.
- Local Load Balancing: In some cases, you might run multiple instances of the same application on a single EC2 instance, and Nginx can provide internal load balancing to distribute traffic among them.
A Common AWS Architecture with Nginx
This architecture combines several services we’ve discussed in previous posts to create a robust and scalable setup.
- Route 53: Manages your domain name (
yourwebsite.com
). - Elastic Load Balancer (ELB): Acts as the main entry point, distributing incoming traffic across multiple EC2 instances. It can also handle SSL/TLS termination for the entire domain.
- Auto Scaling Group: Groups your identical EC2 instances and automatically scales them up or down based on traffic.
- EC2 Instances: Each instance has Nginx installed and configured to act as a local reverse proxy.
- Backend Applications: These are your actual applications (e.g., Node.js, Python, Java apps) running on different ports behind Nginx.
The flow of traffic in this setup is User -> Route 53 -> ELB -> EC2 instance (Nginx) -> Backend Applications.
Nginx Configuration with AWS-specific Examples
This example shows how to configure an Nginx server on an EC2 instance to route incoming requests based on the URL path to different applications.
- A request for
http://yourwebsite.com/blog
will be routed to the Blog Application running on port 4000. - A request for
http://yourwebsite.com/api
will be routed to the REST API running on port 5000.
# This configuration is for an Nginx server running on an EC2 instance,
# receiving traffic from an AWS Elastic Load Balancer.
server {
listen 80; # Listen for traffic from the ELB on port 80
# Example 1: Route traffic to a blog application
location /blog/ {
proxy_pass http://localhost:4000/; # Pass to the blog app running on port 4000
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Example 2: Route traffic to a REST API
location /api/ {
proxy_pass http://localhost:5000/; # Pass to the API app running on port 5000
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Example 3: Forward all other traffic to the main website application
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Conclusion
Integrating Nginx with AWS provides a powerful and flexible architecture for your applications. While AWS services like ELB handle the macro-level load balancing, Nginx gives you granular control over how traffic is handled within each server.
This hybrid approach allows you to combine the best of both worlds: the robust, scalable infrastructure of AWS with the flexible, high-performance capabilities of Nginx. It’s a standard pattern for building modern, production-grade cloud applications.