Nginx ~ Introduction - rohit120582sharma/Documentation GitHub Wiki

Nginx is the open source web server. This is not just a robust, fast, high-performance web server that can work on low-profile, cheap hardware. It is also a reverse-proxy, load balancer server.

It is defined as an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server.

In order to use and enjoy the power of Nginx, you don't have to drop your existing web server or to totally change your infrastructure. It can nicely fit into your setup and enhance it.


What can Nginx do?

  • Reverse Proxy

    • It can work alongside other web serves (like Apache) as a reverse proxy. A reverse proxy is a service that stands between the client and the web servers. It receives the request from the client on behalf of real web server and sends it to the real web servers behind it for processing. When the response is received from the web servers after processing, it replies it back to the requesting client. Reverse proxies are used for performance and security reasons because it involves caching and other algorithms.
  • Load balancing

    • A load balancer is a device/service that distributes traffic load on two or more web servers. That provides fault tolerance and increases performance. There are commercial hardware load balancers available but they are very expensive and somewhat complex to setup and service. Nginx can easily act as a robust load balancer that will cut off costs yet give you the needed load distribution.
  • Scalable concurrent connections handling

    • Nginx was created for managing huge number of concurrent requests.
    • It uses the asynchronous way of serving requests.
    • In traditional web servers (like IIS or Apache), a new thread is created for every request received. So if you have 1000 requests at the same time, that's 1000 threads working concurrently. If the maximum number of threads the server can handle is 1000, any further requests will have to wait till one of the previous requests has finished before it can get served. That means increasing the wait time. With Nginx, only one thread is used but it is based on events (more on that later) which greatly improves speed.
    • Traditional web servers (like Apache and IIS) can serve incoming requests without a problem, until the number of concurrent requests reaches a certain limit (say 1000). Here concurrent requests refer to connections that are all using the server resources at the same time. Whenever this number rises, performance starts to degrade. Nginx does not suffer from this problem and it can easily handle increasing number of concurrent requests.
  • Serving large files or streaming media

    • As mentioned, other web servers create a thread for each new request. What is the request was to serve a 1 hour long video or download a several-gigabytes file? That will certainly block the thread till the download/stream is complete. Several thousands similar requests are enough to make further requests wait in a queue. However, because of the asynchronous nature of Nginx, this is never a problem.
  • SSL encryption

    • When using SSL, an extra overhead is required by the web server to decrypt/encrypt the content it receives and sends. With Nginx acting as a reverse proxy, you can offload this SSL work to be done on Nginx. The backend web server no longer has to work with SSL encrypted data; as reverse proxy will do that.

Some more advantages?

  • Acceleration

    • If you already have two or more web servers running the same application, Nginx can accelerate performance by routing traffic to those web servers in a way that enhances the overall speed.
  • The ability to operate on relatively cheap hardware

    • Nginx can be deployed on servers with very limited hardware capabilities and still perform much better that its counterparts on the same hardware.
  • On-the-fly upgrades

    • Yes, Nginx is one of the very few systems that can patched or upgraded without having to take a downtime and disrupt your business. Personally, I would use Nginx for my environment just for that reason.
  • Ease of installation and maintenance

    • As we move on, you’ll find that Nginx is relatively easier than you think when it comes to basic (and even advanced) usage.


Nginx vs. Apache

A common misconception is that Nginx and Apache are totally interchangeable. That Nginx is just an enhanced web server so once you learn it you should drop Apache and start using Nginx instead. This couldn't be more wrong! Most of the time you’ll see Nginx working side-by-side with Apache. Sometimes you have to use Nginx solely in areas where only Nginx can operate, some other times you have to use Apache. It all depends on the type of project you are working on and what you are trying to accomplish.

Apache creates a separate child process for each request. Each process uses a blocking thread. That means Apache falls short in performance when it comes to serving a considerably huge number of concurrent connections. Nginx on the other hand avoids this problem by creating multiple, non-blocking worker threads, each is able to serve thousands of concurrent connections.

Because of the way Nginx is designed, it requires fewer hardware resources than Apache.

Apache is more popular on operating systems than Nginx. For example, all Linux flavors have Apache available in their officials repositories. On the other hand, and while it is easy to download and install, Nginx still requires some tweaking on the OS side before it can be installed using package management.

Nginx can act as a load balancer and/or a reverse proxy. Having multiple Apache web servers in the backend and using one or more Nginx servers in front of them as a reverse proxy will give you the best of both worlds. Apache alone does not have this capability.

Apache has a long history of supporting dynamic content languages like PHP, Ruby, Python and others. Nginx, while it does support PHP, will require some extra effort from your side, the administrator, to get things working.

There are subtle differences between both web servers when it comes to configuration. Apache translates the URL to a file path on the underlying OS. For example: www.example.com/plants/fruits/apples/red.html would be interpreted as requesting a file called red.html located in /var/www/html/plants/fruits/apples, assuming that the web root directory is /var/www/html. On the other hand, Nginx does not work like this; it parses the URL in a different way. That makes placing an .htaccess file, for example, in the plants directory, let’s say to deny users from hot linking image files, is totally useless to Nginx.



⚠️ **GitHub.com Fallback** ⚠️