X marks the 'ngin'
Tags: beginners, nginx, javascript
Published: 2022-07-06 08:45:39

## Getting Started

**Web server**: The computer that is serving web pages.

A web server delivers requested web pages. Users send requests along their local area network, and that request gets sent along to the webserver at the URL. That webserver will process the request and generate a response containing the requested URL's `/endpoint` index.

There are a plethora of webserver software out there, including XAMPP, Apache, Nginx, Tornado, Caddy, Microsoft Internet Information Services (IIS), and many more! The one you choose is typically up to personal preference, but today I'm going to focus on Nginx.

Nginx is an open source software developed in 2002. It is a webserver used for reverse proxying, caching, and load balancing. Nginx was designed with maximum performance and stability in mind. A few of the benefits of using Nginx are that it functions as a proxy server for emails. It uses a non-threaded and event driven architecture. It's simple to use and reduces overall load times for your clients. 

## Architecture

Architecture of nginx: master-slave architecure. Nginx supports asychronous, event driven, and non-blocking model

![Nginx Archetecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vnddee9xd9yipqsen5k.png)

Reasons to use Nginx:
-Easy to install and maintain
   the installation process is only three simple steps and you only need to remember one command to start/stop/restart the server.
-Reduced wait times for users
-Improved Performance
  if you run multiple instances of the same server, nginx can route traffic for you in such a way that increases your overall speed.
-Load Balancing
  some commercial load balancing programs are expensive. Nginx is free.
-Scalability
   Some webservers are only able to handle a set number of concurrent requests. Nginx does not have that limitation and can will not throttle speeds depending on the number of incoming requests.
-No downtime during upgrades.
   Nginx is able to restart without any downtime at all. Less downtime means less waiting and confusion for your clients.
 

## Config
Core settings of Nginx are mainly configured in the `nginx.conf` file. The config is mainly structured in contexts. Some of the most important parts of the config file are: 
Worker processes
-defines the number of worker processes that nginx will use.
-usually equal to the number of CPU cores
worker connections
-maximum number of simultaneous connections for each worker processes.
-tells worker processes how many people can simultaneously be served by nginx.
access & error log
-files used to log any erros and access attempts. Used for debugging.
gzip
-settings for gzip compression of nginx responses.


## How to Install Nginx in 3 Steps:

Step 1: Install Nginx
`sudo apt-get install nginx`
Step 2: Adjust Firewall
`sudo ufw enable`
Step 3: Check your server.
`sudo ufw app list`
You should see a list of a few different applications. Nginx HTTP allows traffic along port 80 for unencrypted web traffic. Nginx HTTPS allows traffic along port 443 for encrypted traffic.
`sudo ufw allow 'Nginx Full'` is an example of the command to choose which configuration is choses.
You can use the command `sudo ufw status` to see the status of your firewall as well as all allowed traffic.

## Manage the Nginx process
You can check that status of Nginx with the command `sudo systemctl status nginx`
this will output a message about nginx service and reverse proxy is active as well as some other useful information.

You're essentially setup! One of the last steps is defining your configuration and making sure your server files are accessible by nginx.

Navigate to and create a new file /etc/nginx/sites-available/**YOUR APP NAME**. Inside of this file you'll need to define a few parameters.

```
server {
listen 80 default_server;
listen [::] default_server;
root /var/www/**YOUR FILE NAME**;
index index.html;
server_name **YOUR SERVER NAME**;
location / {
try_files $uri $uri/ =404;
}
```
We need to make a copy of that file and put it in our sites-enabled directory. Use the command:
`sudo ln -s /etc/nginx/sites-available/**YOUR FILE NAME** /etc/nginx/sites-enabled/**YOUR FILE NAME**`

Lastly, there's already a default file being served on port 80. You'll have to move that file out of the /etc/nginx/sites-enabled directory to somewhere else.

`sudo mv /etc/nginx/sites-enabled/default /home/default`
This command will move that file out of the sites-enabled directory and put it in your home directory.

Now, restarting nginx with the command `sudo systemctl restart nginx` you can restart the nginx server (with no downtime) and it should now display the application you were attempting to serve.