Switching from Apache to Nginx

The Setup

I recently decided that I wanted to learn about Nginx. You may know that Nginx is a web server that has been growing in popularity in the last few years. People use it as an alternative to more traditional web servers like Apache or IIS. They even use it in conjunction with Apache or IIS with Nginx acting as a reverse proxy. Nginx excels at serving up static content and can use fewer resources than Apache or IIS if properly configured, in some scenarios. The one drawback that I had heard about with Nginx was it doesn’t have the breadth of modules that you find in Apache. To illustrate one of the differences, under Apache PHP is loaded as a module while Nginx loads it via FastCGI. Using FastCGI Nginx is able to get comparable performance with potentially lower resource usage.

In terms of configuration Apache is certainly an acquired taste, but once you get it, it is not all that complicated and many of the directives are well documented in the Apache docs. Coming from Apache, configuration of Nginx is surprising easy to pick up. Several things that take 2 or 3 lines on Apache take only 1 on Nginx.

# Under Apache if I want my server to respond using
# jimshaver.net as well as www.jimshaver.net
ServerName jimshaver.net
ServerAlias www.jimshaver.net
# Under Nginx if I want my server to respond using
# jimshaver.net as well as www.jimshaver.net
server_name jimshaver.net www.jimshaver.net;

This is a simple example, but it illustrates how Nginx is arguably more practical about things.

Migrating my site

So how did I migrate this site to Nginx? I actually installed Nginx, made sure it was off and copied the Apache configuration files from /etc/apache2/sites-available/ to /etc/nginx/sites-available/. Then I opened the config file in a text editor and started building the Nginx config within the same file. Line by line searching the net translating the Apache directives into Nginx directives. Once I had added the line in the Nginx half of the config file, I would comment out the Apache directive to help me keep track. This is probably not the most efficient way to do this, but it is a good way to learn and also be more certain that you got all of the directives converted. The one thing that was a little cryptic was rewrite rules. I would recommend that you google around for an .htaccess to Nginx converter

Nginx doesn’t yet have an equivalent for the Apache enable and disable scripts(unless you write your own), but linking from the site-available to sites-enabled was simple enough. I then turned off Apache started up Nginx and after a few adjustments it seems to be working well. I haven’t noticed a decrease in RAM usage on my server but I have noticed decently faster page loading times.

Another thing that I really like about Nginx was the documentation. Don’t get me wrong Apache has everything documented, but while Apache’s documentation is heavy on text and has examples sparsely sprinkled throughout the page, Nginx is the opposite a lot of really good examples. I was not expecting the documentation to be as good as it was.

Conclusion

I’m going to continue to use apache when the situation warrants it, but I’m very impressed with the features, configuration and documentation of Nginx. I think it will be my preference for projects going forward. If nothing else I’m glad that there is competition and inovation in this space. Most people have never heard of a web server, and yet it is a critical piece of software these days… For everything!

What’s next?

Varnish! Varnish doesn’t support TLS for, in my opinion, stupid reasons, but if you configure Nginx to do TLS termination as a reverse proxy you can proxy requests internally over http, within the web server and have varnish work its caching magic.

Photo Credit the Nginx Wiki

3 thoughts on “Switching from Apache to Nginx

Leave a Reply