$npx -y skills add ancoleman/ai-design-components --skill configuring-nginxConfigure nginx for static sites, reverse proxying, load balancing, SSL/TLS termination, caching, and performance tuning. When setting up web servers, application proxies, or load balancers, this skill provides production-ready patterns with modern security best practices for TLS
| 1 | # Configuring nginx |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide engineers through configuring nginx for common web infrastructure needs: static file serving, reverse proxying backend applications, load balancing across multiple servers, SSL/TLS termination, caching, and performance optimization. Provides production-ready configurations with security best practices. |
| 6 | |
| 7 | ## When to Use This Skill |
| 8 | |
| 9 | Use when working with: |
| 10 | - Setting up web server for static sites or single-page applications |
| 11 | - Configuring reverse proxy for Node.js, Python, Ruby, or Go applications |
| 12 | - Implementing load balancing across multiple backend servers |
| 13 | - Terminating SSL/TLS for HTTPS traffic |
| 14 | - Adding caching layer for performance improvement |
| 15 | - Building API gateway functionality |
| 16 | - Protecting against DDoS with rate limiting |
| 17 | - Proxying WebSocket connections |
| 18 | |
| 19 | Trigger phrases: "configure nginx", "nginx reverse proxy", "nginx load balancer", "enable SSL in nginx", "nginx performance tuning", "nginx caching", "nginx rate limiting" |
| 20 | |
| 21 | ## Installation |
| 22 | |
| 23 | **Ubuntu/Debian:** |
| 24 | ```bash |
| 25 | sudo apt update && sudo apt install nginx -y |
| 26 | sudo systemctl enable nginx |
| 27 | sudo systemctl start nginx |
| 28 | ``` |
| 29 | |
| 30 | **RHEL/CentOS/Rocky:** |
| 31 | ```bash |
| 32 | sudo dnf install nginx -y |
| 33 | sudo systemctl enable nginx |
| 34 | sudo systemctl start nginx |
| 35 | ``` |
| 36 | |
| 37 | **Docker:** |
| 38 | ```bash |
| 39 | docker run -d -p 80:80 -v /path/to/config:/etc/nginx/conf.d nginx:alpine |
| 40 | ``` |
| 41 | |
| 42 | ## Quick Start Examples |
| 43 | |
| 44 | ### Static Website |
| 45 | |
| 46 | Serve HTML/CSS/JS files from a directory: |
| 47 | |
| 48 | ```nginx |
| 49 | server { |
| 50 | listen 80; |
| 51 | server_name example.com www.example.com; |
| 52 | root /var/www/example.com/html; |
| 53 | index index.html; |
| 54 | |
| 55 | location / { |
| 56 | try_files $uri $uri/ =404; |
| 57 | } |
| 58 | |
| 59 | location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ { |
| 60 | expires 1y; |
| 61 | add_header Cache-Control "public, immutable"; |
| 62 | } |
| 63 | } |
| 64 | ``` |
| 65 | |
| 66 | Enable site: |
| 67 | ```bash |
| 68 | sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ |
| 69 | sudo nginx -t && sudo systemctl reload nginx |
| 70 | ``` |
| 71 | |
| 72 | See `references/static-sites.md` for SPA configurations and advanced patterns. |
| 73 | |
| 74 | ### Reverse Proxy |
| 75 | |
| 76 | Proxy requests to a backend application server: |
| 77 | |
| 78 | ```nginx |
| 79 | upstream app_backend { |
| 80 | server 127.0.0.1:3000; |
| 81 | keepalive 32; |
| 82 | } |
| 83 | |
| 84 | server { |
| 85 | listen 80; |
| 86 | server_name app.example.com; |
| 87 | |
| 88 | location / { |
| 89 | proxy_pass http://app_backend; |
| 90 | proxy_set_header Host $host; |
| 91 | proxy_set_header X-Real-IP $remote_addr; |
| 92 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 93 | proxy_set_header X-Forwarded-Proto $scheme; |
| 94 | proxy_http_version 1.1; |
| 95 | proxy_set_header Connection ""; |
| 96 | } |
| 97 | } |
| 98 | ``` |
| 99 | |
| 100 | See `references/reverse-proxy.md` for WebSocket proxying and API gateway patterns. |
| 101 | |
| 102 | ### SSL/TLS Configuration |
| 103 | |
| 104 | Enable HTTPS with modern TLS configuration: |
| 105 | |
| 106 | ```nginx |
| 107 | server { |
| 108 | listen 443 ssl http2; |
| 109 | server_name example.com; |
| 110 | |
| 111 | ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; |
| 112 | ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; |
| 113 | |
| 114 | ssl_protocols TLSv1.3 TLSv1.2; |
| 115 | ssl_prefer_server_ciphers off; |
| 116 | ssl_session_cache shared:SSL:50m; |
| 117 | ssl_session_timeout 1d; |
| 118 | |
| 119 | add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always; |
| 120 | |
| 121 | location / { |
| 122 | try_files $uri $uri/ =404; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | server { |
| 127 | listen 80; |
| 128 | server_name example.com; |
| 129 | return 301 https://$server_name$request_uri; |
| 130 | } |
| 131 | ``` |
| 132 | |
| 133 | See `references/ssl-tls-config.md` for complete TLS configuration and certificate setup. |
| 134 | |
| 135 | ## Core Concepts |
| 136 | |
| 137 | ### Configuration Structure |
| 138 | |
| 139 | nginx uses hierarchical configuration contexts: |
| 140 | |
| 141 | ``` |
| 142 | nginx.conf (global settings) |
| 143 | ├── events { } (connection processing) |
| 144 | └── http { } (HTTP-level settings) |
| 145 | └── server { } (virtual host) |
| 146 | └── location { } (URL routing) |
| 147 | ``` |
| 148 | |
| 149 | **File locations:** |
| 150 | - `/etc/nginx/nginx.conf` - Main configuration |
| 151 | - `/etc/nginx/sites-available/` - Available site configs |
| 152 | - `/etc/nginx/sites-enabled/` - Enabled sites (symlinks) |
| 153 | - `/etc/nginx/conf.d/*.conf` - Additional configs |
| 154 | - `/etc/nginx/snippets/` - Reusable config snippets |
| 155 | |
| 156 | See `references/configuration-structure.md` for detailed anatomy. |
| 157 | |
| 158 | ### Location Matching Priority |
| 159 | |
| 160 | nginx evaluates location blocks in this order: |
| 161 | |
| 162 | 1. `location = /exact` - Exact match (highest priority) |
| 163 | 2. `location ^~ /prefix` - Prefix match, stop searching |
| 164 | 3. `location ~ \.php$` - Regex, case-sensitive |
| 165 | 4. `location ~* \.(jpg|png)$` - Regex, case-insensitive |
| 166 | 5. `location /` - Prefix match (lowest priority) |
| 167 | |
| 168 | Example: |
| 169 | ```nginx |
| 170 | location = /api/status { |
| 171 | return 200 "OK\n"; |
| 172 | } |
| 173 | |
| 174 | location ^~ /static/ { |
| 175 | root /var/www; |
| 176 | } |
| 177 | |
| 178 | location ~ \.php$ { |
| 179 | fastcgi_pass unix:/var/run/php/php-fpm.sock; |
| 180 | } |
| 181 | |
| 182 | location / { |
| 183 | proxy_pass http://backend; |
| 184 | } |
| 185 | ``` |