Thoughts on :Nginx
Published: 2021-11-14 15:41:36






Fantastic way to wrangle some good software product is to experience it firsthand.So lets try Nginx today.And i have a weired hobby of connecting my fav software products with comic characters.

I have choosen Green Latern as Nginx 
*"All the powers that the Power Ring gives"*


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyvb3k7qaxlsfia9gg9z.gif)

Nginx [engine x] is a free and opensource high performance web-server that can be used as 
- Reverse proxy
- Load balancer
- SMTP proxy
- TCP/UDP proxy server. 

There is a commercial edition available for Nginx called as Nginx Plus.

## Source Code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ep5cfgm7z278055hyq2c.PNG)

Nginx source code is published on mercurial http://hg.nginx.org/nginx/file/tip , May be its new info for your, Mercurial is a free multiplatform distributed version control system. Mercurial is used via the command line; the program file itself is called hg.So like git we can download the nginx code using below command line.

```hg clone http://hg.nginx.org/nginx```

If you want to build the nginx from the source code please follow the link http://nginx.org/en/docs/howto_build_on_win32.html


## NGINX Configuration file
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztjhatnc467han9qtnw3.PNG)
NGINX can be configured with nginx.conf file, By default this file is named as nginx.conf and placed under /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.

The Nginx configuration file consist of Directives and Contexts 

## What are Directives and Contexts?

NGINX configuration consists of key-value pairs called directives. Directives decides which configuration to apply.They can be organized and grouped into Blocks known as Contexts.

Contexts are tree like structures that can be nested within one another and Directives can only been used within Contexts.

Directives and Context looks like below:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9j4ldxjyzfeqxyn0t231.PNG)

## Nginx Architecture
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8s47uh7okcdzvtwqx0hc.PNG)
Nginx has one master process and several worker processes.

The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. 

Worker processes do actual processing of requests. nginx employs event-based model and OS-dependent mechanisms to efficiently distribute requests among worker processes. 

The number of worker processes is defined in the configuration file and may be fixed for a given configuration. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzsojpf3envtpujswu64.PNG)
IF we mention auto like below the number of worker processes automatically adjusted to the number of available CPU cores
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgh96lgpr4dip6a5nm6n.PNG)

Each worker process can open a by default 512 connections

As previously mentioned, nginx doesn't spawn a process or thread for every connection. Instead, worker processes accept new requests from a shared "listen" socket and execute a highly efficient run-loop inside each worker to process thousands of connections per worker

## Extending Nginx:

Nginx can be extended by using thrid-party modules.There is a large ecosystem of third‑party modules, ranging from language interpreters to security solutions.

Have a look at the below nginx blog about compling dynamic modules for nginx
https://www.nginx.com/blog/compiling-dynamic-modules-nginx-plus/