Skip to main content

🚀 Advanced Nginx Configuration

This document contains configuration files for setting up security headers in Nginx. These settings enhance security by mitigating common web vulnerabilities.


🌍 Server Configuration

📜 /etc/nginx/sites-available/asifahmadkhan.com.conf

server {
listen 80;
listen [::]:80;
server_name www.asifahmadkhan.com; ## DOMAIN NAME
root /var/www/html/index.html; ## PROJECT PATH

# 🔐 Security settings
include nginxconfig.io/security.conf;

# 📜 Logging
access_log /var/log/nginx/access.log combined buffer=512k flush=1m;
error_log /var/log/nginx/error.log warn;

# 🌎 Serve static files with fallback to index.html
location / {
try_files $uri $uri/ /index.html;
}

# ⚡ API requests handling
location ~ ^/api/ {
try_files $uri $uri/ /index.php?$query_string;
}

# 🔧 Additional config
include nginxconfig.io/general.conf;
}

⚙️ Main Nginx Configuration

📜 /etc/nginx/nginx.conf

user                 www-data;
pid /run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;

# 📦 Load modules
include /etc/nginx/modules-enabled/*.conf;

events {
multi_accept on;
worker_connections 65535;
}

http {
charset utf-8;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
log_not_found off;
types_hash_max_size 2048;
types_hash_bucket_size 64;
client_max_body_size 16M;

# 📄 MIME types
include mime.types;
default_type application/octet-stream;

# 📜 Logging settings
access_log off;
error_log /dev/null;

# 🔧 Load additional configs
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

🛡️ Security Headers

📜 /etc/nginx/nginxconfig.io/security.conf

# 🛑 Security headers
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' http: https: ws: wss: data: blob: 'unsafe-inline'; frame-ancestors 'self';" always;
add_header Permissions-Policy "interest-cohort=()" always;

# 🚫 Deny access to dotfiles (except .well-known)
location ~ /\.(?!well-known) {
deny all;
}

🛠️ General Configurations

📜 /etc/nginx/nginxconfig.io/general.conf

# 🌟 Favicon
location = /favicon.ico {
log_not_found off;
}

# 🤖 Robots.txt
location = /robots.txt {
log_not_found off;
}

# 🎨 Static assets caching
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
expires 7d;
}

# 🖋️ Fonts and SVG caching
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
add_header Access-Control-Allow-Origin "*";
expires 7d;
}

# ⚡ Gzip compression
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

Now your Nginx server is configured with security headers and optimized settings! 🎉