Nginx 301 rewrites rules in Trellis

I am aware of the Nginx includes , I am using this for the Leverage browser caching and for proxy rules, but I never used it for 301 rewrites.

in the past, I always used the plugin that you Root guys recommend for redirects Safe Redirect Manager – WordPress plugin | English (UK) but I saw that when you have more than 250 redirects is not a recommended option because it can slow down the website.

so ideally I would like to write 301 rules in a rewrites.conf.j2 file so Trellis can take care of the redirects. (and I won’t need to use a plugin just for this, that it is always good to avoid using an extra plugin)

should I include the server or location context?

 location = /some/url { 
   rewrite ^/some/url?$ /some/other/url permanent; 

or I can just write the rewrite rule (without location or server context) in the rewrites.conf.j2 file ?

rewrite ^/some/url$ /some/other/url permanent;
rewrite ^/some/url-post$ /some/other/url permanent;
rewrite ^/some/url-page$ /some/other/url permanent;

and moreover, from a performance point of view, is it always better to write the redirect directly in the Nginx config or more or less the performance are the same with a plugin (safe-redirect-manager) doing redirects?


This is what you want :+1: We do the same for our site

Yes doing them in the Nginx would be best performance you could get since it happens before PHP even executes and has to load WordPress.

thanks @swalkinshaw :raised_hands: :raised_hands: :raised_hands:

is there a roots repository where I can see which rules you guys wrote in the Nginx includes files? (I saw that doesn’t have the nginx-includes, so I was wondering if there is another repository with the nginx-includes in place)

moreover, do you know more or less, how many Nginx redirects is too many (performance-wise)?
Currently, we have a list of around 320 redirects, I know that Nginx is very fast at handling redirects, but presumably, there must be some number of redirects that will affect performance.


No, it’s a private repo but it’s exactly what you had. Example:

rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;

No idea :man_shrugging: we have 4

1 Like