Main Content

Testing Out Nginx, WordPress & Varnish

If you’ve been reading my blog, or following me on Twitter, than you know I’m now managing my own server and I’m having some issues. It’s not this site, it’s a different one. I’ve tried a lot of things to make the site better and it seems better, but will sill randomly run out of memory, the CPU will spike, and the whole server turns into a brick until I reboot it.

So what’s next? How about a different server? How about Nginx?

nginx logo

I can say I know very little about servers, and even less about Nginx. But DigitalOcean allows me to create a new virtual server in a minute and they also have a great tutorial on how to get the server setup and WordPress installed. So I decided to give it a try.

Nginx is supposed to do a better job at memory management as its event-driven not process-driven. Nginx is like Apache in the fact that it can run a lot of the same software, but there are some configuration differences with Nginx.

One thing that you need to realize is that Nginx doesn’t use .htaccess files. They can do all the things a .htaccess file can do, but it has to be done a bit differently and put into the virtual host configuration file on the server. Plus you have restart Nginx after editing.

To convert .htaccess file to Nginx I found this nice converter. I put in some 301 redirects and it gave me Nginx code which worked great.

varnish logo

Once that was setup, I installed Varnish; again following a tutorial. Varnish is a super fast caching system. However, Varnish hates cookies where as WordPress loves them. So when you go through the configuration, they tell you to pretty much kill all WordPress cookies except if you’re on the login or admin screens. Turns out, that cause some issues.

Things like the theme customization screen doesn’t work, post previews don’t work, and the toolbar is missing from the site. Those are pretty big in my book.

Turns out that Tim Whitlock also has a Varnish & WordPress tutorial and his outlines how to ignore Varnish all together for logged in users. I highly suggest taking his Varnish cookie code so your WordPress performs as it should.

Like any caching, Varnish also has a tendency to cache things when you don’t always want them cached. If you were to update a post, the users won’t always see the updated copy. To get around that, I installed Varnish HTTP Purge which should remove the cache file when I post an update to existing content. This has worked well, but I’m still having an issue where the site’s not seeing an update to a plugin and my sidebar widget is incorrect.

So far my Nginx has been good. Everything is installed and running on my test server and I’m going to be testing over the next few weeks. My goal is to have the server serving up files fast and furious while keeping the memory and CPU down.

If you’ve been interested in Nginx I’d suggest heading over to DigitalOcean and setting up a virtual machine. For $5 a month it’s a cheap way to get a Nginx server up and running. If after a month you no longer want the server, cancel and you’re only out $5.

Leave a Reply