Linode Forum
https://forum.linode.com/

WWW redirect with Load Balancer
https://forum.linode.com/viewtopic.php?f=19&t=9161
Page 1 of 1

Author:  polemos [ Thu Jul 19, 2012 9:49 am ]
Post subject:  WWW redirect with Load Balancer

We have Linode node-balancer which splits requests between 2 nginx servers.

The identical Nginx servers have been configured to serve for their IP Addresses (local) not to the domain name (mysite.com).
How I can setup a 301 redirect in those servers so that http://mysite.com/* is redirected to http://www.mysite.com

I have done similar configs for sites which did not use Node balancers, so I find this situation tricky.

Thanks in advance for your help.

Author:  hoopycat [ Fri Jul 20, 2012 1:02 pm ]
Post subject:  Re: WWW redirect with Load Balancer

Two approaches:

1) The wrong but easy way:

Code:
server {
  listen ...
  server_name example.com www.example.com ...;


  # Redirect www.example.com to example.com
  if ($host ~* (www)\.(.*)) {
    set $host_without_www $2;
    rewrite ^(.*)$ $scheme://$host_without_www$1 permanent;
  }


2) The more correct way:

Code:
server {
  listen ...
  server_name example.com ...;
  # All the existing configuration, but without www.example.com in the server_name
}

server {
  listen ...
  server_name www.example.com;

  location / {
    rewrite ^/(.*) $scheme://example.com/$1 permanent;
  }
}


The node balancer does not make a difference here.

Author:  polemos [ Fri Jul 20, 2012 2:02 pm ]
Post subject:  Re: WWW redirect with Load Balancer

@hoopycat Thanks for your reply.

Can you please clarify one thing. How does the nodebalancer request the web servers? Via the IP with the domain name, or just the IP.
In other words, will the web servers answer to the domain name or to the IP?

Author:  caker [ Fri Jul 20, 2012 2:06 pm ]
Post subject:  Re: WWW redirect with Load Balancer

The Host header is not modified, so your webserver will know which virtualhost to serve from.

-Chris

Page 1 of 1 All times are UTC-04:00
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/