Mastering NGINX Rewrites

Unlock the full potential of your web server with powerful URL manipulation.

The NGINX Philosophy: No .htaccess, Pure Power

Unlike Apache, NGINX does not support .htaccess files or the mod_rewrite module. This isn't a limitation; it's a design choice that contributes to NGINX's renowned performance and efficiency. All NGINX configurations, including URL rewrites, are centralized in its main configuration files (e.g., nginx.conf or included files). This eliminates per-directory lookups, resulting in faster request processing.

While the syntax differs, NGINX offers robust and flexible tools to achieve virtually any URL rewriting or redirection scenario you can imagine. Let's dive in!

Core NGINX Rewrite Directives

The return Directive: Simple & Efficient Redirects

For straightforward redirects or returning specific HTTP status codes, return is your go-to directive. It's the most efficient way to handle these cases as it immediately stops processing the request and sends the response to the client.


server {
    listen 80;
    server_name old-domain.com;
    # Permanent redirect for an entire domain
    return 301 $scheme://new-domain.com$request_uri;
}

location /old-page.html {
    # Permanent redirect for a specific page
    return 301 /new-page.html;
}

location /maintenance {
    # Return a 503 Service Unavailable status
    return 503 "Site is currently under maintenance.";
}
            

Variables to know:

Tip: Always prefer return over rewrite when you only need to send a redirect or a fixed status code. It's faster and less resource-intensive.

The rewrite Directive: Powerful Regex-Based URL Manipulation

When you need more complex URL manipulations based on regular expressions, the rewrite directive is your workhorse. It modifies the URI based on a regex pattern and a replacement string, optionally followed by a flag.


location /products/ {
    # Rewrites /products/category/item to /items?category=category&name=item
    rewrite ^/products/([^/]+)/([^/]+)$ /items?category=$1&name=$2 last;
}

location /blog/ {
    # Rewrites /blog/post-title to /index.php?page=post-title
    # This is an internal rewrite, NGINX continues processing
    rewrite ^/blog/(.*)$ /index.php?page=$1 break;
}
            

Common Flags:

The try_files Directive: The Front-Controller's Best Friend

try_files is incredibly powerful for serving static files efficiently and implementing front-controller patterns (like those used by WordPress, Laravel, Symfony, etc.). It tells NGINX to check for the existence of files or directories in a specified order, and if none are found, to internally redirect the request to a fallback URI.


location / {
    # For WordPress/Laravel type applications:
    # 1. Try to serve the exact URI as a file ($uri)
    # 2. If not a file, try to serve it as a directory ($uri/)
    # 3. If neither, internally redirect to index.php, preserving query arguments
    try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
    # Pass PHP requests to PHP-FPM
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php/php-fpm.sock; # Or fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}
            

Tip: try_files is often the most elegant and performant solution for handling "pretty URLs" that typically rely on .htaccess rules in Apache.

Powerful NGINX Rewrite Tips

You've Got This, NGINX User!

Transitioning from Apache's .htaccess to NGINX's configuration can feel like a steep learning curve initially, but rest assured, you're on the path to a more performant and scalable web serving environment.

NGINX's explicit and centralized configuration, while requiring a different mindset, ultimately leads to clearer, more efficient, and easier-to-debug setups. Embrace the power of its directives, practice with examples, and don't be afraid to experiment in a development environment.

The NGINX community is vast and supportive, and the official documentation is an excellent resource. Keep learning, keep building, and enjoy the speed and stability that NGINX brings to your projects!