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.

3 Replies

Two approaches:

1) The wrong but easy way:

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:

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.

@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?

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

-Chris

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct