Starting with Nginx

How I deploy my 2 applications with different domains on a single instance.

Starting with Nginx

What is Nginx ?

Nginx is pronounced as "Engine X." It does web serving, load balancing, and acts as a reverse proxy. We know web serving is delivering content to clients, just like a server. But what is reverse proxy??? Lets see below.

What is Proxy?

Making our work done by others, it’s like assigning our work to others in our absence. For example: what if your attendance is said by your friend everyday, and your at home, so he is a proxy, he says the attendance behalf of you.

Forward Proxy

The proxy server communicates with the internet on our behalf. It keeps all the clients devices in a private network, It acts like a firewall protecting us from malware and viruses on the internet. It manages all the clients' requests to the internet and also hides the client's IP address. VPNs are similar; they encrypt data end-to-end and provide more security. They can be remote VPN servers handling all these requests.

Why do people use VPNs? 😎 (To access TikTok or other banned apps and websites in their country :)

Reverse Proxy

So what’s reverse proxy, you can guess it know, its just reverse. So the proxy serves to the internet on behalf of the actual web servers. The clients don’t know where does their requests go, as there are so many web servers in the private network

Why are the web servers in the private network ??

Just like the features we get in forward proxy the security, malware, virus attacks from the internet. It also acts as load balancing, it balances all the load, distributing to all the servers.

Reverse proxy visualisation

image source: cloudflare

What is Load balancing ?

It’s just balancing the load, as it acts as reverse proxy which is maintaining all the servers in a private network, every request which comes to the proxy server is distributed based on the load, if there are 1000 requests coming it balances the load to all the servers. So all the reverse proxies are load balancers. A reverse proxy can even work with a single web server, but a typical load balancer needs more than one server.

image source: AppviewX

Installation of Nginx on a AWS EC2 instance.


Few points before installation

  • Make sure your able to SSH into your instance, or your able to access your instance.

  • Make sure you're able to send requests to your application running on the instance. For example if your nodejs application is running on port 8000, just check if it is accessible in the Internet, like this you_public_ip:8000

  • Our case is accessing both the applications running on different ports on the instance, which we can directly do it by their port number, but we are dealing with domain names, two different domain applications running on the same instance.

  • In our case make sure you have a domain name, you can also use nginx without a domain name, but there is no use of nginx without a domain name, as you can directly access the applications by their port number

Lets start the installation

Run these commands in the ubuntu AWS instance.

sudo apt update 
sudo apt install nginx

This install the nginx onto the server.

Try visiting your public ip address (14.232.235.148)

By default the nginx server runs on port 80

So where does all this text (html, css) come from where is the code written, where is the default configuration?

This is it go to root /etc/ubuntu/etc/nginx/sites-available/default

As you can see here the default server is running on port 80, and the default html page comes from the root /var/www/html. Below is the file.

Lets go to the nginx.conf file and explore

go to root etc/nginx/nginx.conf file

  1. Events

    The events contains the worker connections, which is the maximum number of simultaneous connections a worker can have. A worker is a process which handles clients requests. It works on the master slave architecture, where the slaves are the workers, the number of workers is the number of cores (could be anything but by default its equal to number of cores). This doesn’t use event loop architecture for asynchronous process, it actually uses epoll or kqueue for simultaneous handling of asynchronous processes.

  2. Http

    It contains the settings of how to handle http and https requests from the client. Some of the properties like sendFile → optimise the file transfer using the os capabilities etc, the ssl settings contains the ssl, tsl protocols.

3 Mail

It acts as proxy for mail servers, which means the the requests coming from the email clients like (gmail, yahoo) are gone through nginx. It acts like a middle person. It does load balancing.

Before adding the servers here are few things to note

  • Map you domain in the AWS route 53, or any dns service provider (this maps your public ip address with the domain name)

  • You don’t need to have a ssl certificate, its not necessary, you can create one after adding the servers

Lets add the servers.

Adding these settings in the http section to handle the http and https traffic.

server {
    server_name backend.xyz; # (change this) The domain name you want to use for this server

    location / {
        proxy_pass http://localhost:3000; # (change this) The port of the application running comes here 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    }

server {
    server_name vegho.xyz; # (change this) The domain name you want to use for this server

    location / {
        proxy_pass http://localhost:8080; # (change this) The port of the application running comes here 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    }

That’s it done, you have successfully added a reverse proxy, you have configured nginx for routing the domain with their respective server.

(Optional) You can also add SSL certificates to these servers by using certbot.