Nginx

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:

    • 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

Master: Responsible for

  • 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

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

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

Performance Tuning and Response Monitoring

Reference for Further Reading

Last updated

Was this helpful?