Ngnix - High Performance Web Server, Proxy Server, Content Cache and Reverse Proxy

  •        0
  

We aggregate and tag open source projects. We have collections of more than one million projects. Check out the projects section.



Nginx is a High Performance Web Server, Proxy Server, Content Cache and Reverse Proxy server. It can also be used as mail proxy server and a generic TCP/UDP proxy server. Nginx claims to be more efficient and faster in the Web space compared to the other web servers. This can be evident with the architecture which is based on asynchronous event-driven approach. The event driven architecture enables to scale to hundreds / thousands of concurrent connections.

How it works:

Nginx webserver has the following components.

  1. Master process – Reads configuration, bind to ports and create child processes
  2. Cache Loader process – Runs at startup / scheduled conservatively to load disk-based cache
  3. Cache Manager process – Runs periodically and prunes entries from disk caches to manage
  4. Worker process – Handles network connections, reads and writes content to disk and communicate with upstream server

Each worker process is provided with a set of listen sockets by the master process. The worker process begin by waiting for events on the listen sockets. Events are initiated by new incoming connections which are assigned to a state machine.

Why Ngnix is faster:

Typically Linux applications are built as process or threads which working in “blocking” mode. However with Nginx, the worker process handles this in non blocking mode as described below:

Events occur on sockets and worker process handles them

  1. An event on the listen socket means that a client has initiated a new connection reques
  2. An event on the connection socket means that a client has made a HTTP request.

Each new connection creates another file descriptor and consumes a small amount of memory in the worker process. These processes can be pinned to process CPUs and hence context switches are very minimal.

Ngnix as Load Balancer:

In a cloud environment usually there will be one or more web servers to serve the user request. Ngnix will be placed in front of all web servers and it will balance the incoming request to the available web servers. It supports session persistence (sticky session) where in the request from same client / IP will be routed to same backend server. Web server maintains session, from login to logout all request from that client will be routed to the same backend server.

Ngnix as Proxy server and Content Cache Server

In production environment the actual web servers will not be exposed to public. There will be a proxy server or CDN will be placed in-front of the web servers. Client will hit the proxy server which in turn proxies to the backend server. Static content like images, CSS, Javascript etc may not change frequently. Ngnix can cache the static content so that the request will be directly served from cache and it does not need to hit the backend server.

Installing Ngnix:

On Debian / Ubuntu:      

sudo apt-get install nginx

 

On Redhat / Centos:       

sudo yum install nginx

 

Start / Stop / Restart Service:

sudo service nginx {start | stop | restart | reload }

 

To view the status:

sudo service status nginx

 

Simple Configuration:

Ngnix conf file will be located at /etc/nginx/nginx.conf. If it does not exist there, it may also be at /usr/local/nginx/conf/nginx.conf or /usr/local/etc/nginx/nginx.conf. Below example has a simple configuration where nginx shows the capability of Reverse proxy and also a load balancer.

 

user       www www;  ## Default: nobody

worker_processes  5;  ## Default: 1

error_log  logs/error.log;

pid        logs/nginx.pid;

worker_rlimit_nofile 8192;

 

events {

  worker_connections  4096;  ## Default: 1024

}

 

http {

  include    conf/mime.types;

  include    /etc/nginx/proxy.conf;

  include    /etc/nginx/fastcgi.conf;

  index    index.html index.htm index.php;

 

  default_type application/octet-stream;

  log_format   main '$remote_addr - $remote_user [$time_local]  $status '

    '"$request" $body_bytes_sent "$http_referer" '

    '"$http_user_agent" "$http_x_forwarded_for"';

  access_log   logs/access.log  main;

  sendfile     on;

  tcp_nopush   on;

  server_names_hash_bucket_size 128; # this seems to be required for some vhosts

 

  server { # simple reverse-proxy

    listen       80;

    server_name  domain2.com www.domain2.com;

    access_log   logs/domain2.access.log  main;

 

    # serve static files

    location ~ ^/(images|javascript|js|css|flash|media|static)/  {

      root    /var/www/virtual/big.server.com/htdocs;

      expires 30d;

    }

 

    # pass requests for dynamic content to rails/turbogears/zope, et al

    location / {

      proxy_pass      http://127.0.0.1:8080;

    }

  }

 

  upstream big_server_com {

    server 127.0.0.3:8000 weight=5;

    server 127.0.0.3:8001 weight=5;

    server 192.168.0.1:8000;

    server 192.168.0.1:8001;

  }

 

  server { # simple load balancing

    listen          80;

    server_name     big.server.com;

    access_log      logs/big.server.access.log main;

 

    location / {

      proxy_pass      http://big_server_com;

    }

  }

}

 

