mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+262
-333
@@ -1,406 +1,335 @@
|
||||
---
|
||||
description: This rule provides a comprehensive guide to nginx configuration best practices, covering code organization, common patterns, performance, security, testing, pitfalls, and tooling.
|
||||
globs: nginx.conf
|
||||
description: This guide defines the definitive NGINX configuration best practices for our team, focusing on modularity, security, performance, and maintainability.
|
||||
globs: **/*
|
||||
---
|
||||
# nginx Configuration Best Practices
|
||||
# nginx Best Practices
|
||||
|
||||
This document outlines best practices for configuring nginx as a web server, reverse proxy, and load balancer. Adhering to these guidelines promotes maintainability, security, and performance.
|
||||
NGINX is our go-to for web serving, reverse proxying, and load balancing. This guide outlines the mandatory best practices for NGINX configurations, ensuring readability, security, and optimal performance across all our deployments.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
## Critical Guidelines:
|
||||
|
||||
### 1.1 Directory Structure
|
||||
### 1. Code Organization and Structure
|
||||
|
||||
Maintain a well-organized directory structure to manage configuration files efficiently.
|
||||
Always break down your NGINX configuration into small, focused, and reusable `include` files. This significantly improves maintainability and reduces cognitive load.
|
||||
|
||||
**✅ GOOD: Modular Configuration**
|
||||
|
||||
/etc/nginx/
|
||||
├── nginx.conf # Main nginx configuration file
|
||||
├── conf.d/ # Site-specific configurations (symlinks preferred)
|
||||
│ ├── default.conf # Default configuration
|
||||
│ ├── example.com.conf# Site-specific configuration
|
||||
│ └── ...
|
||||
├── snippets/ # Reusable configuration snippets
|
||||
│ ├── ssl-params.conf # SSL parameters
|
||||
│ ├── proxy-params.conf # Reverse proxy parameters
|
||||
│ └── ...
|
||||
└── modules-enabled/ # Enabled modules (usually managed by package manager)
|
||||
```nginx
|
||||
# /etc/nginx/nginx.conf
|
||||
user nginx;
|
||||
worker_processes auto;
|
||||
|
||||
error_log /var/log/nginx/error.log warn;
|
||||
pid /var/run/nginx.pid;
|
||||
|
||||
**Explanation:**
|
||||
|
||||
* `/etc/nginx/nginx.conf`: The main configuration file that includes other configurations.
|
||||
* `/etc/nginx/conf.d/`: Contains site-specific configurations. Symlinking from `/etc/nginx/sites-available/` to `/etc/nginx/sites-enabled/` is a common, more manageable pattern.
|
||||
* `/etc/nginx/snippets/`: Stores reusable configuration snippets, promoting modularity and reducing duplication.
|
||||
|
||||
### 1.2 File Naming Conventions
|
||||
|
||||
Adopt consistent file naming conventions for clarity.
|
||||
|
||||
* Site-specific configurations: `domain.com.conf` or `application-name.conf`.
|
||||
* Snippets: `description.conf` (e.g., `ssl-params.conf`, `proxy-params.conf`).
|
||||
|
||||
### 1.3 Modular Configuration
|
||||
|
||||
Break down complex configurations into smaller, reusable modules (snippets).
|
||||
|
||||
**Example: `snippets/ssl-params.conf`**
|
||||
|
||||
nginx
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_prefer_server_ciphers on;
|
||||
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
|
||||
ssl_ecdh_curve prime256v1;
|
||||
ssl_session_timeout 10m;
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_session_tickets off;
|
||||
|
||||
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
|
||||
add_header X-Frame-Options SAMEORIGIN;
|
||||
add_header X-Content-Type-Options nosniff;
|
||||
add_header X-XSS-Protection "1; mode=block";
|
||||
|
||||
ssl_stapling on;
|
||||
ssl_stapling_verify on;
|
||||
resolver 8.8.8.8 8.8.4.4 valid=300s;
|
||||
resolver_timeout 5s;
|
||||
|
||||
|
||||
**Usage in site configuration:**
|
||||
|
||||
nginx
|
||||
server
|
||||
listen 443 ssl;
|
||||
server_name example.com;
|
||||
|
||||
include snippets/ssl-params.conf;
|
||||
|
||||
ssl_certificate /etc/nginx/ssl/example.com.crt;
|
||||
ssl_certificate_key /etc/nginx/ssl/example.com.key;
|
||||
|
||||
location / {
|
||||
# ...
|
||||
|
||||
}
|
||||
|
||||
|
||||
### 1.4 Component Architecture
|
||||
|
||||
Design your nginx configurations with a clear separation of concerns:
|
||||
|
||||
* **Global Settings**: Core directives in `nginx.conf` affecting all virtual hosts.
|
||||
* **Virtual Host Configuration**: Separate files for each site/application in `conf.d/`.
|
||||
* **Reusable Blocks**: Snippets for common settings like SSL, reverse proxy configurations, or access control.
|
||||
|
||||
### 1.5 Code Splitting
|
||||
|
||||
For large and complex configurations, split files by functionality (e.g., routing rules, security policies, caching configurations). Use `include` directives to assemble the complete configuration.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### 2.1 Design Patterns
|
||||
|
||||
* **Reverse Proxy**: Forward requests to backend servers. Useful for load balancing, caching, and security.
|
||||
* **Load Balancer**: Distribute traffic across multiple backend servers for high availability and scalability. Use `upstream` blocks.
|
||||
* **Static Content Server**: Efficiently serve static files like images, CSS, and JavaScript.
|
||||
* **Cache**: Reduce latency and server load by caching frequently accessed content.
|
||||
|
||||
### 2.2 Recommended Approaches for Common Tasks
|
||||
|
||||
* **Serving Static Content:**
|
||||
|
||||
nginx
|
||||
location /static/ {
|
||||
alias /var/www/example.com/static/;
|
||||
expires 30d;
|
||||
access_log off;
|
||||
}
|
||||
|
||||
|
||||
* **Reverse Proxy to a Backend Server:**
|
||||
|
||||
nginx
|
||||
location / {
|
||||
proxy_pass http://backend_server;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
upstream backend_server {
|
||||
server backend1.example.com;
|
||||
server backend2.example.com;
|
||||
}
|
||||
|
||||
|
||||
* **Implementing Basic Authentication:**
|
||||
|
||||
nginx
|
||||
location /protected/ {
|
||||
auth_basic "Restricted Area";
|
||||
auth_basic_user_file /etc/nginx/.htpasswd;
|
||||
}
|
||||
|
||||
|
||||
### 2.3 Anti-patterns and Code Smells
|
||||
|
||||
* **Large, Monolithic Configuration Files**: Difficult to manage and debug. Break them into smaller, modular files using `include`.
|
||||
* **Duplicated Configuration Blocks**: Create snippets to reuse common configurations.
|
||||
* **Using `if` Statements Excessively**: `if` statements can have performance implications. Prefer using `try_files` or other more efficient directives.
|
||||
* **Insecure Configuration**: Exposing sensitive information, using weak ciphers, or not implementing proper access controls.
|
||||
* **Ignoring Error Logs**: Neglecting to monitor and analyze error logs can lead to undetected issues.
|
||||
|
||||
### 2.4 State Management
|
||||
|
||||
Nginx itself is generally stateless. State management is typically handled by backend applications. However, Nginx Plus offers features like session persistence (sticky sessions) for load balancing.
|
||||
|
||||
* **Session Persistence (Nginx Plus):**
|
||||
|
||||
nginx
|
||||
upstream backend {
|
||||
server backend1.example.com;
|
||||
server backend2.example.com;
|
||||
sticky cookie srv_id expires=1h;
|
||||
}
|
||||
|
||||
|
||||
### 2.5 Error Handling
|
||||
|
||||
* **Custom Error Pages**: Configure custom error pages for a better user experience.
|
||||
|
||||
nginx
|
||||
error_page 404 /404.html;
|
||||
location = /404.html {
|
||||
root /var/www/example.com/html;
|
||||
internal;
|
||||
}
|
||||
|
||||
|
||||
* **Logging**: Use comprehensive logging to capture errors and warnings for troubleshooting.
|
||||
* **Retry Logic**: Implement retry logic in your application or use Nginx's `proxy_next_upstream` directive to retry failed requests on different backend servers.
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1 Optimization Techniques
|
||||
|
||||
* **Enable Gzip Compression**: Compress text-based responses to reduce bandwidth usage.
|
||||
|
||||
nginx
|
||||
gzip on;
|
||||
gzip_disable "msie6";
|
||||
gzip_vary on;
|
||||
gzip_proxied any;
|
||||
gzip_comp_level 6;
|
||||
gzip_buffers 16 8k;
|
||||
gzip_http_version 1.1;
|
||||
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
|
||||
|
||||
|
||||
* **Enable Caching**: Cache static content and dynamic responses to reduce server load and improve response times. Use `proxy_cache_path` and `proxy_cache` directives.
|
||||
|
||||
nginx
|
||||
proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
|
||||
|
||||
server {
|
||||
location / {
|
||||
proxy_pass http://backend_server;
|
||||
proxy_cache my_cache;
|
||||
proxy_cache_valid 200 302 1h;
|
||||
proxy_cache_valid 404 10m;
|
||||
proxy_cache_use_stale error timeout updating invalid_header http_500 http_502 http_503 http_504;
|
||||
add_header X-Cache-Status $upstream_cache_status;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
* **Tune Worker Processes and Connections**: Adjust `worker_processes` and `worker_connections` based on your server's resources.
|
||||
|
||||
nginx
|
||||
worker_processes auto;
|
||||
events {
|
||||
worker_connections 1024;
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
|
||||
* **HTTP/2 or HTTP/3**: Enable HTTP/2 or HTTP/3 for improved performance.
|
||||
* **Keep-Alive Connections**: Enable keep-alive connections to reduce latency by reusing existing connections.
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
nginx
|
||||
keepalive_timeout 65;
|
||||
|
||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent "$http_referer" '
|
||||
'"$http_user_agent" "$http_x_forwarded_for"';
|
||||
|
||||
### 3.2 Memory Management
|
||||
access_log /var/log/nginx/access.log main;
|
||||
|
||||
* **Optimize Buffer Sizes**: Tune buffer sizes (e.g., `proxy_buffer_size`, `proxy_buffers`) to match your application's needs and avoid memory fragmentation.
|
||||
* **Monitor Memory Usage**: Use tools like `top` or `htop` to monitor nginx's memory usage and identify potential memory leaks.
|
||||
sendfile on;
|
||||
tcp_nopush on;
|
||||
tcp_nodelay on;
|
||||
keepalive_timeout 65;
|
||||
|
||||
### 3.3 Rendering Optimization (If applicable)
|
||||
# Global HTTP settings
|
||||
include /etc/nginx/conf.d/http_common.conf;
|
||||
# Upstream server definitions
|
||||
include /etc/nginx/conf.d/upstreams/*.conf;
|
||||
# SSL/TLS profiles
|
||||
include /etc/nginx/conf.d/ssl_profiles/*.conf;
|
||||
# Virtual hosts
|
||||
include /etc/nginx/sites-enabled/*.conf;
|
||||
}
|
||||
```
|
||||
|
||||
Nginx primarily serves content; rendering optimization is typically handled by the backend application.
|
||||
**❌ BAD: Monolithic Configuration**
|
||||
|
||||
* **Content Delivery Network (CDN)**: Offload static content delivery to a CDN.
|
||||
```nginx
|
||||
# /etc/nginx/nginx.conf (too long, hard to navigate)
|
||||
# ... all server blocks, upstream definitions, SSL settings in one file ...
|
||||
server {
|
||||
listen 80;
|
||||
server_name example.com;
|
||||
# ... hundreds of lines ...
|
||||
}
|
||||
upstream backend_app {
|
||||
# ... more lines ...
|
||||
}
|
||||
# ... SSL config ...
|
||||
```
|
||||
|
||||
### 3.4 Bundle Size Optimization (If applicable)
|
||||
### 2. Consistent Formatting and Comments
|
||||
|
||||
This primarily applies to serving static assets.
|
||||
Use two-space indentation. Add descriptive comments for every directive or block that isn't immediately obvious.
|
||||
|
||||
* **Minify and Compress**: Minify and compress CSS and JavaScript files before serving them.
|
||||
* **Use Brotli**: Consider using Brotli compression, which offers better compression ratios than Gzip.
|
||||
**✅ GOOD**
|
||||
|
||||
### 3.5 Lazy Loading (If applicable)
|
||||
```nginx
|
||||
# /etc/nginx/sites-enabled/api.conf
|
||||
server {
|
||||
listen 80;
|
||||
listen 443 ssl http2; # Enforce HTTPS and HTTP/2
|
||||
server_name api.example.com;
|
||||
|
||||
This is typically implemented at the application level.
|
||||
# Include standard SSL settings
|
||||
include /etc/nginx/conf.d/ssl_profiles/default_ssl.conf;
|
||||
|
||||
## 4. Security Best Practices
|
||||
location / {
|
||||
proxy_pass http://api_backend; # Proxy to the API upstream
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_redirect off;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 4.1 Common Vulnerabilities and Prevention
|
||||
|
||||
* **SQL Injection**: Prevent SQL injection by validating and sanitizing user inputs at the application level.
|
||||
* **Cross-Site Scripting (XSS)**: Implement proper output encoding and use HTTP headers like `X-XSS-Protection` and `Content-Security-Policy`.
|
||||
|
||||
nginx
|
||||
add_header X-XSS-Protection "1; mode=block";
|
||||
add_header Content-Security-Policy "default-src 'self'";
|
||||
|
||||
|
||||
* **Clickjacking**: Prevent clickjacking by setting the `X-Frame-Options` header.
|
||||
|
||||
nginx
|
||||
add_header X-Frame-Options "SAMEORIGIN";
|
||||
|
||||
|
||||
* **Buffer Overflow**: Keep Nginx up to date to patch vulnerabilities related to buffer overflows.
|
||||
* **Denial of Service (DoS)**: Implement rate limiting to prevent DoS attacks.
|
||||
|
||||
nginx
|
||||
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;
|
||||
**❌ BAD**
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
listen 443 ssl http2;
|
||||
server_name api.example.com;
|
||||
include /etc/nginx/conf.d/ssl_profiles/default_ssl.conf;
|
||||
location / {
|
||||
limit_req zone=mylimit burst=5 nodelay;
|
||||
# ...
|
||||
proxy_pass http://api_backend;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_redirect off;
|
||||
}
|
||||
|
||||
} # No comments, inconsistent indentation
|
||||
```
|
||||
|
||||
### 4.2 Input Validation
|
||||
### 3. Security Hardening
|
||||
|
||||
* **Validate User Inputs**: Validate all user inputs at the application level to prevent malicious data from reaching backend servers.
|
||||
* **Limit Request Size**: Limit the size of incoming requests to prevent buffer overflows and DoS attacks.
|
||||
Always enable modern TLS, HTTP/2, strong cipher suites, and essential security headers.
|
||||
|
||||
nginx
|
||||
client_max_body_size 2m;
|
||||
|
||||
**✅ GOOD: Secure SSL Profile (`/etc/nginx/conf.d/ssl_profiles/default_ssl.conf`)**
|
||||
|
||||
### 4.3 Authentication and Authorization
|
||||
```nginx
|
||||
ssl_protocols TLSv1.2 TLSv1.3; # Only modern, secure protocols
|
||||
ssl_prefer_server_ciphers on;
|
||||
ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_session_timeout 1h;
|
||||
ssl_session_tickets off;
|
||||
ssl_dhparam /etc/nginx/ssl/dhparam.pem; # Generate with `openssl dhparam -out dhparam.pem 2048`
|
||||
|
||||
* **Basic Authentication**: Use basic authentication for simple access control. However, it's not secure over HTTP; always use HTTPS.
|
||||
* **JWT (JSON Web Tokens)**: Implement JWT-based authentication for APIs.
|
||||
* **OAuth 2.0**: Use OAuth 2.0 for delegated authorization.
|
||||
* **Access Control Lists (ACLs)**: Use ACLs to control access to specific resources based on IP addresses or other criteria.
|
||||
# Essential security headers
|
||||
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-Frame-Options "DENY" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header Referrer-Policy "no-referrer-when-downgrade" always;
|
||||
```
|
||||
|
||||
nginx
|
||||
allow 192.168.1.0/24;
|
||||
deny all;
|
||||
|
||||
**❌ BAD: Weak SSL/Missing Headers**
|
||||
|
||||
### 4.4 Data Protection
|
||||
```nginx
|
||||
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Outdated protocols
|
||||
ssl_ciphers HIGH:!aNULL:!MD5; # Too broad, potentially weak
|
||||
# No security headers
|
||||
```
|
||||
|
||||
* **HTTPS**: Use HTTPS to encrypt all traffic between clients and the server. Obtain and configure SSL/TLS certificates.
|
||||
* **Secure Storage**: Store sensitive data (e.g., passwords, API keys) securely using encryption and access controls.
|
||||
### 4. Load Balancing and Upstream Configuration
|
||||
|
||||
### 4.5 Secure API Communication
|
||||
Define upstream groups with explicit `weight`, `max_fails`, and `fail_timeout`. Use `proxy_protocol` and `RealIP` when behind another proxy/load balancer.
|
||||
|
||||
* **API Keys**: Require API keys for all API requests.
|
||||
* **Rate Limiting**: Implement rate limiting to prevent abuse and DoS attacks.
|
||||
* **Input Validation**: Validate all API request parameters.
|
||||
* **Output Encoding**: Encode API responses to prevent XSS attacks.
|
||||
**✅ GOOD: Robust Upstream (`/etc/nginx/conf.d/upstreams/api_backend.conf`)**
|
||||
|
||||
## 5. Testing Approaches
|
||||
```nginx
|
||||
upstream api_backend {
|
||||
# Round-robin load balancing by default
|
||||
server 10.0.0.1:8080 weight=5 max_fails=3 fail_timeout=10s;
|
||||
server 10.0.0.2:8080 weight=5 max_fails=3 fail_timeout=10s;
|
||||
# NGINX Plus: Enable health checks
|
||||
# health_check interval=5s passes=1 fails=3;
|
||||
}
|
||||
|
||||
### 5.1 Unit Testing
|
||||
# In http block or server block that accepts PROXY protocol
|
||||
http {
|
||||
# ...
|
||||
server {
|
||||
listen 80 proxy_protocol;
|
||||
listen 443 ssl proxy_protocol;
|
||||
# ...
|
||||
# Set the real client IP from the PROXY protocol header
|
||||
set_real_ip_from 192.168.1.0/24; # IP range of your upstream load balancer
|
||||
real_ip_header proxy_protocol;
|
||||
# ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Nginx configuration files are difficult to unit test directly. Unit testing is more relevant for custom Nginx modules written in C.
|
||||
**❌ BAD: Basic Upstream / Missing RealIP**
|
||||
|
||||
### 5.2 Integration Testing
|
||||
```nginx
|
||||
upstream api_backend {
|
||||
server 10.0.0.1:8080; # No weight, fail_timeout, max_fails
|
||||
server 10.0.0.2:8080;
|
||||
}
|
||||
# No proxy_protocol or RealIP configuration, logs show proxy IP
|
||||
```
|
||||
|
||||
* **Test Configuration Syntax**: Use `nginx -t` to test the syntax of your configuration files.
|
||||
* **Test with Docker**: Use Docker containers to create isolated test environments.
|
||||
* **Test with Real Traffic**: Use tools like `ab` (Apache Bench) or `wrk` to simulate real traffic and measure performance.
|
||||
### 5. Performance Optimization
|
||||
|
||||
### 5.3 End-to-End Testing
|
||||
Leverage caching, compression, and resource limits to ensure high-throughput and low-latency.
|
||||
|
||||
Use tools like Selenium or Cypress to automate end-to-end tests of your application.
|
||||
**✅ GOOD: Caching, Compression, and Limits**
|
||||
|
||||
### 5.4 Test Organization
|
||||
```nginx
|
||||
http {
|
||||
# ...
|
||||
# Caching for static assets or API responses
|
||||
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m max_size=1g;
|
||||
|
||||
* **Separate Test Environment**: Create a separate test environment that mirrors your production environment.
|
||||
* **Automated Testing**: Automate your testing process using CI/CD pipelines.
|
||||
server {
|
||||
# ...
|
||||
location /static/ {
|
||||
root /var/www/html;
|
||||
expires 30d;
|
||||
add_header Cache-Control "public, immutable";
|
||||
}
|
||||
|
||||
### 5.5 Mocking and Stubbing
|
||||
location /api/ {
|
||||
proxy_cache my_cache;
|
||||
proxy_cache_valid 200 302 10m;
|
||||
proxy_cache_valid 404 1m;
|
||||
proxy_cache_revalidate on;
|
||||
proxy_cache_min_uses 1;
|
||||
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
|
||||
proxy_cache_background_update on;
|
||||
proxy_cache_lock on;
|
||||
proxy_cache_lock_timeout 5s;
|
||||
proxy_cache_lock_age 5s;
|
||||
proxy_cache_bypass $http_pragma; # Bypass cache for specific requests
|
||||
add_header X-Cache-Status $upstream_cache_status;
|
||||
proxy_pass http://api_backend;
|
||||
}
|
||||
|
||||
* **Mock Backend Services**: Mock backend services to isolate your Nginx configuration and test different scenarios.
|
||||
# Enable Gzip compression for text-based content
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_proxied any;
|
||||
gzip_comp_level 6;
|
||||
gzip_buffers 16 8k;
|
||||
gzip_http_version 1.1;
|
||||
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
# Rate limiting (per IP, 10 requests per second burstable by 20)
|
||||
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
|
||||
limit_req zone=mylimit burst=20 nodelay;
|
||||
|
||||
### 6.1 Frequent Mistakes
|
||||
# Connection limits (10 connections per IP)
|
||||
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
|
||||
limit_conn conn_limit 10;
|
||||
|
||||
* **Incorrect Syntax**: Nginx configuration syntax can be tricky. Always test your configuration files using `nginx -t`.
|
||||
* **Missing Semicolons**: Forgetting semicolons at the end of directives.
|
||||
* **Incorrect File Permissions**: Ensure that Nginx has the necessary permissions to read configuration files and access static content.
|
||||
* **Not Reloading Configuration**: Forgetting to reload Nginx after making changes to the configuration files (e.g., `nginx -s reload`).
|
||||
* **Using `if` Statements Inappropriately**: Overusing `if` can lead to unexpected behavior and performance issues. Prefer `try_files` or other alternatives.
|
||||
# Max request body size (10MB)
|
||||
client_max_body_size 10M;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 6.2 Edge Cases
|
||||
**❌ BAD: No Caching, No Compression, No Limits**
|
||||
|
||||
* **Handling Large Files**: Tune buffer sizes and timeouts when handling large file uploads or downloads.
|
||||
* **WebSockets**: Configure Nginx to properly proxy WebSocket connections.
|
||||
* **Long URIs**: Configure `client_header_buffer_size` and `large_client_header_buffers` to handle long URIs.
|
||||
```nginx
|
||||
# No proxy_cache_path defined
|
||||
server {
|
||||
# ...
|
||||
location /static/ {
|
||||
root /var/www/html;
|
||||
# No expires, no Cache-Control
|
||||
}
|
||||
# No gzip directives
|
||||
# No rate limiting or connection limits
|
||||
# No client_max_body_size, vulnerable to large body attacks
|
||||
}
|
||||
```
|
||||
|
||||
### 6.3 Version-Specific Issues
|
||||
### 6. Common Pitfalls and Gotchas
|
||||
|
||||
* **Check Release Notes**: Review the release notes for each Nginx version to identify any breaking changes or known issues.
|
||||
* **Test Upgrades**: Test Nginx upgrades in a staging environment before deploying to production.
|
||||
**a. `location` Block Order:** Specific (exact, regex) locations must come before general (prefix) ones.
|
||||
|
||||
### 6.4 Compatibility Concerns
|
||||
**❌ BAD**
|
||||
|
||||
* **Backend Application Compatibility**: Ensure that your Nginx configuration is compatible with the backend applications you are using.
|
||||
* **TLS Versions and Ciphers**: Choose TLS versions and ciphers that are compatible with your clients and backend servers.
|
||||
```nginx
|
||||
location / { # This catches everything first
|
||||
proxy_pass http://default_backend;
|
||||
}
|
||||
location = /specific { # This will never be hit
|
||||
return 200 "Specific page";
|
||||
}
|
||||
```
|
||||
|
||||
### 6.5 Debugging Strategies
|
||||
**✅ GOOD**
|
||||
|
||||
* **Error Logs**: Check Nginx error logs (`/var/log/nginx/error.log`) for errors and warnings.
|
||||
* **Access Logs**: Analyze Nginx access logs (`/var/log/nginx/access.log`) to understand traffic patterns and identify potential issues.
|
||||
* **`nginx -t`**: Use `nginx -t` to test the syntax of your configuration files.
|
||||
* **Verbose Logging**: Enable verbose logging to capture more detailed information about requests and responses.
|
||||
* **tcpdump/Wireshark**: Use `tcpdump` or Wireshark to capture and analyze network traffic.
|
||||
```nginx
|
||||
location = /specific { # Exact match first
|
||||
return 200 "Specific page";
|
||||
}
|
||||
location ~ \.php$ { # Regex matches next
|
||||
fastcgi_pass unix:/var/run/php/php-fpm.sock;
|
||||
# ...
|
||||
}
|
||||
location / { # General prefix match last
|
||||
proxy_pass http://default_backend;
|
||||
}
|
||||
```
|
||||
|
||||
## 7. Tooling and Environment
|
||||
**b. Missing `resolver` for Upstream Hostnames:** If your upstream servers are defined by hostnames (not IPs), NGINX needs a DNS resolver.
|
||||
|
||||
### 7.1 Recommended Development Tools
|
||||
**❌ BAD**
|
||||
|
||||
* **Text Editor**: Use a text editor with syntax highlighting and autocompletion for Nginx configuration files (e.g., VS Code with Nginx configuration extension).
|
||||
* **Command-Line Tools**: Use command-line tools like `nginx`, `curl`, `ab`, and `tcpdump` for testing and debugging.
|
||||
* **Docker**: Use Docker for creating isolated test environments.
|
||||
```nginx
|
||||
upstream dynamic_backend {
|
||||
server app.internal.svc.cluster.local:8080; # Will fail to resolve without resolver
|
||||
}
|
||||
```
|
||||
|
||||
### 7.2 Build Configuration
|
||||
**✅ GOOD**
|
||||
|
||||
* **Version Control**: Store your Nginx configuration files in a version control system (e.g., Git).
|
||||
* **Configuration Management**: Use configuration management tools like Ansible or Chef to automate the deployment and management of your Nginx configuration.
|
||||
```nginx
|
||||
http {
|
||||
resolver 10.0.0.10 valid=30s; # Use your internal DNS server, cache for 30s
|
||||
resolver_timeout 5s;
|
||||
|
||||
### 7.3 Linting and Formatting
|
||||
upstream dynamic_backend {
|
||||
server app.internal.svc.cluster.local:8080;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
* **Nginx Linter**: Use an Nginx linter to check your configuration files for syntax errors and best practices violations (e.g., `nginx -t`).
|
||||
* **Code Formatting**: Use a code formatter to ensure consistent formatting of your configuration files.
|
||||
### 7. Testing Approaches
|
||||
|
||||
### 7.4 Deployment Best Practices
|
||||
Always validate your NGINX configuration before reloading or restarting the service.
|
||||
|
||||
* **Blue-Green Deployment**: Use blue-green deployment to minimize downtime during deployments.
|
||||
* **Canary Deployment**: Use canary deployment to test new Nginx configurations with a small subset of users before rolling them out to the entire user base.
|
||||
* **Rollback Strategy**: Have a rollback strategy in place in case of deployment failures.
|
||||
**✅ GOOD: Configuration Syntax Check**
|
||||
|
||||
### 7.5 CI/CD Integration
|
||||
```bash
|
||||
sudo nginx -t
|
||||
```
|
||||
|
||||
* **Automated Testing**: Integrate automated testing into your CI/CD pipeline.
|
||||
* **Configuration Validation**: Validate Nginx configuration files as part of your CI/CD pipeline.
|
||||
* **Automated Deployment**: Automate the deployment of Nginx configuration files using CI/CD tools like Jenkins, GitLab CI, or CircleCI.
|
||||
This command checks the syntax of your configuration files and attempts to open files referenced by `include` directives. If successful, it will output:
|
||||
|
||||
By following these best practices, you can create a robust, secure, and performant Nginx configuration that meets the needs of your web applications.
|
||||
```
|
||||
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
|
||||
nginx: configuration file /etc/nginx/nginx.conf test is successful
|
||||
```
|
||||
|
||||
**❌ BAD: Deploying without `nginx -t`**
|
||||
|
||||
Never deploy or reload NGINX without first running `nginx -t`. A single syntax error can bring down your entire web service.
|
||||
|
||||
For more advanced testing, implement integration tests that hit your NGINX endpoints and verify expected behavior (e.g., correct routing, headers, content). Consider using NGINX Amplify for continuous configuration analysis and performance monitoring.
|
||||
Reference in New Issue
Block a user