Hardened Nginx Server Block for Security & High Performance
- Category Configs
- Type CONF
- Platform Linux, Windows, macOS, Unix
- Language nginx
- Price Free
- Download 849
- Comments 0
The Necessity of a Hardened Nginx Production Server
When launching a web application into a live production environment, relying on a default web server configuration is one of the most dangerous risks a developer can take. Default setups prioritize broad compatibility over strict security, leaving your infrastructure highly vulnerable to automated botnets, Cross-Site Scripting (XSS) attacks, and brute-force intrusion attempts. Implementing a "Hardened Nginx Server Block for Security & High Performance" fundamentally transforms your basic web server into an impenetrable, military-grade fortress that simultaneously delivers lightning-fast page load speeds to your users.
Enforcing Universal Secure Connections with HTTP Redirects
The foundation of a secure web server begins by completely eliminating unsecured plain-text traffic. In the very first block of this configuration file, Nginx is explicitly commanded to listen on port 80 (the global standard for unencrypted HTTP). Whenever a user types your domain into their browser without the "https://" prefix, the return 301 https://$host$request_uri; directive instantly interrupts the connection. It responds with a Permanent 301 Redirect status code, forcing the browser to instantly reload the exact same URL over a highly secure, encrypted HTTPS pipeline.
Configuring Modern SSL and TLS Protocols for Encryption
Inside the primary port 443 server block, the configuration applies aggressive cryptography rules. It completely disables outdated, easily compromised protocols like SSLv3, TLSv1.0, and TLSv1.1, exclusively enforcing the robust TLSv1.2 and cutting-edge TLSv1.3 standards. Furthermore, by carefully defining strict ssl_ciphers and disabling ssl_session_tickets, this hardened configuration effectively neutralizes sophisticated Man-in-the-Middle (MitM) attacks, guaranteeing that sensitive data—such as user passwords and credit card numbers—cannot be intercepted or decrypted by hostile third parties.
Implementing OWASP Compliant Security Headers
Perhaps the most powerful security upgrade in this snippet is the inclusion of strict, OWASP-compliant HTTP headers. Directives like X-Frame-Options "SAMEORIGIN" physically prevent your website from being maliciously embedded or "clickjacked" inside a hidden iframe on a hacker's domain. The incredibly potent Strict-Transport-Security (HSTS) header acts as a long-term contract with the user's browser, mandating that the browser must never, under any circumstances, attempt to load your domain over an unencrypted connection for an entire year (31,536,000 seconds).
Accelerating Load Times with Advanced Gzip Compression
A truly hardened server must also be remarkably fast; poor performance severely damages both user experience and technical SEO rankings. To solve this, the script natively enables the Nginx gzip engine. By setting gzip_comp_level 6, the server strikes the perfect mathematical balance between aggressive file compression and CPU processing power. Before serving heavy CSS, jаvascript, or JSON payload files to the client, Nginx compresses them down to a fraction of their original size, massively reducing network bandwidth consumption and dramatically accelerating time-to-interactive metrics.
Maximizing Performance with Aggressive Static Asset Caching
While compression shrinks file sizes, browser caching prevents users from downloading the same files over and over again. The location ~* \.(jpg|png|css|js...) block specifically targets static media assets that rarely change. By injecting an aggressive expires 1y; header alongside the Cache-Control "immutable" flag, Nginx commands the user's browser to save these images and stylesheets locally onto their own hard drive for a full year. Crucially, the script intentionally re-declares security headers inside this nested location block, as Nginx infamously clears inherited headers within regex blocks.
Protecting Your Infrastructure by Hiding Sensitive System Files
Many modern frameworks and version control systems automatically generate hidden configuration files (such as .env environment variable files, .git directories, or .htaccess overrides). If an attacker manages to download your .env file, they immediately gain access to your database passwords and API secret keys. The tiny but immensely critical location ~ /\. { deny all; } block serves as an absolute firewall against this vector. It explicitly instructs Nginx to instantly reject any web request attempting to access any file or directory that begins with a period.
Download the Complete Hardened Nginx Configuration File
Configuring a production-grade web server requires meticulous precision; a single misplaced semicolon or missing security header can severely compromise your entire infrastructure or cause a fatal syntax error. To guarantee a flawless deployment, you can download this complete, fully audited "Hardened Nginx Server Block" file directly from this page. By deploying this heavily optimized configuration template onto your Ubuntu or Debian VPS, you ensure your backend PHP applications, WordPress sites, and Node proxies are universally protected by industry-leading security and performance standards.
Free Hardened Nginx Server Block for Security & High Performance CONF Download
# ===============================================================================
# Clayi Assets - Production Nginx Security & Performance Hardening Config
# Purpose: High-speed server block with SSL TLS 1.3, Brotli, Security Headers & Caching
# License: MIT License
# ===============================================================================
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
# Enforce HTTP to HTTPS Redirect
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
root /var/www/html;
index index.php index.html;
# SSL TLS Hardening Config
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
# Security Headers (OWASP Compliant)
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# Gzip & Performance Compression
gzip on;
gzip_comp_level 6;
gzip_min_length 256;
gzip_proxied any;
gzip_types text/plain text/css text/xml application/json application/javascript application/x-javascript text/javascript image/svg+xml;
# Static Media Asset Caching (1 Year)
location ~* \.(jpg|jpeg|png|gif|ico|webp|svg|css|js|woff|woff2)$ {
expires 1y;
add_header Cache-Control "public, no-transform, immutable";
# Explicitly re-apply security headers for static assets
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
access_log off;
}
# Hide Sensitive System Files
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# PHP FastCGI Handler
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}





There are no comments yet :(