Introduction
In this article, we will make some Nginx configurations to improve your website’s response time and speed. The goal is to increase the Google PageSpeed score and achieve a score above 80⁄100.
Requirements
- A server with Ubuntu 16.04 installed.
- Having root privileges.
- nginx web server.
Step 1 - Getting the PageSpeed Score
You can get your website’s current PageSpeed score from this URL.

Step 2 - Enabling Compression
If your CSS, JavaScript and image files are large, the amount of data that site visitors need to download will increase. This can cause slowness. Compression reduces this data to a smaller, more compact version. Gzip compression is a method that can be used in Nginx. When Gzip compression is enabled, browsers can download content faster; therefore, it helps increase the score on PageSpeed.
To enable compression, open your site’s Nginx configuration file with nano or a similar text editor:
sudo nano /etc/nginx/sites-available/defaultYou will encounter a configuration like the following.
server {
listen 80 default_server;
listen [::]:80 default_server;
…
}Enter the following lines below the code and set the compression level.
server {
listen 80 default_server;
listen [::]:80 default_server;
gzip on;
gzip_comp_level 5;
}
For the compression value, you can choose a number between 1 and 9. The value 5 is a good value in terms of CPU usage and provides approximately 75% compression for most ASCII files (approximately the same as level 9).
…
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;Let us also explain these few lines we added:
The gzip_min_length value is entered to prevent small files from being compressed further.
The gzip_proxied value enables compression for those using a proxy like Cloudflare.
gzip_vary is entered for browsers to cache both the compressed content and the normal version.
Finally, specify the MIME types for the content you want to compress. We compress images, JSON data, javascript files and other common content:
server {
listen 80 default_server;
listen [::]:80 default_server;
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
application/atom+xml
application/javascript
application/json
application/ld+json
application/manifest+json
application/rss+xml
application/vnd.geo+json
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/bmp
image/svg+xml
image/x-icon
text/cache-manifest
text/css
text/plain
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy;
# text/html is always compressed by gzip module
location ~* .(jpg|jpeg|png|gif|ico|css|js|pdf)$ {
expires 7d;
}
}
We entered the location line for browser cache configuration.
The entire configuration we entered will be like this. Save and close the file.
You can check whether there are any erroneous lines in the configuration we entered with the following command;
sudo nginx -tThen we restart the nginx service for the configurations we entered to become active.
sudo systemctl restart nginxStep 3 - Measure the Results
To check which PageSpeed score these configurations affect, analyze your website again through PageSpeed. You will see that the compression and browser caching warnings are gone:

Conclusion
Our goal was to achieve a score above 80. If your results are still below 80 points after making these configurations, there may be other settings you need to pay attention to. The score you receive will vary depending on the content of your website. Google PageSpeed will tell you what needs to be adjusted. Changing your Nginx configuration is just one method of improving PageSpeed and may not be sufficient on its own.
Leave a Comment
* Your comment will be published after approval.
Comments
0No comments yet. Be the first to comment!