Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include redirects on external file?

Tags:

redirect

nginx

I need to setup about 5-6k redirects on a domain (for a site migration), and I'm new to nginx. I have some test redirects working in the main .conf file for the domain. But I don't want to have 5k+ rewrites in the main .conf file so I have been told that I can include a external file in the .conf to keep it clean, so my main .conf like this

server {
  listen.....etc
  etc

  rewrite ^oldurl newurl permanent;
  rewrite ^oldurl newurl permanent;
  include /etc/nginx/conf.d/redirects.conf;

  location  ....etc
  etc
}

Then in the redirects.conf I just have

rewrite ^oldurl newurl permanent;

But when I try to restart nginx I get the error:

"rewrite" directive is not allowed here in /etc/nginx/conf.d/redirects.conf:1 

Thanks

like image 779
PaddyD Avatar asked Mar 25 '15 10:03

PaddyD


1 Answers

OK, issue was I was calling the extenal file redirects.conf, since every file ending in .conf is considered a site configuration file.

I changed it to sitename.redirects and now it works fine

like image 80
PaddyD Avatar answered Oct 22 '22 11:10

PaddyD