Nginx Definitive Guide: Tutorial included

Nginx Definitive Guide: Tutorial included

Book Icon - Software Webflow Template
6
 min read
Nginx Definitive Guide: Tutorial included

Browser makes a request to an NGINX Server, and NGINX server makes a request to Airbnb Server,

AirBnB server responds to the NGINX Server, giving it all the content, and NGINX sends back the Clients Browser

We treat NGINX as a Web Sever but in technical terms its a Reverse Proxy.

Why do we need NGINX

Technically you could just send data without NGINX straight to your server, but its a bad idea when you are scaling.

When you get a lot of traffic, we need to increase the amount of servers inside our AWS Account.

If we only have one server, we would receive so many requests that we would receive a bottleneck, increasing latency.

So we can increase the amount of servers we have, lets say we add two more servers, this in turn increasing the complexity as we would need to handle quite a bit of under the hood modifications and require deep understanding.

Alternatively we use NGINX which handles this for us, we only communicate with one instance of NGINX, NGINX forwards the request to a known server,

this is known as a load balancer. NGINX has Load Balancer functionality.

Encryption

HTTP vs HTTPS: If you have multiple servers, you would have to encrypt every single one, thats why we use NGINX. Instead of having to encrypt / decrypt process every single server, we can have it in NGINX, make a request NGINX handles the encryption / decryption process for us

Why Use NGINX: TOP 2, ENCRYPTION HANDLING / LOAD BALANCER 

WHY TO USE REVERSE PROXY 

NGINX Tutorial:

Please note while I will provide setup initialization for all, for this simple example I'm running it locally on my Mac.

Install and Start NGINX: Mac:
brew install nginx
brew services start nginx
Ubuntu server:
sudo apt update
sudo apt install nginx
sudo systemctl start nginx
Windows: Download NGINX: https://nginx.org/en/download.html cd C:\nginx nginx.exe Note that on Windows, NGINX might not run as a background service by default. You can manually start and stop it using the nginx.exe executable.

After running brew services start nginx

You will see the following in the CLI and nginx and will be prompted with a message that NGINX is running on port 8080

If you inspect element and refresh Network processes, you will see Server running and its nginx.

On Mac it will show it as a background process in General: Allow in the Background:

Terminology for nginx.conf

The following inside the curly brackets are all directives belonging to context. Eg Directive listen inside the server directive.

What we're saying in http -> server is once we go to Port: 8080, serve the content of root

http {
        server {
            listen 8080;
            root /User/garo/Desktop/mysite;
        }
    }
events {}
worker_processes
worker_connections
default_type

Location Block

Allows us to specify certain endpoints to serve different types of html elements.

When you go to localhost:8080/foods we're requesting to serve the index.html inside the foods/ directory

http {
        server {
            listen 8080;
            root /User/garo/Desktop/mysite;
            
            location /foods {
            		root /User/garo/Desktop/mysite;
            }
        }
    }
events {}

sites-available vs sites-enabled

In Nginx, the sites-available directory contains individual configuration files for various websites or applications that you want to host on your server. However, these configuration files are not automatically used by Nginx until they are enabled. The sites-available directory acts as a repository of available configuration templates that can be enabled when needed.

To make Nginx use the configurations stored in the sites-available directory, you need to create symbolic links (also known as symlinks) to these configuration files in the sites-enabled directory. Nginx reads the enabled configuration files from the sites-enabled directory and processes them during startup.

Creating a symlink:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

With these links, keep modifying your files in the sites-available and the link will automatically update in sites-enabled

Check if symlink exists: 

run the command: ls /path/to/directory/symlink_name

If the symlink exists there will be an L in lower case in the front of the read/write/execute permissions.

lrwxrwxrwx 1 user group   20 Aug 15 11:30 symlink.txt -> /path/to/files/original.txt

ubuntu@ip-numbers:/etc/nginx/sites-enabled$ ls -la
drwxr-xr-x 2 root root 4096 Jun 28  2022 .
drwxr-xr-x 8 root root 4096 Aug 15 10:30 ..
lrwxrwxrwx 1 root root   34 Jun 24  2022 default -> /etc/nginx/sites-available/default

default in sites-enabled is a symlink and pathed from target file sites-available


Removing a symlink

cd /etc/nginx/sites-enabled

sudo rm symlink_name

Helpful Nginx commands:

Check if everything is running okay
sudo nginx -t

Check nginx servcie status

sudo systemctl status nginx.service

check error log
sudo tail -100 /var/log/nginx/error.log


Check IP port conflicts
sudo lsof -i :80 -i :443

Restart Nginx

sudo systemctl restart nginx

Look for Duplicates

In the instance you are running multiple subdomains on the same web server, you can find all instances in /etc/nginx with this grep command

grep -r "subdomain.domain.services" /etc/nginx


/etc/nginx/sites-available/subdomain.domain.services.save:  server_name subdomain.domain.services;
/etc/nginx/sites-available/subdomain.domain.services.save:# server_name subdomain.domain.services;
/etc/nginx/sites-available/subdomain.domain.services:       server_name subdomain.domain.services;
/etc/nginx/sites-available/subdomain.domain.services:       server_name subdomain.domain.services;
/etc/nginx/conf.d/proxy.conf:        server_name devices.domain.services;
/etc/nginx/conf.d/proxy.conf:    if ($host = subdomain.domain.services) {
/etc/nginx/conf.d/proxy.conf:        server_name subdomain.domain.services;


Conf.d Directory

Commonly used to store individual configuration files that are included in the main Nginx configuration. These files can contain various configuration blocks and directives tailored for specific needs. This organization makes managing configurations more modular and easier to maintain.

proxy.conf file

Contains configuration settings related to reverse proxying. Nginx can be set up to act as a reverse proxy, which means it takes incoming client requests and forwards them to specific backend servers. This can be useful for load balancing, caching, or providing additional security layers.

Newsletter - Software Webflow Template

Subscribe to our newsletter now!

Thanks for joining our newsletter.
Oops! Something went wrong.