A quick guide on optimizing NGINX for WordPress
NGINX is a much better for WordPress sites than Apache, however, there are still a couple of things you can do to speed up your WordPress websites running on NGINX.
I will show you some great tips to optimize your NGINX server to make your WordPress websites run as fast as possible.
Enable GZIP Compression
GZIP compression is normally disabled by default. Enabling GZIP Compression will compress HTML content, images, CSS and javascript on your webpages.
Follow these simple steps to enable GZIP Compression on NGINX.
-
-
- Login to your server via SSH with terminal or putty.
- Run the following command:
sudo nano /etc/nginx/nginx.conf
- Scroll down the file until you see the GZIP settings as seen below:
- You will see in the file that GZIP is already enabled (gzip on;), however everything is commented out so although GZIP is technically enabled, it is not doing the job. Simply remove all the # characters (uncomment every gzip line). Check the screenshot below to see how the file should look to enable GZIP Compression:
- Save the file and exit.
- Now restart NGINX using the following command:
sudo service nginx restart
-
That is all there is to it. GZIP Compression is now enabled on your NGINX server.
Enable Static File Caching
Adding caching to CSS, javascript, image and HTML files will increase page loading times. This is done by adding some code to your server block.
-
-
-
-
-
- To find the server block for your domain name, enter the sites enabled directory using the following command:
cd /etc/nginx/sites-available/
dir
- You should see a list of all your domains. Now use the following command to enter the server block of your domain name:
sudo nano /etc/nginx/sites-available/domainname.com
(Replacing domainname.com with your domain name).
- Now add the following code inside the first server section. If you followed my guide on how to build a website, you can simply enter this code below the line ”
try_files $uri $uri/ /index.php?$args;
}”
Enter the following code:
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 365d; }
Your file should look like this:
- Now save and exit the file.
- Restart NGINX using this command:
sudo systemctl reload nginx
- To find the server block for your domain name, enter the sites enabled directory using the following command:
-
-
-
-
You have now added static file caching to your website. With both GZIP Compression and static file caching enabled your website should load much faster. There are more ways to optimize NGINX, however the two main optimizations are now complete. Check how much faster your website loads now after these little NGINX tweaks.