Question

In Nginx, can 301 redirect URLs be grouped in an external file that can be included in the .conf file of the domain so that .conf is not modified each time a 301 redirect is needed?

Example:

A .nginx file, .redirects groups only 301 redirects:

location /test1 {
  rewrite ^ http://domain.com/test2 permanent;
}

Then, .redirects is included in the .conf file:

include /.redirects;

Can it work? (I've tested this and it didn't work for me.)

Was it helpful?

Solution

It would work but you need to mind one thing, what context is this file going to be included to.

In your condition it's a location block, so you need to include it inside a server block, not http for example,

Also use full path not relative

server {
  # bla bla
  include /etc/nginx/.redirects;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top