Nginx Reverse Proxy

Web application တစ်ခုကို တည်ဆောက်တဲ့အခါ server တစ်ခုတည်းကို အင်တာနက်နဲ့ တိုက်ရိုက်ချိတ်ဆက်တာဟာ လုံခြုံရေးအရ၊ စွမ်းဆောင်ရည်အရ အမြဲတမ်းကောင်းမွန်တဲ့ နည်းလမ်းမဟုတ်ပါဘူး။ ဥပမာအားဖြင့်၊ သင့်ရဲ့ Node.js, Python, ဒါမှမဟုတ် Apache server တွေကို အင်တာနက်နဲ့ တိုက်ရိုက်ထိတွေ့စေတာဟာ အန္တရာယ်ရှိနိုင်သလို traffic များလာတဲ့အခါမှာလည်း server လေးလံတာမျိုး ဖြစ်နိုင်ပါတယ်။

ဒီလိုပြဿနာတွေကို ဖြေရှင်းဖို့အတွက် Reverse Proxy ကို အသုံးပြုကြပါတယ်။ ဒီနေ့မှာတော့ အင်တာနက်မှာ အသုံးအများဆုံး reverse proxy တစ်ခုဖြစ်တဲ့ Nginx အကြောင်းကို ဥပမာ configuration တွေနဲ့တကွ အသေးစိတ် လေ့လာသွားပါမယ်။

Reverse Proxy ဆိုတာ ဘာလဲ။

Reverse Proxy ဆိုတာ သင့်ရဲ့ web servers (backend servers) တွေရှေ့မှာ ထိုင်နေတဲ့ server တစ်ခုဖြစ်ပြီး၊ ဝင်လာတဲ့ request တွေအားလုံးကို ကြားဖြတ်ဖမ်းယူကာ လိုအပ်တဲ့ backend server ဆီကို လမ်းညွှန်ပို့ဆောင်ပေးပါတယ်။

ရိုးရှင်းတဲ့ ဥပမာတစ်ခုနဲ့ ပြောရရင်၊ ကုမ္ပဏီတစ်ခုရဲ့ အစည်းအဝေးခန်းမကို လာတဲ့ဧည့်သည် (client) ဟာ ခန်းမထဲမှာရှိတဲ့ ဝန်ထမ်းတစ်ဦးချင်းစီ (backend server) နဲ့ တိုက်ရိုက်မတွေ့ပါဘူး။ သူတို့ဟာ ဧည့်ခံ (receptionist) နဲ့ အရင်တွေ့ပြီး ဘယ်ဝန်ထမ်းနဲ့ တွေ့ချင်တယ်ဆိုတာကို ပြောရပါတယ်။ ဧည့်ခံကမှ အဲဒီဝန်ထမ်းကို လမ်းညွှန်ပေးပါတယ်။ ဒီဥပမာမှာ ဧည့်ခံ (receptionist) ဟာ Reverse Proxy နဲ့ အလားသဏ္ဍာန်တူပါတယ်။

Nginx ကို Reverse Proxy အဖြစ် ဘာကြောင့် သုံးသင့်တာလဲ။

  • လုံခြုံရေးကို မြှင့်တင်ပေးခြင်း (Enhanced Security): Nginx ဟာ သင့်ရဲ့ backend server တွေရဲ့ IP address တွေနဲ့ identities တွေကို ဖုံးကွယ်ပေးပါတယ်။ ဒါကြောင့် တိုက်ခိုက်သူတွေဟာ သင့်ရဲ့ နောက်ကွယ်က server တွေကို တိုက်ရိုက် access လုပ်လို့မရပါဘူး။
  • Load Balancing: Nginx ဟာ ဝင်လာတဲ့ traffic တွေကို backend servers များစွာဆီကို မျှတစွာ ဖြန့်ဝေပေးနိုင်ပါတယ်။ ဒါဟာ server တစ်ခုတည်းကို အလုပ်တွေ ပုံကျမလာအောင် ကာကွယ်ပေးပြီး စွမ်းဆောင်ရည်ကို မြှင့်တင်ပေးပါတယ်။
  • SSL/TLS Termination: Nginx ဟာ SSL certificate တွေကို ကိုင်တွယ်နိုင်ပါတယ်။ ဒါဟာ traffic encryption/decryption လုပ်ငန်းစဉ်ကို Nginx က လုပ်ဆောင်ပေးတာကြောင့် backend servers တွေရဲ့ CPU load ကို သက်သာစေပါတယ်။
  • Caching: Nginx က frequently-accessed static content (ပုံတွေ၊ CSS ဖိုင်တွေ) ကို cache လုပ်နိုင်တာကြောင့် client request တွေကို ပိုမိုမြန်ဆန်စွာ တုံ့ပြန်နိုင်ပါတယ်။
  • တစ်ခုတည်းသော Entry Point: သင့်မှာ server တွေ အများကြီးရှိနေရင်တောင် Nginx ကနေတဆင့် domain တစ်ခုတည်းကိုသာ သုံးပြီးဝင်နိုင်အောင် လုပ်ပေးနိုင်ပါတယ်။

