karun100 / NGINX-As-Reverse-Proxy-On-AWS-EC2-For-Application-Nodes-Hosting-Golang-App

0 stars 0 forks source link

NGINX-As-Reverse-Proxy-On-AWS-EC2-For-Application-Nodes-Hosting-Golang-App

1.First will deploy Golang app to 2 x application nodes on Amazon EC2.

sudo yum update -y
sudo yum install -y golang
export GOROOT=/usr/lib/golang
export GOPATH=$HOME/projects
export PATH=$PATH:$GOROOT/bin
package main

import (
    "fmt"
    "net/http"
    "os"
)

func handler(w http.ResponseWriter, r *http.Request) {
    h, _ := os.Hostname()
    fmt.Fprintf(w, "Hi there, I'm served from %s!", h)
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8484", nil)
}
Hi there, I'm served from <application node hostname>!

2.Nginx As Reverse Proxy to be deployed on a web node.It should load balance the traffic to 2 app servers or nodes deployed above hosting Golang app.

sudo apt-get install -y nginx
sudo systemctl status nginx 
ls -la sites-enabled/
$ sudo vi /etc/nginx/sites-available/default
upstream backend {
        server <application_node1_IP>:8484;
    server <application_node2_IP>:8484;
        }
server {
        listen 8484 default_server;
        listen [::]:8484 default_server;
{
        proxy_pass http://backend;
     }      
sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl status nginx