Nginx Rewrite Rules, Anti-Hotlinking, Static/Dynamic Separation, and Keepalived High‑Availability Configuration Guide
This article provides a comprehensive step‑by‑step guide on configuring Nginx rewrite rules, implementing anti‑hotlink protection, separating static and dynamic resources, and setting up Keepalived for high‑availability load balancing, complete with example configurations and shell scripts.
1. Nginx Rewrite Rules – The rewrite directive rewrites a URL to another URL using the syntax rewrite <regex> <replacement> [flag]; . Common flags include last , break , redirect (302), and permanent (301). Example configuration to redirect www.dbspread.com to www.dbspread.com/new.index.html is shown.
2. Nginx Anti‑Hotlinking – By defining a location ~* \.(rmvb|jpg|png|swf|flv)$ block with valid_referers and returning 403 for invalid referers, hotlinking of media files can be prevented.
3. Nginx Static/Dynamic Separation – Static resources (e.g., images, CSS, JS) are served from a dedicated directory with long expires headers, while dynamic requests are proxied to Tomcat back‑ends. The configuration includes proxy_set_header directives, upstream definitions, and caching settings.
4. Keepalived High‑Availability – Keepalived provides VRRP‑based failover for Nginx. The guide details installing Keepalived (download, extract, compile), creating /etc/keepalived/keepalived.conf , defining global_defs , vrrp_script for health checks, and vrrp_instance with virtual IP 192.168.152.200 . Master and backup node configurations are provided.
5. Supporting Scripts – A shell script /usr/local/src/check_nginx_pid.sh checks if Nginx is running and restarts it; if restart fails, it kills Keepalived to trigger VIP takeover.
6. Deployment Steps – After installing Keepalived and configuring Nginx, copy the Keepalived config to /etc/keepalived , start both services, verify VIP failover by stopping the master node, and confirm that the backup assumes the master role. Finally, map the virtual IP to the domain in /etc/hosts for testing.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.