0
0
Nginxdevops~5 mins

Rewrite directive in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, you want to change the web address a user types into something else behind the scenes. The rewrite directive in nginx helps you do this by changing URLs before the server processes them.
When you want to redirect old website pages to new ones without breaking links.
When you need to remove or add parts of a URL for cleaner addresses.
When you want to force all traffic to use HTTPS instead of HTTP.
When you want to hide query parameters from users by rewriting URLs.
When you want to organize URLs to match your app’s internal structure.
Config File - nginx.conf
nginx.conf
server {
    listen 80;
    server_name example.com www.example.com;

    location = /old-page {
        rewrite ^/old-page$ /new-page permanent;
    }

    location / {
        root /var/www/html;
        index index.html index.htm;
    }
}

This configuration listens on port 80 for example.com and www.example.com.

The location = /old-page block uses the rewrite directive to change requests for /old-page to /new-page permanently.

The location / block serves files from the website folder.

Commands
Check the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Test the rewrite by requesting the old URL and checking the HTTP headers for a redirect.
Terminal
curl -I http://example.com/old-page
Expected OutputExpected
HTTP/1.1 301 Moved Permanently Server: nginx Location: http://example.com/new-page Content-Type: text/html Content-Length: 178 Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: the rewrite directive changes URLs before nginx processes them, enabling redirects and cleaner URLs.

Common Mistakes
Using rewrite without specifying 'permanent' or 'redirect' flag when a redirect is intended.
Without these flags, nginx rewrites the URL internally but does not tell the browser to change the address, causing confusion.
Add 'permanent' for a 301 redirect or 'redirect' for a 302 redirect to inform the browser.
Not testing nginx configuration with 'nginx -t' before reloading.
Syntax errors can cause nginx to fail to reload, leading to downtime.
Always run 'sudo nginx -t' to verify configuration before reloading.
Placing rewrite directives outside of a location or server block.
Rewrite directives must be inside server or location blocks to work properly.
Put rewrite rules inside the appropriate server or location block.
Summary
Use the rewrite directive inside server or location blocks to change URLs.
Test your nginx configuration with 'nginx -t' before reloading to avoid errors.
Reload nginx with 'systemctl reload nginx' to apply changes without downtime.
Verify rewrites by requesting the old URL and checking for proper redirects.