0

I'm a noob in nginx. I start configuring my domains (marianamarques.ntr.br and fabricadevozes.com.br) dns to point to my aws ec2 instance public ip. Thats ok. When i start configure the nginx:

  • i created /var/www
  • i created /var/www/marianamarques.ntr.br/public_html
  • i created /var/www/fabricadevozes.com.br/public_html
  • i created /etc/nginx/sites-availble/marianamarques.ntr.br.conf listening to port 80 with root pointing to /var/www/marianamarques.ntr.br/public_html
  • i created /etc/nginx/sites-availble/fabricadevozes.com.br.conf listening to port 80 with root pointing to /var/www/fabricadevozes.com.br/public_html

When i tell on browser http://www.fabricadevozes.com.br i got htmls from /var/www/fabricadevozes.com.br/public_html but when i tell on browser http://www.marianamarques.ntr.br i got htmls from /var/www/fabricadevozes.com.br/public_html too.

I'm a bit desperated. My nginx was installed from apt-get and after hours and hours of web searches i know my /etc/nginx/conf.d/nginx.conf was missing (i don't have this file) but my nginx server starts with no issues.

Anybody can help?

Kara
  • 6,115
  • 16
  • 50
  • 57
Vinicius Souza
  • 113
  • 3
  • 9
  • Do you have server_name for those sites configured? Also did you look at [this question](http://stackoverflow.com/questions/11773544/nginx-different-domains-on-same-ip)? – liepumartins Feb 19 '14 at 14:34

3 Answers3

1

nginx.conf should be located in /etc/nginx.

Post it along with your config files in sites-enabled and it'll be easier to tell you exactly what's wrong, but sounds like you may have a mistake in the server_name or root directives in your server definition. Make sure you specify the server name with and without the www. It could be loading your default domain if you didn't specify www. in the server name

server {
  listen 80;
  server_name marianamarques.ntr.br www.marianamarques.ntr.br;
  root /var/www/marianamarques.ntr.br/public_html;
  ...
}

If thats not it, we'd need to see the config files.

  • thanks for the answer. My config files in sites-enabled is very similar to what you describe. In server_name i use with and without www. domains (eg. server_name marianamarques.ntr.br www.marianamarques.ntr.br;). But i found something now: when i call marianamarques.ntr.br without www prefix the nginx points to correct root /var/www/marianamarques.ntr.br/public_html but when i call marianamarques.ntr.br with www prefix nginx points to /var/www/fabricadevozes.com.br/public_html – Vinicius Souza Feb 19 '14 at 14:33
0

Solved based on the link Nginx no-www to www and www to no-www - i create a second server listening to same port but expecting the server_name with-www prefix and then rewrite this to my other server point to without-www prefix like this:

server {
    server_name  www.domain.com;
    rewrite ^(.*) http://domain.com$1 permanent;
}

server {
    server_name  domain.com;
    #The rest of your configuration goes here#
}

I expect it can help others. Thanks a lot for everything! And sorry... my english is very poor!

Community
  • 1
  • 1
Vinicius Souza
  • 113
  • 3
  • 9
0

Apparently you solved the problem but let me explain why it happened, you should read How nginx processes a request

Quote:

In this configuration nginx tests only the request’s header field “Host” to determine which server the request should be routed to. If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port. In the configuration above, the default server is the first one — which is nginx’s standard default behaviour. It can also be set explicitly which server should be default, with the default_server parameter in the listen directive

When you had a www or a non-www site missing, nginx couldn't match it to any server so it sends the request to the default server, and assuming you removed the default file from sites-enabled and didn't set any as default then the default server is the first one alphabetically, if we compare marianamarques.ntr.br.conf vs fabricadevozes.com.br.conf then the winner is the one starting with an f, that's why it was showing that server instead.

And since you did the basic redirection server, let me add that you better have used return over rewrite, check taxing rewrites

server {
  server_name www.example.com;
  return 301 http://example.com$request_uri$is_args$query_string;
}
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89