Updating Configuration:

Updating configuration is simple, lightweight and a reliable operation with Zero downtime of application. Whenever there is a new config available and when master process receives a signal to reload the config, the following sequence takes place.

  1. Reloads configuration and forks new worker processes. These new worker processes begin accepting connections and process traffic
  2. Signals old worker processes to gracefully exit. As soon as HTTP request is complete, the worker process shuts down the connection, and when all connections are closed, worker process exits.

 

Reference

https://www.nginx.com/

https://www.nginx.com/blog/testing-the-performance-of-nginx-and-nginx-plus-web-servers/

 


   

We publish blog post about open source products. If you are interested in sharing knowledge about open source products, please visit write for us




Related Articles

An introduction to web cache proxy server - nuster

  • web-cache proxy-server load-balancer

Nuster is a simple yet powerful web caching proxy server based on HAProxy. It is 100% compatible with HAProxy, and takes full advantage of the ACL functionality of HAProxy to provide fine-grained caching policy based on the content of request, response or server status. This article gives an overview of nuster - web cache proxy server, its installation and few examples of how to use it.

Read More


Appserver.io – The First Multithreaded Application Server for PHP written in PHP

  • appserver application-server php

What if you could reliably run PHP without Nginx or Apache, but also without relying on its internal server? What if you could do async operations in PHP with true multi threading, fully taking advantage of multi core processors without hacks or a jungle of callbacks? What if you had drag and drop installation support for your PHAR packaged web apps in an environment identical to its production counterpart? Welcome to appserver.io – the worlds first open source application server for PHP.

Read More


Univention Corporate Server - An open source identity management system

  • ucs identity-management-system

Univention Corporate Server is an open source identity management system, an IT infrastructure and device management solution and an extensible platform with a store-like App Center that includes tested third party applications and further UCS components: This is what Univention combines in their main product Univention Corporate Server, a Debian GNU/Linux based enterprise distribution. This article provides you the overview of Univention Corporate Server, its feature and installation.

Read More


Lucene Vs Solr

  • searchengine lucene solr

Lucene is a search library built in Java. Solr is a web application built on top of Lucene. Certainly Solr = Lucene + Added features. Often there would a question, when to choose Solr and when to choose Lucene.

Read More


SeoToaster: easy, fast and efficient open source CMS for top SEO performance

  • cms content-management-system seo ecommerce

SeoToaster is a free Open Source CMS & Ecommerce solution to build, manage and market websites optimized for for top search engine performance. As the name implies, Seo Toaster is to date the only content management system (CMS) to truly integrate SEO execution and web marketing automation technology in full compliance with the search engines industry’s best practices.

Read More



Scale up vs Scale out. First decision to make before moving to cloud

  • cloud scale-up scale-out scalability

Before moving to cloud, First decision to make is whether Scale up the hardware or Scale out with multiple systems. Both has its pros and cons.

Read More


LogicalDOC - Open Source DMS

  • dms document-management-system

LogicalDOC is both a document management and a collaboration system. The software is loaded with many functions and allows organizing, indexing, retrieving, controlling and distributing important business documents securely and safely for any organization and individual.

Read More


ONLYOFFICE Document Server, an online office app for Nextcloud and ownCloud

  • office office-suite word spreadsheet

ONLYOFFICE Document Server is a free collaborative online office suite including viewers and editors for texts, spreadsheets and presentations, fully compatible with Office Open XML formats (.docx, .xlsx, .pptx). This article provides you the overview of ONLYOFFICE Document Server, its features, installation and integration with Nextcloud and ownCloud.

Read More


Should web application store images in Database or File system?

  • database image-store filesystem

