What are some features and configurations of Nginx?

What are some features and configurations of Nginx?

Here are twenty advanced features that you can configure with Nginx, along with example syntax for each feature:

  1. Reverse Proxy:
location / {
    proxy_pass http://backend_server;
}
  1. Load Balancing:
upstream backend {
    server backend1.example.com;
    server backend2.example.com;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
  1. SSL/TLS Termination:
server {
    listen 443 ssl;
    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/certificate.key;
}
  1. API Routing:
location /api/ {
    proxy_pass http://api_server;
}
  1. Multiple Websites:
server {
    listen 80;
    server_name example1.com;
    root /var/www/example1;
}

server {
    listen 80;
    server_name example2.com;
    root /var/www/example2;
}
  1. Access Control:
location / {
    allow 192.168.1.0/24;
    deny all;
}
  1. URL Rewriting:
rewrite ^/old-url$ /new-url permanent;
  1. Caching:
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m;

server {
    location / {
        proxy_cache my_cache;
        proxy_cache_valid 200 60m;
        proxy_pass http://backend;
    }
}
  1. Compression:
gzip on;
gzip_types text/plain text/css application/json application/javascript;
  1. Rate Limiting:
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;

server {
    location / {
        limit_req zone=mylimit burst=20;
    }
}
  1. HTTP/2 Support:
server {
    listen 443 ssl http2;
    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/certificate.key;
}
  1. WebSocket Proxying:
location /websocket/ {
    proxy_pass http://websocket_server;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
  1. Basic Authentication:
location / {
    auth_basic "Restricted";
    auth_basic_user_file /path/to/htpasswd;
}
  1. IP Whitelisting:
location / {
    allow 192.168.1.0/24;
    allow 10.0.0.0/8;
    deny all;
}
  1. Request Limiting:
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;

server {
    location / {
        limit_req zone=mylimit burst=5;
    }
}
  1. Logging:
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
  1. Redirect:
server {
    listen 80;
    server_name old-domain.com;
    return 301 $scheme://new-domain.com$request_uri;
}
  1. Upstream Health Checks:
upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    health_check;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
  1. SSL/TLS Stapling:
server {
    listen 443 ssl;
    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/certificate.key;
    ssl_stapling on;
    ssl_stapling_verify on;
}
  1. Proxy Buffering:
server {
    location / {
        proxy_buffering on;
        proxy_buffer_size 16k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_pass http://backend;
    }
}

These are just a few examples of the advanced features you can configure with Nginx. Nginx provides a powerful and flexible configuration language that allows you to customize and optimize your web server setup according to your specific requirements.