Reverse Proxy dengan Nginx dan Golang untuk Microservices
2
min read .
Published on
Saat aplikasi makin besar, memecahnya jadi beberapa service kecil (microservices) adalah pilihan bijak. Misalnya:
- Service Product di port 8080
- Service Blog di port 8081
Agar user tidak perlu tahu port-port tersebut, kita pakai Nginx reverse proxy untuk menyatukan semuanya di bawah satu domain.
1. Setup Nginx Reverse Proxy
Buat file konfigurasi Nginx:
sudo nano /etc/nginx/sites-available/yourdomain.com
Isi dengan konfigurasi berikut:
upstream api_product {
server 127.0.0.1:8080;
}
upstream api_blog {
server 127.0.0.1:8081;
}
server {
listen 80;
server_name yourdomain.com;
# Proxy untuk service product
location /product/ {
proxy_pass http://api_product/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Proxy untuk service blog
location /blog/ {
proxy_pass http://api_blog/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Aktifkan konfigurasi:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
Cek apakah konfigurasi valid:
sudo nginx -t
Reload Nginx:
sudo systemctl reload nginx
2. Service Product
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type Response struct {
Message string `json:"message"`
Data []Product `json:"data,omitempty"`
}
type Product struct {
Name string `json:"name"`
Price int `json:"price"`
}
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, Response{
Message: "Success",
Data: []Product{
{Name: "Product 1", Price: 100},
{Name: "Product 2", Price: 200},
{Name: "Product 3", Price: 300},
},
})
})
r.POST("/create", func(c *gin.Context) {
var product Product
if err := c.ShouldBindJSON(&product); err != nil {
c.JSON(http.StatusBadRequest, Response{Message: "Bad Request"})
return
}
c.JSON(http.StatusCreated, Response{
Message: "Product Created",
Data: []Product{product},
})
})
r.Run(":8080")
}
3. Service Blog
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type Response struct {
Message string `json:"message"`
Data []Article `json:"data,omitempty"`
}
type Article struct {
Name string `json:"name" binding:"required"`
Content string `json:"content" binding:"required"`
}
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, Response{
Message: "Success",
Data: []Article{
{Name: "Article 1", Content: "Content 1"},
{Name: "Article 2", Content: "Content 2"},
{Name: "Article 3", Content: "Content 3"},
},
})
})
r.POST("/create", func(c *gin.Context) {
var article Article
if err := c.ShouldBindJSON(&article); err != nil {
c.JSON(http.StatusBadRequest, Response{Message: "Bad Request"})
return
}
c.JSON(http.StatusCreated, Response{
Message: "Article Created",
Data: []Article{article},
})
})
r.Run(":8081")
}
4. Cara Akses
Jika semua sudah berjalan:
http://yourdomain.com/product/
→ mengarah ke service product (port 8080).http://yourdomain.com/blog/
→ mengarah ke service blog (port 8081).
User hanya perlu mengakses domain, tanpa tahu detail port service di belakangnya.
5. Kesimpulan
Dengan konfigurasi sites-available → sites-enabled, Nginx bisa mengatur reverse proxy untuk berbagai microservice dengan lebih rapi. Digabung dengan Golang (Gin), kita bisa membuat service ringan dan cepat yang mudah diskalakan.