In Nginx, rewrite rules are used to modify or redirect URLs, allowing you to control how incoming requests are processed and handled.
Location Block:
Rewrite rules are typically added within a specific location block in the Nginx configuration file (nginx.conf
) or in a separate configuration file in the /etc/nginx/conf.d/
directory. The location block determines the context in which the rewrite rule will be applied.
server {
listen 80;
server_name example.com;
location / {
# Rewrite rule goes here
}
}
In this example, the rewrite rule will be applied to all requests under the / location.
Basic Rewrite Rule Syntax:
The basic syntax for a rewrite rule in Nginx is as follows:
rewrite regex replacement [flag];
regex
: A regular expression that matches the part of the URL you want to rewrite.replacement
: The replacement string that will replace the matched part of the URL.flag
(optional): Specifies additional rewrite flags for controlling the rewrite behavior.
Simple URL Rewrite:
Let's start with a simple example of rewriting a URL. Suppose you want to redirect requests from /old-page
to /new-page
. The rewrite rule would look like this:
location / {
rewrite ^/old-page$ /new-page permanent;
}
The ^/old-page$
regular expression matches only the exact /old-page
URL. The permanent flag indicates a permanent redirect (HTTP 301). If you want a temporary redirect (HTTP 302), you can use the redirect flag instead.
Rewrite with Query Parameters:
You can also rewrite URLs while preserving query parameters. For example, redirecting /old-page?id=123 to /new-page?id=123:
location / {
rewrite ^/old-page$ /new-page$is_args$args permanent;
}
The $is_args$args
variable preserves the query parameters from the original request.
Rewrite Flags:
Rewrite flags provide additional control over how the rewrite rule is processed. Some common flags include:
last
: Stops processing the current set of rewrite rules and restarts the search using the modified URL.break
: Stops processing the current set of rewrite rules and applies the modified URL.redirect or permanent:
Performs a permanent redirect (HTTP 301).redirect or temporary
: Performs a temporary redirect (HTTP 302).
Flags can be combined by separating them with a comma.
Server Variables:
Nginx provides various server variables that you can use in rewrite rules to capture and reference parts of the URL. For example:
$request_uri:
The original request URI.$args:
The query string.$host
: The hostname from the request.$http_referer
: The referring URL.
These variables can be used in regex patterns or as replacements in rewrite rules.
Conditionally Redirecting:
You can apply rewrite rules conditionally using if blocks. However, caution should be exercised as if blocks can have performance implications. Here's an example:
location / {
if ($request_uri = /old-page) {
return 301 /new-page;
}
}
It's recommended to use if blocks sparingly and consider alternative solutions using try_files or map directives when possible.
Redirecting HTTP to HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
This rule redirects all HTTP requests for example.com to HTTPS.
Removing www from the URL
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
This rule removes the www subdomain from the URL.
Redirecting URLs with query parameters
server {
...
rewrite ^/product/(.*)$ /new-product?sku=$1 redirect;
...
}
This rule redirects /product/123 to /new-product?sku=123, where 123 is the value captured by (.*).
Redirecting specific file extensions
server {
...
rewrite ^/(.*)\.html$ /$1.htm permanent;
...
}
This rule redirects requests for .html files to the same URL but with the .htm extension.
Testing and Reloading Configuration:
After adding or modifying rewrite rules, check the Nginx configuration for syntax errors:
sudo nginx -t
If the configuration is valid, reload or restart Nginx to apply the changes:
sudo service nginx reload
It's essential to test the rewrite rules thoroughly to ensure they behave as expected and don't introduce unintended redirects or infinite loops.
Remember to monitor your server's access logs and regularly test your rewrite rules to ensure they continue to function correctly with any changes or updates made to your website or application.