Web developers most frequent question, Should user images be stored in database or file system? Which is the best way. Both has some pros and cons.

Read More


PrestaShop - A feature rich Open Source eCommerce solution

PrestaShop is an Open Source eCommerce Solution. It comes complete with over 310 features that have been carefully developed to assist business owners in increasing sales with virtually little effort. It is being used in more than 150,000 online stores.

Read More


GreenMail - Email Test Framework in Java

  • email email-server test automation

In any project there will be a need to send mail out to users. It could be an alert mail, forget password or authentication related mail. Mail is the default communication between the software and the users. As a developer, we can write code, to send out a mail but we need to make sure whether it got successfully received and how the body of mail, Is it the same like what we have sent. GreenMail is a Email test framework which helps to send and receive mails. It is a test framework which supports SMTP, POP3, IMAP including SSL.

Read More


Whats new in Lucene / Solr 4.0

  • lucene solr new-release

The release 4.0 is one of the important milestone for Lucene and Solr. It has lot of new features and performance important. Few important ones are highliggted in this article.

Read More


Is ZooKeeper mandatory for Cloud

  • zookeeper distributed cloud

Cloud is nothing but more than one system or application distributed across the network, across the globe. It may have couple of application servers, database server, shared data storage, backup server and lot more. The resources in the distributed environment must have information about each other so that they could co-ordinate and share without any issues. ZooKeeper helps to do that.

Read More


AbanteCart - Easy to use open source e-commerce platform, helps selling online

  • e-commerce ecommerce cart

AbanteCart is a free, open source shopping cart that was built by developers with a passion for free and accessible software. Founded in 2010 (launched in 2011), the platform is coded in PHP and supports MySQL. AbanteCart’s easy to use admin and basic layout management tool make this open source solution both easy to use and customizable, depending on the skills of the user. AbanteCart is very user-friendly, it is entirely possible for a user with little to no coding experience to set up and use this cart. If the user would be limited to the themes and features available in base AbanteCart, there is a marketplace where third-party extensions or plugins come to the rescue.

Read More


Top 15 Open source alternative to Microsoft products

  • microsoft-alternative open-source-enterprise

Microsoft is monopoly in the commercial software. Here are 15 best alternatives to most popular and widely used Microsoft products.

Read More


An Introduction to the UnQLite Embedded NoSQL Database Engine

  • database nosql embedded key-value-store

UnQLite is an embedded NoSQL database engine. It's a standard Key/Value store similar to the more popular Berkeley DB and a document-store database similar to MongoDB with a built-in scripting language called Jx9 that looks like Javascript. Unlike most other NoSQL databases, UnQLite does not have a separate server process. UnQLite reads and writes directly to ordinary disk files. A complete database with multiple collections is contained in a single disk file. The database file format is cross-platform, you can freely copy a database between 32-bit and 64-bit systems or between big-endian and little-endian architectures.

Read More


8 Best Open Source Searchengines built on top of Lucene

  • lucene solr searchengine elasticsearch

Lucene is most powerful and widely used Search engine. Here is the list of 7 search engines which is built on top of Lucene. You could imagine how powerful they are.

Read More


How to solve CommunicationsException in Java while using Hibernate and MySQL

  • java hibernate mysql communicationsexception timeout

You might have faced CommunicationsException, basically timeout issue in Java while connecting to MySQL using Hibernate. The session would be timed out after certain period of time. You might be thinking that the your site or application is running without any issue but it would have stopped or crashed due to exception.

Read More


10 Free services for your Website / Blog. Just plug it.

  • free website blog free-service free-resources

Each website / blog delivers useful content or service to its users. But website themselves requires some service to monitor and increase its presence. Here are few free services which could be used by Website / Blog. This will be very much helpful for small business owners.

Read More


10 sites to get the large data set or data corpus for free

  • search test-data large-data-set data-corpus dataset

You may require GBs of data to do performance or load testing. How your app behaves when there is loads of data. You need to know the capacity of your application. This is the frequently asked question from the sales team "The customer is having 100GB of data and he wants to know whether our product will handle this? If so how much RAM / Disk storage required?". This article has pointers to the large data corpus.

Read More