Server blocks configured, but getting "hello world" of nginx

I’ve followed the tutorial below to setup a couple of server blocks, but
I
get the “Welcome to nginx” message every time.

$ ls -l /etc/nginx/sites-available/
total 8
-rw-r–r-- 1 root root 1185 Feb 2 17:01 morebearsmore.com
-rw-r–r-- 1 root root 2744 Feb 2 17:07 strattonbrazil.com

$ ls -l /etc/nginx/sites-enabled/
total 0
lrwxrwxrwx 1 root root 44 Feb 2 17:03 morebearsmore.com
/etc/nginx/sites-available/morebearsmore.com
lrwxrwxrwx 1 root root 45 Feb 2 16:44 strattonbrazil.com
/etc/nginx/sites-available/strattonbrazil.com

This is the contents of more of the configs (minus the comments at the
top).

server {
listen 80;
listen [::]:80 default_server ipv6only=on;

root /var/www/morebearsmore.com/public_html;

index index.html index.htm;

Make site accessible from http://localhost/

server_name morebearsmore.com;

location / {

First attempt to serve request as file, then

as directory, then fall back to displaying a 404.

try_files $uri $uri/ /index.html;

Uncomment to enable naxsi on this location

include /etc/nginx/naxsi.rules

}

location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}

}

I’ve added a “hello world” index file to that directory, too.

$ ls -l /var/www/strattonbrazil.com/public_html/index.html
-rw-r–r-- 1 root root 148 Feb 2 16:41 /var/www/
strattonbrazil.com/public_html/index.html

$ cat /var/www/strattonbrazil.com/public_html/index.html

www.strattonbrazil.com

Success: You Have Set Up a Virtual Host

But again every time I get the same welcome message. Here’s the access
log
for hitting morebearsmore.com a few times from my web browser. I don’t
see
any messages in the error log.

71.217.116.55 - - [02/Feb/2014:17:13:57 +0000] “GET / HTTP/1.1” 304 0
“-”
“Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)
Ubuntu Chromium/31.0.1650.63 Chrome/31.0.1650.63 Safari/537.36”
71.217.116.55 - - [02/Feb/2014:17:14:00 +0000] “-” 400 0 “-” “-”
71.217.116.55 - - [02/Feb/2014:17:14:07 +0000] “GET / HTTP/1.1” 304 0
“-”
“Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)
Ubuntu Chromium/31.0.1650.63 Chrome/31.0.1650.63 Safari/537.36”

On Sunday 02 February 2014 09:14:03 Josh S. wrote:

$ ls -l /etc/nginx/sites-enabled/
total 0
lrwxrwxrwx 1 root root 44 Feb 2 17:03 morebearsmore.com
/etc/nginx/sites-available/morebearsmore.com
lrwxrwxrwx 1 root root 45 Feb 2 16:44 strattonbrazil.com
/etc/nginx/sites-available/strattonbrazil.com

This is the contents of more of the configs (minus the comments at the
top).
[…]

What’s in your nginx.conf?

wbr, Valentin V. Bartenev

This is my nginx.conf page, which I haven’t done anything with. The
/etc/nginx/conf.d/ directory on my machine is empty.

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
worker_connections 768;

multi_accept on;

}

http {

Basic Settings

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

server_tokens off;

server_names_hash_bucket_size 64;

server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

Logging Settings

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

Gzip Settings

gzip on;
gzip_disable “msie6”;

gzip_vary on;

gzip_proxied any;

gzip_comp_level 6;

gzip_buffers 16 8k;

gzip_http_version 1.1;

gzip_types text/plain text/css application/json

application/x-javascript
text/xml application/xml application/xml+rss text/javascript;

nginx-naxsi config

Uncomment it if you installed nginx-naxsi

#include /etc/nginx/naxsi_core.rules;

nginx-passenger config

Uncomment it if you installed nginx-passenger

#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;

Virtual Host Configs

include /etc/nginx/conf.d/.conf;
include /etc/nginx/sites-enabled/
;
}

