Welcome dear reader, today we walk through the most useful Nginx configuration practices for hosting your web applications. Nginx is a fast, lightweight web server that also acts as a reverse proxy and load balancer — but a few bad defaults can slow your site down or expose it to unnecessary risks. Let’s make sure your server blocks are clean, fast, and secure.
Table of contents
- What is Nginx and How Does It Work?
- How to Organize Your Server Blocks
- Which Performance Settings Should You Tune?
- Which Security Headers Should You Set?
- Conclusion
What is Nginx and How Does It Work?
Nginx (pronounced engine-x) is a web server that receives HTTP requests and answers them — either with static files such as images and HTML, or by forwarding the request to your application. It uses an event-driven architecture, meaning a single Nginx process can handle thousands of simultaneous connections without spawning a thread per request. This is what makes it so fast.
Configuration happens in text files, usually under /etc/nginx/. The main file nginx.conf loads smaller files from conf.d/ and sites-enabled/, where each website lives in its own server block.
How to Organize Your Server Blocks
A server block defines one website. A typical block for a PHP application looks like this:
server {
# (1) Domain names served by this block
server_name example.com www.example.com;
# (2) Document root and entry file
root /var/www/public_html;
index index.php index.html;
# (3) Static assets: cached by the browser
location ~* \.(css|js|png|jpg|svg)$ {
expires 30d;
}
# (4) Hand off PHP files to PHP-FPM
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
}
The best practice here is one file per site in sites-available/, and a symbolic link into sites-enabled/ — exactly the same pattern as Apache’s virtual hosts in our guide on how to install WordPress on a VPS server.
Which Performance Settings Should You Tune?
Three settings give you the most benefit with the least effort. First, enable gzip compression in nginx.conf so text assets travel compressed:
gzip on; gzip_types text/css application/javascript application/json;
Second, set sensible timeouts so slow clients do not tie up workers:
client_body_timeout 12s; keepalive_timeout 15s; send_timeout 10s;
Third, prefer UNIX sockets over TCP for local PHP-FPM connections (as in the server block above) — it skips the network stack entirely.
After any change, validate the syntax before reloading:
# Test the configuration, then reload without downtime sudo nginx -t && sudo systemctl reload nginx
Which Security Headers Should You Set?
Finally, add a few security headers to your server block. They tell browsers to protect your users, and they cost you nothing:
add_header X-Content-Type-Options nosniff; add_header X-Frame-Options SAMEORIGIN; add_header Referrer-Policy strict-origin-when-cross-origin;
Also, make sure your server does not reveal its version — set server_tokens off; in the http block.
Conclusion
That’s already it for our tour of Nginx best practices. Keep one server block per website, enable gzip and tight timeouts, use UNIX sockets for PHP-FPM, and add the basic security headers. With these habits, your Nginx setup will stay fast and safe as it grows. Leave a comment below if you have any questions or feedback about the content of this article.
At re:Software S.L., we create innovative solutions for businesses that aim to digitize their processes and routines, and our expertise with nginx helps us maintain your servers uptime and bandwidth.

