Book Image

Nginx Essentials

By : Valery Kholodkov, Valery I Kholodkov
Book Image

Nginx Essentials

By: Valery Kholodkov, Valery I Kholodkov

Overview of this book

Table of Contents (13 chapters)

The basics of the rewrite engine


The rewrite engine allows you to manipulate the request URI of inbound requests.

The rewrite engine is configured using rewrite rules. Rewrite rules are used when the request URI needs to undergo transformation before further processing. Rewrite rules instruct Nginx to match the request URI with a regular expression and substitute the request URI with a specified pattern whenever a match has been scored.

Rewrite rules can be specified inside server, location, and if sections of the configuration.

Let's study some examples of rewrite rules in action. Consider a simple case when one resource needs to be substituted by another:

location / {
    rewrite ^/css/default\.css$ /css/styles.css break;
    root /var/www/example.com;
}

With the preceding configuration, every request to /css/default.css will have its URI rewritten to /css/styles.css and will fetch this resource instead. The rewrite directive specifies a pattern that has to match the request URI in order to...