3

I have a hosting platform that hosts multiple clients' websites. It is a Catalyst app load balanced behind nginx. I would like each client to have the ability to choose whether they want to have their domain have www in front of it or no www in front of it, and then I would like to be able to redirect appropriately. My question is, is it best to handle something like this at the nginx level, or at the Catalyst level? Typically a simple www to non-www redirect would be handled at the load balancer level, but I think that since which way I'm redirecting will vary by domain it will maybe make more sense to handle the redirects at the Catalyst level? If so, what is the best way to handle a redirect from www to non-www (or viceversa) in Catalyst? Or if you think nginx would be better, what would be the best way to handle this using that? Thanks!

srchulo
  • 5,143
  • 4
  • 43
  • 72
  • How many domains are we talking about here? Is there a vhost for each domain? – innaM May 25 '13 at 11:30
  • @innaM right now there are 100 domains. And there will be more. There isn't a vhost for each one, I was trying to avoid that so I could just point domains at the nginx server and have it work without having to mess with the config every time. Currently I have one server block that passes all domains to the Catalyst app servers. Do you think the best way would just be to have a server block for each domain? That would be a lot of copying and pasting! – srchulo May 25 '13 at 16:10
  • I wasn't thinking of copy and paste, but of automating the process. After all, you're a perl programmer :-) But it's hard for me to say whether this is an option in your situation. – innaM May 26 '13 at 09:14
  • @innaM, well that is true :) I could automate it with perl, I guess I just didn't like the fact that I would have to restart nginx every time I added a new domain to the system so the config file could take effect, but perhaps that isn't as big a deal as I thought. Maybe this is the best option. – srchulo May 26 '13 at 15:42

2 Answers2

1

You could do this with the customer's DNS For example if you make www.domain.com a CNAME alias of domain.com and rewrite www.domain.com to domain.com with nginx. I would think that would be the simplest approach. Does it confuse Catalyst in some way?

Note also that using CNAME aliases in this way means you have to be careful with SSL certificates you might use for HTTPS.

G. Cito
  • 6,210
  • 3
  • 29
  • 42
  • but if I rewrite www.domain.com to domain.com with nginx, how will I know which domains to rewrite how if I have 20 different domains coming into nginx? Example, domaina.com needs to be redirected to www.domaina.com, and domainb.com needs to be redirected to www.domainb.com. Perhaps I'm misunderstanding your solution, but wouldn't I have to manually put this in the config file for nginx for each unique domain? – srchulo May 24 '13 at 19:22
  • Since this would happen at the DNS level there would not be any rewriting _per se_. Does Catalyst require one canonical domain in the request? I use reverse proxies as well so that may isolate me from issues you are seeing. – G. Cito May 30 '13 at 15:25
0

Web servers like nginx are very well suited to do simple URL redirections. With nginx, you can do the redirections like this:

server {
        listen       0.0.0.0:80;
        server_name  *.mydomain.com;

        rewrite ^(.*) http://mydomain$1 permanent;
}
Julien
  • 5,729
  • 4
  • 37
  • 60
  • I would like to be able to redirect from www to non-www, or viceversa per each individual domain. What you have here means I would have to have to define a server configuration for each domain I want to support. Once I have any decent number of domains to support, it's going to get really messy having to add this directive every time. – srchulo May 24 '13 at 23:13