Changing form Apache to NGINX – Part 1

I’m currently in the process of switching my webserver from Apache with mod_php to the much faster NGINX with PHP-FPM, as I’m finding BugMuncher is running out of memory on the old apache set up.

I’ve been using Apache on webservers for as long as I can remember, so switching to NGINX has been a bit odd so far, especially understanding requests. So I’ve decided to write a series of posts documenting the lessons I learn, partly so I can refer to them, but also for anyone else making the switch to NGINX.

Virtual Host Config files

Like Apache 2, NGINX can use separate config files for each server, and uses the sites-available / sites-enabled directories system. However this is where the similarities end, NGINX config files resemble JSON, using curly braces to group settings. White space is used to separate variable names from values, and each variable declaration must be ended with a semi-colon.

All the guides I read online just gave an example config file, but never fully explained what was going on. I’m going to try and break it down, and explain what’s happening.

Each virtual host will live in a server block, which is similar to Apache’s ‘VirtualHost’ tag:


server {
    # listen on port 80
    listen 80; 

    # Where the website lives, much like 'DocumentRoot' in Apache
    root /srv/www; 

    # The host name to serve, same as 'ServerName' in Apache
    server_name bugmuncher.com;
}

 

Now, at this point most guides will tell you to add a location block to your server block, but actually this is all you need! If you use this config file and put an index.html in /srv/www it will be accessible from bugmuncher.com

This was the most important lesson for me, as all the guides I read, including those on wiki.nginx.org seemed intent on making you put location blocks in your server. Now don’t get me wrong, location blocks are very powerful, and hugely important to most set ups, but they are not required. Once I realised this, everything started dropping into place.

Posted in: Programming,Technology by Matt Bearman on 8th February 2012 at 11:25 am
Tags: , , , ,

No Responses to “Changing form Apache to NGINX – Part 1” - (Leave a comment)

No comments yet.

RSS | TrackBack URL

Leave a comment