# Nginx

## &#x20;What is a web-server

* When a user search for a page on a web-browser
  * Request is initiated by browser, leaving your LAN (Local Area Network)
    * DNS Lookup
    * TCP3 Handshake
    * SSL4 Handshake
    * HTTP and Get Request
  * Request then travel through Global Area Network
  * Then reaches to computer/server which needs to serve the request
  * Similar route in reverse direction for response
* The server/computer that serve the web-pages or provide response to requests is web-server
* Every web server has an IP address and a Domain name
* Request is served by web-server, an application is required to make a computer, a web-server
* Few examples for web-server software in market:&#x20;
  * XAMPP
  * Apache
  * Nginx
  * Tornado
  * Caddy
  * Microsoft IIS

## what is Ngnix

* Open source software, objective is to reduce web-page load time
* Used for:
  * Reverse proxying, Caching, Load Balancing
  * Provides HTTP server capabilities
  * Also functions as proxy server for email (IMAP, POP and SMTP)
* Mainly designed for maximum performance and stability
* Uses a non-threaded and event-driven architecture

## Nginx Architecture

* Uses master-slave architecture by supporting event-driven, asynchronous, and non-blocking model

![](https://679135566-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ma8GKQU92FWew4_578V%2F-Mf1CmANKyvjRfs6aMVQ%2F-Mf1CvbvGk5qLyx8_6PS%2Fimage.png?alt=media\&token=435cd575-65d1-4d2c-9585-24e41f6d572a)



**Master: Responsible for**&#x20;

* Reading and validating configuration
* Starting, terminating and maintaining required number of workers  and number of processes within each worker
* Re-configuring without any service interruption
* Re-opening for log files
* Compile embedded for scripts

**Cache Loader: responsible for**

* Checking on-disc cache items and populating nginx in-memory database with the cache metadata
* Prepares nginx instances to work with files already available in the disc, arranged in specific allocated directory structure
* Cache loaded traverses the directory structure, checks cache content with the meta-data, updates relevant entry and also checks if everything is clean when system exists

**Cache-Manager: responsible for**

* Cache expiration and termination management

## **Why to use Nginx**

* Easy to install and maintain
* Improves Performance
* Offers Scalability
* Reduce the wait time for users
* Load Balancing
* On the fly upgrades (no downtime when upgrading)

## Nginx Installation

* Install Nginx
* Adjust Firewall
* Check your server
* Manage the Nginx Process

```bash
sudo apt-get update
sudo apt-get install nginx # install nginx
sudo ufw enable # enable firewall
nginx -v # check nginx installation status
sudo ufx app list # listing apps with firewall

sudo ufw allow "Nginx full" # to enable both encrypted and unencrypted graphic

sudo ufw status
sudo systemctl status nginx
```

### Manage Nginx Processes, Basis Commands

```bash
sudo systemctl stop nginx # stop nginx web-server
sudo systemctl restart nginx # stop and start the service again
sudo systemctl reload nginx # if making configuration changes, reload without stop
sudo systemctl disable nginx # To not let nginx automatically start when server boots
sudo systemctl enable nginx # to enable server automatically start at boot
sudo nginx -t # to test config file
```

### Important Directories and Files

#### **Content**

* _**/var/www/html**_: The actual web content, which by default only consists of the default Nginx page you saw earlier, is served out of the /var/www/html directory. This can be changed by altering NGINX configuration files.

#### **Server Configuration**

* _**/etc/nginx**_: The NGINX configuration directory. All of the Nginx configuration files reside here.
* _**/etc/nginx/nginx.conf**_: The main NGINX configuration file. This can be modified to make changes to the NGINX global configuration.
* _**/etc/nginx/sites-available/**_: The directory where per-site “server blocks” can be stored. NGINX will not use the configuration files found in this directory unless they are linked to the sites-enabled directory (see below). Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory.
* _**/etc/nginx/sites-enabled/**_: The directory where enabled per-site “server blocks” are stored. Typically, these are created by linking to configuration files found in the sites-available directory.
* _**/etc/nginx/snippets**_: This directory contains configuration fragments that can be included elsewhere in the NGINX configuration. Potentially repeatable configuration segments are good candidates for refactoring into snippets.

#### **Server Logs**

* _**/var/log/nginx/access.log**_: Every request to your web server is recorded in this log fil unless and until NGINX is configured to do otherwise.
* _**/var/log/nginx/error.log**_: Every NGINX error will be recorded in this log only.

### Configuration Settings

* Core settings of the Nginx are mainly configured in the nginx.conf file
* The configuration file is mainly structured into contexts
* Most important settings are:
  * worker\_processes (How many workers, depends on number of core)
  * worker\_connections (How many requests can be served simultaneously)
  * access log and error log (for debugging)
  * gzip (compression of nginx responses)

### Load Balancing with Nginx

### Reverse Proxying with Nginx

## More Topics

### Web-Application Firewalls

### Cryptographic Sections - Certificates and SSL Terminations

### Security Aspects Related to Web-Servers

## Performance Tuning and Response Monitoring

## Reference for Further Reading

* Edureka Youtube Video Lecture
* [https://medium.com/adrixus/beginners-guide-to-nginx-configuration-files-527fcd6d5efd](https://medium.com/adrixus/beginners-guide-to-nginx-configuration-files-527fcd6d5efd)
