27 Jul 2018
Web Server

Apache vs Nginx

A Web server is a program that uses HTTP to fetch files that form web pages and serve them to the client requesting these pages. The basic objectives of a Web Server are to store, process and deliver web pages to the users. The web server stores the HTML documents, Images, Style-sheets and JavaScript files

Apache is the most widely used web server. It is free and open source. Functionality for Apache is introduced through modules. Modules can be installed to extend the server’s capabilities. To add a new module, you need to install it and restart the Apache server. Functionality that you don’t need or want can be removed. This helps to keeps the server small, consumes fewer resources and memory, and makes the server less prone to security holes.

When a request comes in, the server does not know which module is responsible for handling a specific request and will ask each module whether they can process a request. The modules will reply and the server will assign the request to a module according to the reply given by the modules.

One server can host many websites. To achieve this, every one of those websites has to be assigned a different name, even if they map to the same machine.

NGINX is open source software. It can be used as a web server, reverse proxy, and a load balancer.

Igor Sysoev wrote NGINX to solve the C10K problem, a term coined in 1999 to describe the difficulty that existing web servers experienced in handling large numbers (the 10K) of concurrent connections (the C)

Nginx consists of modules which are controlled by directives. Directives can be simple directives and block directives. A simple directive consists of the name and parameters separated by spaces and ends with a semicolon. A block directive has the same structure as a simple directive, but instead of the semicolon, it ends with a set of additional instructions surrounded by braces ({ and }). If a block directive has other directives inside braces, it is called a context

Events Context: The “events” context is used to set global options that affect how Nginx handles connections at a general level. There can only be a single events context defined within the Nginx configuration.

HTTP Context: HTTP context is contained within the “main” context. This context will contain all of the directives and contexts necessary to define how the program will handle HTTP or HTTPS connections.

Server Context: The “server” context is declared within the “HTTP” context. It allows for multiple declarations. The reason for allowing multiple declarations of the server context is that each instance defines a specific virtual server to handle client requests.

Nginx has one master process and several worker processes. The master process can read and evaluate configuration, and maintain worker processes. Worker processes do the actual processing of requests.

Apache uses .htaccess files for directory-specific configurations in the directory that houses the files. Nginx does not allow these files as it causes security problem if misconfigured. As a result, all configurations defined in a .htaccess file must be placed within the location block in the server context.

Apache has many functionalities but it takes up a lot of memory. Nginx is more secure and faster than Apache, however, it cannot do any processing on its own. You can have Apache and Nginx working together on the same server to leverage each server’s strengths, but you can’t have them listening on the same port.

Nginx is in the front, listening on port 80. Nginx accepts user connections and can act as what is referred to as a reverse proxy. A reverse proxy will pass requests through Nginx to your backend server. This can be your Apache server. Nginx is fast for serving static content, if the request is only for static data, Nginx will serve the request. If any processing needs to be done, the request will be forwarded to Apache server. Apache server will do all the processing and return the reply back to Nginx, which will then send the reply to the client.

Archana Gudi

Post Comments

* marked fields are mandatory.