At the end of last year, this blog was migrated to a new server, hosted by DigitalOcean. The old server ran on Apache but on the new server I wanted to try out the LEMP One-click application.
LEMP stands for Linux, NGINX (say: en-juhn-ex), MySQL, PHP. The old server ran on Apache, so some things needed to be migrated from Apache to Nginx.
Differences between Apache and NGINX
The biggest difference between Apache and NGINX (for me) is the fact that Apache can be configured using .htaccess files and NGINX can't. That means that any logic contained in the .htaccess file(s) must be migrated to the NGINX Server Block. A server block is the NGINX equivalent of Apache's Virtual Host.
If you want to learn more, read this excellent article about the differences between Apache and Nginx.
The old .htaccess file
So, let's have a look at the old annotated .htaccess file. There was only one file, which was located in the
The equivalent NGINX Server Block
And here is the NGINX Server Block that accomplices the same thing.
Let's point out some differences that stood out for me:
- The NGINX Server Block contains all .htaccess logic but also the logic that was in the Apache Virtual Host. This explains why the
rootdirectives (among others) are necessary.
- There are three
serverdirectives: 1 that redirect non-https traffic, 1 that redirects traffic to the www-domain, and the last 1 that actually serves the website.
- The order of the
locationdirectives influences how requests are handled. See below for a link that explains how NGINX selects the server and location that will handle a request.
- The content types for the Expire headers have changed. This might not be due to NGINX, it is worth watching out for this if you are migrating a website.
Learn more about NGINX
One of the strong points of NGINX is that it is very opinionated. This means the docs are very clear about good and bad configurations. Please read the following links carefully if you plan a migration yourself.
- Docs for core NGINX directives. Check out
- Docs for rewrite NGINX directives. Check out
- An explanation of how NGINX picks the
locationfor each request.
- Read about pitfalls and common mistakes.
- Why the NGINX
Ifdirective is evil.
Did you think this article was helpful? Can my server block be improved or do you have some additional resources? Let me know in a comment or join the discussion on Reddit!