2

I haven't seen anything related to this topic on Google and since I'm a newbie on Nginx I'd like to ask a question about load balancing: I have a dedicated server currently running Apache with multiple accounts and domains. I'd like to switch to Nginx and set up a load balance only for one of these domains (mydomain1.com) to load balance traffic between this dedicated server and another 3 ones. I have the following Nginx config (/etc/nginx/conf.d/default.conf) on my dedicated server:

upstream mywebsite1  {
  ip_hash;
  server xxx.xxx.xxx.196 weight=1 max_fails=3 fail_timeout=15s;
  server xxx.xxx.xxx.67 weight=1 max_fails=3 fail_timeout=15s;
  server xxx.xxx.xxx.201 weight=1 max_fails=3 fail_timeout=15s;
}

server {
    listen       80;
    server_name  mywebsite1.com;

    access_log /var/log/nginx/proxy.log;

    location / {
        proxy_pass http://mywebsite1;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

}

But this is not working and when I read the proxy.log is also balancing traffic not just from mywebsite1.com, but also from my other domains: mywebsite2.com, mywebsite3.com, etc. Any help is really appreciated since as you see I'm not an expert! Thanks :)

simon
  • 33
  • 1
  • 6

2 Answers2

3

I know it is years old question, but it still might help someone.

To make it work like you want, you must define at least two virtualhosts (server blocks).

1st is so called "default" - that is it serves everything that is not defined in any other virtualhost. Default in nginx context means defining:

server_name _;

You can add index.html to that virtualhost to tell visitors to go to right place. Display some sort of error message. Or redirect visitors to right place without any message - what ever suits your purposes. But some sort of default is required if you want your other virtualhost block(s) to serve only specific domain(s) and nothing else.

2nd is "mywebsite1.com" - that only serves that particular domain. Your configuration for that domain is correct. And you can add more virtualhost blocks for different domains.

If you only have one virtualhost (even if it is not "default" type) then every single http request will go to that virtualhost, regardless if domain name matches or not.

You need to keep in mind that you should define different root path for every virtualhost, unless you want them all so serve same content.

root /some/path;

Which domain is served by which virtualhost is defined through server_name directive. "_" means default and serves anything that does not match some other virtualhost.

You can define more than one domain if you want virtualhost block to serve more than one (do not forget to add both with and without www if you want both to work):

server_name www.example.com example.com some.other.domain.com;

You can also use wildcards:

server_name *.example.com;

So correct config file would be something like this:

# default virtualhost to serve everything that does not match other virtualhosts
server {
    listen       80;
    server_name  _;

    root /some/path/default_site;

    # add other rules for default site
}

# virtualhost to server only (www.)mywebsite1.com
server {
    listen       80;

    # please note that you need to add both with and without "www." if you want both to work.
    server_name  mywebsite1.com www.mywebsite1.com;

    root /some/path/mywebsite1.com;

    # add other rules for mywebsite1.com

}

# virtualhost for example.com (without www)
server {
    listen       80;

    server_name  example.com;

    root /some/path/example.com;

    # add other rules for example.com

}
srx
  • 31
  • 2
1

If you send all of your traffic to your Nginx server, it has to do something with it. Since you only have one server block, regardless of what the server name is configured to be it will take the traffic for all host names.

If you don't want Nginx to handle traffic for all of your domains, simply don't point all of your domains at it (with DNS).

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Thank you Brad, and how do I point only one domain if Nginx is running on the server? Do I change it on the nginx conf file? – simon Jun 10 '14 at 15:39
  • @simon No, configure your DNS entries. It has nothing to do with Nginx. – Brad Jun 10 '14 at 15:43