Skip to main content

Setup Nginx as Proxy Reverse

Continuing the journey of Web Server Application, today we would setup Nginx as Proxy Reverse. If you want to start with how to install Nginx and configure it with SSL/TLS, read here.
This post scoped only in CentOS 7 and I'm using Alibaba Cloud as my cloud provider. Using other Operating System (OS) or cloud provider may need some adjustment to be matched, but I'll describe in general. If you have a trouble during follow this tutorial, don't hesitate to ask in the comment. Let's dive in!

Step 1 Run Go Application

To make this tutorial simple, we gonna use go application to be proxy reverse by our Nginx. Since go application are binary, so they could directly running on our server without need to install any dependency. I have prepared the pre-compiled binary of go application that listen http request on port 8080 for linux OS. So let's download it (because we're not in administrative task, I recommend you to use normal user instead of root user).
$ curl -o main -L https://github.com/h3rucutu/simple-hello-world-go-native/releases/download/v0.1.0/main-linux-amd64-0.1.0
After downloaded the binary, add executable permission to this file.
$ chmod +x main
Then run it in background.
$ nohup ./main &
Verify that our go application are running.
$ curl -w "\n" -v localhost:8080
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Thu, 25 Jun 2020 09:38:22 GMT
< Content-Length: 27
< 
* Connection #0 to host localhost left intact
{"message": "Hello World!"}
Great, our go application are running well.

Step 2 Setup Nginx as Proxy Reverse

This step continuing my post before (read: here). Change our mode into root.
$ sudo -s
[sudo] password for h3rucutu:
#
Then edit the configuration of Nginx (since we want to use domain example.caltic.tech that configured inside example.caltic.tech.conf).
# vim /etc/nginx/conf.d/example.caltic.tech.conf
server {
  server_name example.caltic.tech; # managed by Certbot
  root        /usr/share/nginx/html;

  # Load configuration files for the default server block.
  include /etc/nginx/default.d/*.conf;

  location / {
    // Add code start here
    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_pass http://172.31.5.163:8080/; // Change this IP with your IP Private Server
    proxy_read_timeout 90;
    proxy_http_version 1.1;
    proxy_request_buffering off;
    // Add code stop here
  }

  error_page 404 /404.html;
  location = /40x.html {
  }

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
  }

  listen [::]:443 ssl ipv6only=on; # managed by Certbot
  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/example.caltic.tech/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/example.caltic.tech/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
  if ($host = example.caltic.tech) {
      return 301 https://$host$request_uri;
  } # managed by Certbot

  listen       80;
  listen       [::]:80;
  server_name example.caltic.tech;
  return 404; # managed by Certbot
}
Then save your file, test Nginx configuration and restart Nginx.
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# systemctl restart nginx
Go to your favourite browser and navigate to example.caltic.tech.

Our Go Application are proxy reverse by Nginx

Congratulation now our go application are proxy reverse by Nginx! This tutorial simulate that any application that you want to proxy reverse by Nginx, whatever your application is written. Just mapped in the server block of Nginx configuration to the local port where your application is running.
Later more on this blog. Stay tune!

Comments

Popular posts from this blog

Share Connection eth0 on Kali

When I was still using Backtrack as pentest Operating System on my netbook, I need to configure everything about networking, include sharing connection over eth0 interface. I must type on the terminal to set up the interface, then set the IP and netmask, etc. After setting the interface connection, I also must to configure  iptables to forwarding the internet connection from the other interface into eth0 , and the last step I must turn on forwarding rule. It's really fun, although need several step to do it. But, when I am using Kali Linux, I realise that the step which can I do on backtrack doesn't running on Kali Linux. After learn several option about networking on Kali, finally I can share connection over eth0 on Kali Linux. Okay, the first assume that we must have connected internet (not using eth0 ). Next, when your connection are ready, you can right click on the networking icon, select Edit Connection. Edit Connection When the window "Networking Connectio

Bypassing Nokia Maps N9

Nokia N9 have maps application, both of Nokia Maps and Nokia Drive are related one to other. But the same problem with Nokia Drive on Nokia N9 also occur in Nokia Maps (see here ). Nokia Maps also need logged in Nokia Account before you can use Nokia Maps. So I think the same way with bypassing Nokia Account in Nokia Drive will be succeed too in Nokia Maps. The same step can be read on my previous post here , until preparing the configuration Nokia Drive download. Okay let's go! First, make sure you have installed Filebox and setting to show hidden files and show root filesystem. Open browser on your N9 and download Modified Nokia Maps Configuration here . Don't press clear when the download has finished, just tap Done. Go to Filebox, and navigate to /home/user/MyDocs/Downloads , you'll found Maps.conf there, copy and paste this file into /home/user/.config/Nokia . But I recommend you if there's existing Nokia Maps Configuration backup it into Maps.conf.bak the

Fixing Ralink Driver on Kali Linux

Okay, now i'm going to make Kali Linux as primary OS on my laptop, not longer Backtrack. Yes, i have decided to migrate from Backtack into the new version of Backtrack, named Kali Linux. This is my first tutorial of Kali Linux on my blog. So let's begin to fixing an error about Ralink wireless driver when we installing Kali Linux. Kali Linux Desktop When we are on the network configuration page from installation Kali Linux, the PC which used Ralink wireless will be notice that the driver need external source to be installed at installation of Kali Linux, in order to the wireless work normally. To satisfy that condition, we need the external source which contain anything about Ralink driver. As we know, Kali Linux is based on Debian Wheezy, so i was searching all about Ralink wireless driver. Ahaaa, when i had read from one of them, i found the package (see  http://wiki.debian.org/WiFi ). Let's try to identify where our file which we needed. On this tutorial, we w