Introduction to Nginx and Unicorn
- Amruta Bhaskar
- Dec 8, 2020
- 0 Bemerkungen)
- 202 Ansichten
Nginx is a pure web server that's intended for serving up static content and/or redirecting the request to another socket to handle the request.
Unicorn is a Rack web server and only intended to host a 'Rack App' which is usually generating dynamic content. Rack apps can also serve up static content but it's less efficient than most other traditional web servers.
Most RoR setups use a combination of both traditional web servers and Rack servers to apply the best of both of their capabilities. Nginx is incredibly fast at request redirection through proxy balancing and serving up static content. Unicorn is quite capable of processing HTTP headers and balancing inbound requests to Ruby for processing.
Nginx: A high performance free open source web server powering busiest sites on the Internet. nginx [engine x] is an HTTP and reverse proxy server, as well as a mail proxy server, written by Igor Sysoev. According to Netcraft Nginx served or proxied 30.46% of the top million busiest sites in Jan 2018; Unicorn: Rack HTTP server for fast clients and Unix. Unicorn is an HTTP server for Rack applications designed to only serve fast clients on low-latency, high-bandwidth connections and take advantage of features in Unix/Unix-like kernels. Slow clients should only be served by placing a reverse proxy capable of fully buffering both the request and response between Unicorn and slow clients. Nginx and Unicorn can be categorized as "Web Servers" tools.
"High-performance HTTP server" is the top the reason why over 1437 developers like Nginx, while over 80 developers mention "Fast" as the leading cause for choosing Unicorn.
Nginx and Unicorn are both open-source tools. It seems that nginx with 9.1K GitHub stars and 3.43K forks on GitHub has more adoption than Unicorn with 1.35K GitHub stars and 249 GitHub forks.
Unicorn follows the Unix philosophy which is to do one thing and do it well, and that is to serve fast, low-latency clients. The fact that Unicorn is designed for fast, low-latency clients also implies that it's not very good with slow, high-latency clients, which is indeed true. This is one of Unicorn's weak points and it's where a reverse proxy comes into play: it sits in front of Unicorn and takes care of those slow clients.
Fortunately, such a reverse proxy already exists and is called Nginx.
The decision to handle only fast clients greatly simplifies the design of Unicorn and allows a much simpler and smaller codebase, at the cost of some added complexity on the deployment.
An alternative decision could be designing Unicorn in such a way that it wouldn't need a reverse proxy. However, this means that it would have to implement extra functionality to do all the things that now Nginx does, resulting in a more complex codebase and more engineering efforts.
Instead, its creators decided to leverage existing software that is battle-tested and very well designed and to avoid wasting time and energy on problems already solved by other software.