WordPress and Nginx a recipe for speed

Until recently I have been running WordPress exclusively behind Apache because I had a hard time finding an Nginx config that “just worked” with WordPress. I’ve also been running all of my NodeJS apps behind an Nginx reverse proxy because I just like Nginx better. I feel like it is easier to configure and much faster performance wise. Anyway, in this article I wanted to share my config for getting a WordPress site up and running behind Nginx. Here goes the code.

For the purposes of this example I am working on my local machine in my dev environment. I am only focusing on running one site on my local machine. First you will need to make sure that you have PHP installed as well as a matching version of php-fpm. In my case I have PHP 7.2 and php-fpm7.2. Now for my site specific config

[email protected]:~$ sudo nano /etc/nginx/sites-enabled/default

server {
    listen 80;
    listen [::]:80;
    server_name localhost;

    root /home/murph/wordpress-rescues/iliveinaterminal.com; #The root path to your site, same as document_root in apache
    index index.php;

    # log files
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ { #This will change based on your php configuration so make sure to check it.
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires max;
        log_not_found off;
    }

}

And that’s it!. That should be all you need to get a WordPress installation up and running behind Nginx. Now that I have this config I want to work on swapping out Apache for Nginx on my production servers!

Scroll to Top