Discovering Nginx: A Developer’s Guide to Maximizing Your Web Server Potential
Nginx is something that most software developers have used at some point in their careers. However, there is a difference between working with Nginx and really understanding it. Nginx offers many advanced features that most people are not aware of and, therefore, not utilizing.
In this post, I’ll go over some of the unique features that Nginx offers and how they can help you take your web application to the next level.
What is Nginx?
Nginx is an open source web server. It’s currently the market leader for web servers, followed by Apache. Nginx offers a fair number of plug-ins that provide extra features that are both interesting and functional. Unfortunately, many of the most powerful plug-in features are not widely known, and are therefore not used by the average developer.
Basic Features of Nginx
The basic Nginx features that most users are aware of include:
Serving Files: As a web server, this is naturally the most obvious and widely used feature that Nginx offers. After all, it wouldn’t be a very good web server if it didn’t have the capacity to serve files.
Handling Redirects: As is expected of web servers, Nginx is also able to complete redirect requests.
Serving Multiple Hosts: Nginx offers multiple server blocks with different names and ports. This is nice from the development side because you often have multiple sites on a single server. When the server is directing a request to a specific host, it uses the line by line configuration to look through the server block to find the one with the matching port and server name.
Slightly More Advanced Features of Nginx
In addition to the three base functions that Nginx offers, most developers are typically aware of some of the slightly more advanced features, including:
Proxy Requests to Somewhere: This is one of the more commonly-used advanced features. It’s a rather powerful feature because it’s actually invisible to the client. For example, for one of our projects, we use a local Nginx configuration proxy so that we could pass the requests from the web browser to the port the application uses. In this setup, the application can thus run on whatever port it prefers. The user doesn’t see this interaction and it simply works in their web browser.
URL Rewriting: This feature can be combined with other features because of the line by line nature of the Nginx configuration. We have used URL rewriting in conjunction with other features on a number of projects. For example, we have rewritten URLs before proxy to another host to ensure the ported format was correct. We have also rewritten URLs before redirecting to preserve the args during the redirect.
Super Advanced Features of Nginx
Finally, some of the lesser known and most unique features that Nginx has to offer include:
Independent Request Authorization: This is a really cool feature because it allows you to separate your auth servers from the server that’s actually servicing the request for content. In most cases, it only passes the header of the request to the auth server because the body of the request is not relevant.
There are two interesting things about this feature. The first is that the process is invisible to the client, which means that the user experience is not altered by the use of this feature. The second is that if the auth server rejects the request for any reason, it never even passes the request to the application server. This takes a lot of load off of the application server and maintains an efficient user interface (UI). By splitting out your authorization invisibly to the client, it allows you to break up a monolithic application into separate parts in a very easy straightforward fashion.
Dynamic Web Server Plug-In: Nginx itself only supports static things. However if you want to evaluate things on demand, Nginx has integration capabilities with numerous dynamic web server plug-ins. There are two main options that we typically use on our projects:
- Passenger Plug-In: The Passenger plug-in is a ruby web server that integrates as a subset of Nginx instead of running as a separate application.
- Fastcgi_pass: The Fastcgi_pass plug-in is commonly used to evaluate php programming languages in Nginx.
Caching: When caching is enabled, a subset of data is stored in a high-speed data storage layer to allow future requests to be returned more quickly than if it was retrieved from the primary location. Caching can reduce the load on a proxy service and speed up duplicate requests.
While caching is extremely beneficial for web servers, most people don’t know how it’s implemented. One of the easiest ways to implement caching is directly through Nginx using relatively easy code.
Real Time Messaging Protocol (RTMP): RTMP, also known as streaming, is the feature that I think is the most fun. If you want to do a live stream, you can actually use Nginx to put the stream directly on your own device instead of streaming through Twitch. You can also use a ruby web server to perform authorization so that only authorized users with a username and password can view your stream.
Nginx can also stream directly to Twitch using the RTMP plug-in. To steam to Twitch, it uses your server as a hop between your local machine and Twitch. Whether you stream to your device or to Twitch, RTMP is a really cool, unexpected feature on Nginx.
Overall, Nginx offers a wide variety of fascinating features that, if you’re willing to explore, can help you create a fantastic web application.