I think I have everything working as expected. The only thing that’s
still
strange to me is when I go to the morebearsmore.com domain with “www”
prefixed to it, it goes to the test html file in the other server block.
I
had this problem in apache, so I switched to nginx and I’m still seeing
it.
I tried to setup both server blocks at the same time. Why would
www.morebearsmore.com go to my strattonbrazil.com directory while the
other
morebearsmore.com goes to the correct directory? I figured with a fresh
install of nginx, I would see it “default” to one or the other. Is
strattonbrazil.com just happening to be the fallback?

Nevermind. I found the answer here that fixed it. I’m redirecting from
www now. Still don’t understand why it fell back to the other server
block.

server {
server_name www.morebearsmore.com;
return 301 http://morebearsmore.com$request_uri;
}

That’s strange. It only fixed it on my desktop. It still goes to the
strattonbrazil.com site when I type in www.morebearsmore.com on my
phone,
which was the original problem. Is the phone doing some kind of
caching?
Why would this happen on a windows phone and iphone with nginx (and
apache
when I tried it) but not my desktop?

On Monday 03 February 2014 09:13:24 Josh S. wrote:

This is my nginx.conf page, which I haven’t done anything with. The
/etc/nginx/conf.d/ directory on my machine is empty.
[…]
include /etc/nginx/conf.d/.conf;
include /etc/nginx/sites-enabled/
;
}

Ok. Did you reload nginx after the configuration was added to
“sites-enabled”?

wbr, Valentin V. Bartenev

As a test, if I add a querystring to see if it breaks the cache, it does
work. Is this an ISP cache?

www.morebearsmore.com goes to strattonbrazil.com server block
www.morebearsmore.com?foo=7 goes to the correct morebearsmore.com server
block

On Monday 03 February 2014 10:52:29 Josh S. wrote:

Nevermind. I found the answer here that fixed it. I’m redirecting from
www now. Still don’t understand why it fell back to the other server
block.
[…]

This article should shed the light:
http://nginx.org/en/docs/http/server_names.html

wbr, Valentin V. Bartenev

How long is that cache kept? If it redirected to the other one, will be
redirect on my phone indefinitely? I tried clearly my phone’s settings
and
it still pulls up the other site’s page–the old page, too, as I’ve
changed
the words. Is nginx saying this page is cacheable and thus not
returning
the new version because the browser uses the old one?

This article should shed the light:
Server names

Thanks for the link. That seems pretty clear, but how is nginx deriving
the hostname? If I run hostname I get “home” back. I still don’t
understand why it fell back to the other one.

On Monday 03 February 2014 10:57:33 Josh S. wrote:

As a test, if I add a querystring to see if it breaks the cache, it does
work. Is this an ISP cache?

www.morebearsmore.com goes to strattonbrazil.com server block
www.morebearsmore.com?foo=7 goes to the correct morebearsmore.com server
block

[…]

More likely your browser cache.

wbr, Valentin V. Bartenev

On Monday 03 February 2014 11:21:06 Josh S. wrote:

How long is that cache kept?

It depends on browser if there were no cache-related headers in
response.

If it redirected to the other one, will be redirect on my phone indefinitely?
I tried clearly my phone’s settings and it still pulls up the other site’s
page–the old page, too, as I’ve changed the words. Is nginx saying this
page is cacheable and thus not returning the new version because the browser
uses the old one?

By default nginx doesn’t return any cache-related headers. If the
browser has
cached some page, it can show the page to user without requesting
server.

This article should shed the light:
Server names

Thanks for the link. That seems pretty clear, but how is nginx deriving
the hostname? If I run hostname I get “home” back. I still don’t
understand why it fell back to the other one.

If there is no server defined for a requested host, it falls back to
that one
with the “default_server” parameter in the listen directive for a
specific
address:port pair, or to the first one in the configuration if there is
no such
parameter.

wbr, Valentin V. Bartenev

On Monday 03 February 2014 11:48:41 Josh S. wrote:

or to the first one in the configuration if there is no such
parameter.

As if all the server blocks are configured together? That sounds really
strange to me, that one server block could be the default for another
server block.

They all are configured together since they share the same address:port
pair.
The only separation that server can make is based on requested host, but
what
if there is no matches among configured server names? Then nginx picks
one
based on a simple rule I already mentioned.

