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
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
* Connected to localhost ( 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 that configured inside
# vim /etc/nginx/conf.d/
server {
  server_name; # 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; // 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/; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/; # 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 = {
      return 301 https://$host$request_uri;
  } # managed by Certbot

  listen       80;
  listen       [::]:80;
  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

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!


Popular Posts