What are some features and configurations of Nginx?
.webp&w=3840&q=75)
Here are twenty advanced features that you can configure with Nginx, along with example syntax for each feature:
- Reverse Proxy:
location / {
proxy_pass http://backend_server;
}
- Load Balancing:
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
location / {
proxy_pass http://backend;
}
}
- SSL/TLS Termination:
server {
listen 443 ssl;
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/certificate.key;
}
- API Routing:
location /api/ {
proxy_pass http://api_server;
}
- Multiple Websites:
server {
listen 80;
server_name example1.com;
root /var/www/example1;
}
server {
listen 80;
server_name example2.com;
root /var/www/example2;
}
- Access Control:
location / {
allow 192.168.1.0/24;
deny all;
}
- URL Rewriting:
rewrite ^/old-url$ /new-url permanent;
- 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;
}
}
- Compression:
gzip on;
gzip_types text/plain text/css application/json application/javascript;
- Rate Limiting:
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
server {
location / {
limit_req zone=mylimit burst=20;
}
}
- HTTP/2 Support:
server {
listen 443 ssl http2;
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/certificate.key;
}
- 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";
}
- Basic Authentication:
location / {
auth_basic "Restricted";
auth_basic_user_file /path/to/htpasswd;
}
- IP Whitelisting:
location / {
allow 192.168.1.0/24;
allow 10.0.0.0/8;
deny all;
}
- Request Limiting:
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;
server {
location / {
limit_req zone=mylimit burst=5;
}
}
- Logging:
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
- Redirect:
server {
listen 80;
server_name old-domain.com;
return 301 $scheme://new-domain.com$request_uri;
}
- Upstream Health Checks:
upstream backend {
server backend1.example.com;
server backend2.example.com;
health_check;
}
server {
location / {
proxy_pass http://backend;
}
}
- 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;
}
- 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.