Nginx Reverse Proxy Configuration ဥပမာများ

အောက်ပါ configuration ဥပမာတွေဟာ /etc/nginx/nginx.conf ဒါမှမဟုတ် /etc/nginx/sites-available/ မှာရှိတဲ့ သီးခြား site configuration ဖိုင်ထဲမှာ ပါဝင်နိုင်ပါတယ်။

ဥပမာ ၁- အခြေခံ Reverse Proxy (Single Server)

ဒီ configuration က client request တွေအားလုံးကို backend မှာရှိတဲ့ server တစ်ခု (ဥပမာ- port 3000 မှာ run နေတဲ့ Node.js app) ဆီကို လမ်းညွှန်ပေးပါလိမ့်မယ်။

Nginx

# Nginx basic reverse proxy configuration
# Nginx အခြေခံ reverse proxy configuration
server {
    listen 80; # Listen for incoming HTTP traffic on port 80
    server_name mywebsite.com www.mywebsite.com; # Specify your domain name
    location / {
        # Forward all requests to the backend server
        # ဝင်လာသော request အားလုံးကို backend server ဆီသို့ လမ်းညွှန်ပါ။
        proxy_pass http://localhost:3000;
        # Pass the original host and IP to the backend
        # မူရင်း host နှင့် IP များကို backend သို့ ပို့ပါ။
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

ဥပမာ ၂- Load Balancing

သင့်မှာ backend servers တွေ အများကြီးရှိရင် Nginx ကနေ traffic ကို မျှတစွာ ဖြန့်ဝေပေးနိုင်ပါတယ်။ ဒီဥပမာမှာ backend servers သုံးခုကို backend_servers ဆိုတဲ့ group တစ်ခုအဖြစ် သတ်မှတ်ပြီး proxy_pass ကို အဲဒီ group ဆီကို လမ်းညွှန်ပေးထားပါတယ်။

Nginx

# Nginx load balancing configuration
# Nginx load balancing configuration
upstream backend_servers {
    # Define a group of backend servers
    # backend servers များကို group တစ်ခုအဖြစ် သတ်မှတ်ပါ။
    server 192.168.1.10:8000;
    server 192.168.1.11:8000;
    server 192.168.1.12:8000;
}
server {
    listen 80;
    server_name mywebsite.com;
    location / {
        # Pass requests to the upstream group
        # Request များကို upstream group ဆီသို့ ပို့ပါ။
        proxy_pass http://backend_servers;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

ဥပမာ ၃- SSL/TLS Termination

ဒီ configuration ကတော့ Nginx ကို SSL Certificate တွေနဲ့ configure လုပ်ပြီး HTTP request တွေကို HTTPS အဖြစ် auto-redirect လုပ်ပေးပါလိမ့်မယ်။ Nginx က HTTPS traffic (port 443) ကို လက်ခံပြီး၊ encryption လုပ်ငန်းစဉ်ကို ဖြတ်သန်းပြီးတဲ့အခါ HTTP (port 3000) အဖြစ် backend server ဆီကို ပြန်ပို့ပေးပါတယ်။

Bash

# Nginx SSL/TLS termination configuration
# Nginx SSL/TLS termination configuration
server {
    listen 80; # Listen for HTTP traffic
    server_name mywebsite.com;
    # Redirect all HTTP requests to HTTPS
    # HTTP request အားလုံးကို HTTPS သို့ redirect လုပ်ပါ။
    return 301 https://$host$request_uri;
}
server {
    listen 443 ssl; # Listen for HTTPS traffic
    server_name mywebsite.com;
    # SSL certificate paths
    # SSL certificate လမ်းကြောင်းများ
    ssl_certificate /etc/nginx/ssl/mywebsite.crt;
    ssl_certificate_key /etc/nginx/ssl/mywebsite.key;
    location / {
        # Forward the decrypted traffic to the backend server
        # Decrypted လုပ်ပြီးသား traffic ကို backend server သို့ ပို့ပါ။
        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 ကို reverse proxy အဖြစ် အသုံးပြုခြင်းဟာ သင့်ရဲ့ application တွေကို ပိုမိုလုံခြုံ၊ မြန်ဆန်ပြီး စိတ်ချရအောင် ကူညီပေးနိုင်ပါတယ်။ ဒါဟာ backend server တွေကို direct exposure ကနေ ကာကွယ်ပေးရုံသာမက Load Balancing နဲ့ SSL Termination လိုမျိုး အရေးကြီးတဲ့ လုပ်ငန်းဆောင်တာတွေကိုပါ လုပ်ဆောင်ပေးတာကြောင့် ခေတ်မီတဲ့ web architecture တွေအတွက် မရှိမဖြစ်လိုအပ်တဲ့ အစိတ်အပိုင်းတစ်ခုဖြစ်ပါတယ်။

ဒီ configuration ဥပမာတွေကို သင့်ကိုယ်ပိုင် project မှာ စတင်စမ်းသပ်ကြည့်ပြီး Nginx reverse proxy ရဲ့ စွမ်းဆောင်ရည်တွေကို လက်တွေ့လေ့လာနိုင်ပါတယ်။


English Version

The Internet’s Gatekeeper: A Deep Dive into Nginx Reverse Proxy with Examples

When building a web application, exposing a single server directly to the internet is often not the best practice, either for security or performance. For example, letting your Node.js, Python, or Apache server be directly accessible can be risky and may cause the server to slow down under heavy traffic.

To solve these problems, we use a Reverse Proxy. In this post, we’ll take a deep dive into Nginx, one of the most popular reverse proxies on the internet, and explore its configurations with practical examples.

What is a Reverse Proxy?

A reverse proxy is a server that sits in front of one or more web servers (backend servers). It intercepts all client requests and forwards them to the appropriate backend server, routing the response back to the client.

A simple analogy is a company’s front desk. A client (the visitor) doesn’t directly talk to a specific employee (the backend server) in the building. They first talk to the receptionist (the reverse proxy), who knows where to direct them.

Why Use Nginx as a Reverse Proxy?

  • Enhanced Security: Nginx hides the IP addresses and identities of your backend servers, making them less vulnerable to direct attacks.
  • Load Balancing: Nginx can distribute incoming traffic across multiple backend servers, preventing any single server from becoming overwhelmed and improving overall performance.
  • SSL/TLS Termination: Nginx can handle the encryption and decryption of traffic, offloading this CPU-intensive task from your backend servers and allowing them to focus on serving your application.
  • Caching: Nginx can cache frequently-accessed static content (images, CSS files), which significantly speeds up response times for subsequent requests.
  • Single Entry Point: It provides a single point of entry for your application, simplifying domain management even if you have multiple backend services.

Nginx Reverse Proxy Configuration Examples

The following configuration examples can be included in your nginx.conf file or a separate site configuration file inside /etc/nginx/sites-available/.

Example 1: Basic Reverse Proxy (Single Server)

This configuration will forward all client requests to a single backend server (e.g., a Node.js app running on port 3000).

Nginx

# Nginx basic reverse proxy configuration
# Nginx အခြေခံ reverse proxy configuration

server {
    listen 80; # Listen for incoming HTTP traffic on port 80
    server_name mywebsite.com www.mywebsite.com; # Specify your domain name

    location / {
        # Forward all requests to the backend server
        # ဝင်လာသော request အားလုံးကို backend server ဆီသို့ လမ်းညွှန်ပါ။
        proxy_pass http://localhost:3000;
        
        # Pass the original host and IP to the backend
        # မူရင်း host နှင့် IP များကို backend သို့ ပို့ပါ။
        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: Load Balancing

If you have multiple backend servers, Nginx can distribute traffic evenly among them. In this example, we define a group of three backend servers in an upstream block and use proxy_pass to route traffic to this group.

Nginx

# Nginx load balancing configuration
# Nginx load balancing configuration

upstream backend_servers {
    # Define a group of backend servers
    # backend servers များကို group တစ်ခုအဖြစ် သတ်မှတ်ပါ။
    server 192.168.1.10:8000;
    server 192.168.1.11:8000;
    server 192.168.1.12:8000;
}

server {
    listen 80;
    server_name mywebsite.com;

    location / {
        # Pass requests to the upstream group
        # Request များကို upstream group ဆီသို့ ပို့ပါ။
        proxy_pass http://backend_servers;

        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: SSL/TLS Termination

This configuration shows how to set up Nginx to handle SSL certificates. It will listen on port 443 for HTTPS traffic, decrypt it, and then forward the unencrypted traffic to your backend server (e.g., on port 3000).

Nginx

# Nginx SSL/TLS termination configuration
# Nginx SSL/TLS termination configuration

server {
    listen 80; # Listen for HTTP traffic
    server_name mywebsite.com;
    
    # Redirect all HTTP requests to HTTPS
    # HTTP request အားလုံးကို HTTPS သို့ redirect လုပ်ပါ။
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl; # Listen for HTTPS traffic
    server_name mywebsite.com;

    # SSL certificate paths
    # SSL certificate လမ်းကြောင်းများ
    ssl_certificate /etc/nginx/ssl/mywebsite.crt;
    ssl_certificate_key /etc/nginx/ssl/mywebsite.key;

    location / {
        # Forward the decrypted traffic to the backend server
        # Decrypted လုပ်ပြီးသား traffic ကို backend server သို့ ပို့ပါ။
        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

Using Nginx as a reverse proxy helps make your applications more secure, faster, and more reliable. It protects your backend servers from direct exposure and provides essential services like Load Balancing and SSL Termination, making it a crucial component of modern web architecture.


Scroll to Top
Share via
Copy link