And there is no magic in placing them in different configuration files
(in fact,
that “sites-available” thing is just a debian way of splitting web
server
configuration), they could be configured in one file as well.

rgrep default_server /etc/nginx/

/etc/nginx/sites-available/strattonbrazil.com: #listen [::]:80
default_server ipv6only=on;
/etc/nginx/sites-available/morebearsmore.com: #listen [::]:80
default_server ipv6only=on;

Note, that you have “default_server” for IPv6 only, but your listen
directives
for IPv4 haven’t got this parameter.

wbr, Valentin V. Bartenev

or to the first one in the configuration if there is no such
parameter.

As if all the server blocks are configured together? That sounds really
strange to me, that one server block could be the default for another
server block.

rgrep default_server /etc/nginx/

/etc/nginx/sites-available/strattonbrazil.com: #listen [::]:80
default_server ipv6only=on;
/etc/nginx/sites-available/morebearsmore.com: #listen [::]:80
default_server ipv6only=on;

On Monday 03 February 2014 12:32:53 Josh S. wrote:

Right, I actually have those lines commented out. That’s the part I don’t
understand. For example, if I put everything in the same file (example
below), neither one of them have a default_server or a wildcard. The only
other option I see from the link you sent me is www.morebears.com is
getting “defaulted” by nginx to the hostname. However, I don’t know what
about my server would default to strattonbrazil.com. Based on the below
settings, I don’t see how that should be happening and in my system
environment I don’t see anything like hostname that would also direct to
strattonbrazil.com.
[…]

It’s up to your DNS server configuration. Nginx knows nothing about it,
it just receives requests that actually can contain arbitrary host name.

wbr, Valentin V. Bartenev

What does the DNS server have to do with it? I thought it just
translated
domain names to IP addresses. I thought the browser queries the DNS,
gets
the IP from the domain name (which is the same for both domains–with or
without www), and returns it to the browser. The browser then fires the
request to that IP address with the domain inside the HTTP header, which
nginx uses to determine the correct server block.

Right, I actually have those lines commented out. That’s the part I
don’t
understand. For example, if I put everything in the same file (example
below), neither one of them have a default_server or a wildcard. The
only
other option I see from the link you sent me is www.morebears.com is
getting “defaulted” by nginx to the hostname. However, I don’t know
what
about my server would default to strattonbrazil.com. Based on the below
settings, I don’t see how that should be happening and in my system
environment I don’t see anything like hostname that would also direct
to
strattonbrazil.com.

server {
listen 80;
#listen [::]:80 default_server ipv6only=on;

root /var/www/morebearsmore.com/public_html;

index index.html index.htm;

}

server {
listen 80;
#listen [::]:80 default_server ipv6only=on;

root /var/www/strattonbrazil.com/public_html;

index index.html index.htm;

}

On Monday 03 February 2014 12:59:28 Josh S. wrote:

What does the DNS server have to do with it? I thought it just translated
domain names to IP addresses. I thought the browser queries the DNS, gets
the IP from the domain name (which is the same for both domains–with or
without www), and returns it to the browser. The browser then fires the
request to that IP address with the domain inside the HTTP header, which
nginx uses to determine the correct server block.

Right. But your configuration only had two server blocks: one with
server_name “morebearsmore.com” and one with server_name
strattonbrazil.com”,
so there is no one for “www.morebearsmore.com”, and since you didn’t
have
“default_server” parameter in any of them, then nginx just picked up the
first included in nginx.conf file.

wbr, Valentin V. Bartenev

On Monday 03 February 2014 14:28:41 Josh S. wrote:

Right, and that’s fine. It just seems a bizarre behavior. I would have
expected an nginx error or something. Thanks for all your help getting it
figured out. nginx’s configuration seems very intuitive in general.

Well, I tend to agree with you.

I believe this behavior comes from Apache, and the original idea was to
be
familiar for people who migrates from it (which means for all about 10
years
ago, when the first version of nginx was released).

wbr, Valentin V. Bartenev

Right, and that’s fine. It just seems a bizarre behavior. I would have
expected an nginx error or something. Thanks for all your help getting
it
figured out. nginx’s configuration seems very intuitive in general.