Cookie Consent by Free Privacy Policy Generator

Nginx Directives, Rules and Blocks for SEO

If you're using Nginx for SEO and care about how your site performs in search, it makes sense to go beyond just serving pages fast. Nginx gives you loads of control at the edge, but most of the good SEO bits live under the radar.

I’ve pulled together a list of Nginx directives I’ve found genuinely useful when working on technical SEO projects for sites that use Nginx. Some improve crawl efficiency, some tighten up redirect logic, and others just make your server behave better in real-world search scenarios. Practical config-level improvements you can actually use.

Comment below to add your own if I’ve missed any.

Rewrite​

Used to create clean, SEO-friendly URLs and manage redirects. Or use my tool - https://chrisleverseo.com/tools/nginx-redirect-generator/
Code:
rewrite ^/old-page$ /new-page permanent;

Return​

Simpler and faster than rewrite for straight redirects.
Code:
return 301 /new-url;

Map​

Lets you conditionally rewrite URLs based on patterns. Good for stripping tracking parameters or routing.
Code:
map $request_uri $clean_uri {
    default $request_uri;
    ~^/product/.*\?.*utm_ $uri;
}

If​

Useful for conditional logic in rewrites or redirect rules, though best used carefully.
Code:
if ($request_uri ~* "utm_") {
    return 301 $uri;
}

Try_files​

Helps prevent unnecessary 404s by falling back to index or clean versions of URLs.
Code:
try_files $uri $uri/ /index.php?$args;

Expires and Cache-control​

Controls browser caching to reduce load and improve speed.
Code:
location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico)$ {
    expires 30d;
    add_header Cache-Control "public, no-transform";
}

Gzip​

Enables compression of text-based assets like HTML, CSS, JS.
Code:
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;

add_header​

Used for security headers and to set canonical control headers if needed.
Code:
add_header X-Robots-Tag "noindex, nofollow";

error_page​

Used to handle 404s or 410s with custom pages or logic.
Code:
error_page 404 /404.html;

limit_req​

Can be used to protect crawl budget or guard against excessive requests. (be careful on this one with Googlebot)
Code:
limit_req_zone $binary_remote_addr zone=botlimit:10m rate=10r/s;

server blocks​

Allow you to define canonical hosts and prevent duplicate content across www and non-www.
Code:
server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}


log_format + access_log​

Helps monitor crawler activity, identify indexation issues, and spot crawl traps. If you want to know whether Googlebot is actually crawling your critical pages, this is where you find out.
Code:
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent"';

access_log  /var/log/nginx/access.log  main;

geo + map for Geo-IP Logic​

Can be used to serve region-specific content or redirect based on location without relying on JavaScript or cookies. Then map it to a redirect or header - good for multilingual or international SEO strategies when used carefully.
Code:
geo $geo_country {
    default ZZ;
    include /etc/nginx/geoip-country.conf;
}

proxy_cache​

Improves load time by caching upstream content responses, useful when working with headless or API-driven content that doesn’t change often.
Code:
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;

add_header Link​

You can use this to set canonical tags at the edge, which is helpful in edge rendering setups.
Code:
add_header Link '<https://example.com/canonical-page>; rel="canonical"';

set_real_ip_from + real_ip_header​

Useful when Nginx is behind a CDN or load balancer and you want to log the real user IP. That lets you segment bots from real users more accurately.
Code:
set_real_ip_from  0.0.0.0/0;
real_ip_header    X-Forwarded-For;

client_max_body_size​

Avoids 4xx errors from users or bots uploading content that exceeds the allowed size. Prevents indexation blocks for file-heavy pages.
Code:
client_max_body_size 10M;

disable_symlinks​

Security hardening. Stops Nginx from following symlinks. Keeps bots out of hidden directories that shouldn’t be exposed or crawled.
Code:
disable_symlinks if_not_owner from=/var/www/html/;
 
Last edited:
Back
Top