rewrite – What’s the best NGINX “redirect” solution from the following options?

NGINX doesn’t do Apache-style redirects. We want to replace these three URL queries “/grid/grid.html”, “grid/grid.html”, “grid.html” with just “/grid/”.

We have separate domain-specific config files inside “/etc/nginx/sites-available”, and use CertBot for SSL.

The RegEx in the examples below captures all the URL variations listed above. But which of these two is best practice:

  1. a location block inside the existing server block:
server {
  ...
  location ~ ^(((/?)grid/)?grid.html) {
    return 301 $scheme://grid/;
    # OR https://grid/;
  }
  ...
}  
  1. add a rewrite block inside the existing server block, using the site root:
server {
  ...
  rewrite ^(((/?)grid/)?grid.html) /grid/ last;
  return 403;
  ...
}

When updating the cert, Certbot writes to the existing server block in these config files (as well as adding its own separate server block at the end of the same config file).

Read more here: Source link