Understand NGNIX

Arnav Zek
3 min readDec 6, 2020

--

NGNIX (pronounced Engine X) is a web server /reverse proxy/load balancer used by many high traffic applications because it can handle thousands of simultaneous requests with its event-driven architecture.

It is faster than Apache and openLightSpeed in most areas

How does it work?

It instances a worker process for each CPU core that run concurrently and handle requests is a non-blocking fashion

How to configure it?

//Installation
sudo apt-get update
sudo apt-get install nginx

Next, we need to reconfigure our firewall software to allow access to Nginx, for this we use “Uncomplicated Firewall” (UFW)

We can list the applications configurations that ufw knows how to work with by typing:

sudo ufw app list
  • Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)

To allow port 80 types the following command

sudo ufw allow 'Nginx HTTP'

You can verify the change by the following command

sudo ufw status

Check your Web Server

We can check with the systemd init system to make sure the service is running by typing

systemctl status nginx

“systemctl” stands for system control and “systemd” which plays with the word System D “System D is a manner of responding to challenges that require one to have the ability to think quickly, to adapt, and to improvise when getting a job done”

Configuration

The configuration is stored in

sudo nano /etc/nginx/sites-available/default

The areas that these brackets define are called “contexts”

A virtual server is defined by a server directive

The server configuration block usually includes a listen directive to specify the IP address and port on which the server listens for requests.

If a port is omitted, the standard port is used. Likewise, if an address is omitted, the server listens on all addresses.

NGINX tests the request’s Host header field against the server_name to find the appropriate server block for each request

If the Host header field does not match a server name, NGINX routes the request to the default server block which is the first one

NGINX Plus can send traffic to different proxies or serve different files based on the request URIs. These blocks are defined using the location directive placed within a server directive

A location context can contain directives that define how to resolve a request – either serve a static file or pass the request to a proxied server

You can use variables in the configuration file

A variable is denoted by the (dollar) sign at the beginning of its name.

With the error_page directive, you can configure NGINX Plus to return a custom page along with an error code

error_page 404 /404.html;

listen 80 - The server block will listen for incoming connections on port 80 for the specified domain.

There is one more place where the configuration is stored

/etc/nginx/nginx.conf

If you want to upload large files to the server you ill have to update the above file with the following code

client_max_body_size 200M;

Redirect Domains

server {
server_name .mydomain.com;
return 301 http://www.adifferentdomain.com$request_uri;
}

Restart NGINX

/etc/init.d/nginx restart//orservice nginx restart

We can also do A/B testing with nginx with split_clients context

Resources

http://nginx.org/en/docs/beginners_guide.html

--

--