benunc
By:
benunc

Nginx server blocks not working.

April 13, 2015 8k views
Nginx LEMP

Brand new customer here! Just created a one-click LEMP stack on 14.04, and followed the steps here for setting up server blocks.

Today's goal is just to be able to access those two separate subdirectories from my browser.

visiting http://45.55.179.247 correctly shows the index.html file at /var/www/benandjacq.com/html

based on these settings (softlinked) in etc/nginx/sites-enabled/benandjacq.com :

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

    root /var/www/benandjacq.com/html;
    index index.html index.htm;

    server_name 45.55.147.249;

    location / {
        try_files $uri $uri/ =404;
    }
}

but when I try to visit http://45.55.179.247/cwh.com I am not shown the index.html file at /var/www/cwh.com/html in spite of these settings (softlinked) in etc/nginx/sites-enabled/cwh.com:

server {
    listen 80;
    listen [::]:80;

    root /var/www/cwh.com/html;
    index index.html index.htm;

    server_name 45.55.147.249;

    location / {
        try_files $uri $uri/ =404;
    }
}

Question: What URL should I be visiting to confirm my server blocks are set up correctly, without actually pointing my DNS records here?

1 comment
  • For this to work you would need to specify a different server_name directive. Nginx takes this directive when dealing with http requests and decides which server block to apply based on that and since both blocks have the same server_name then nginx just decides to serve the first one.
    One thing though, I would recommend using real domains and/or subdomains to test this, if you want to just test then I suggest setting up a couple of subdomains as an A records in your DNS to point to your IP and then configure nginx with those as server_name, e.g.

    test1.domain.com in A 45.55.147.249
    test2.domain.com in A 45.55.147.249

    And then exchange that in your blocks:

    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    
        root /var/www/benandjacq.com/html;
        index index.html index.htm;
    
        server_name test1.domain.com;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
    
    server {
        listen 80;
        listen [::]:80;
    
        root /var/www/cwh.com/html;
        index index.html index.htm;
    
        server_name test2.domain.com;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
    
3 Answers

Server Blocks can be used to route different domains but would not be the thing to do to route requests for different folders. Instead you would want a single server block with two location blocks:

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

    root /var/www/benandjacq.com/html;
    index index.html index.htm;

    server_name 45.55.147.249;

    location / {
        try_files $uri $uri/ =404;
    }
    location /cwh.com {
       alias  /var/www/cwh.com/html/
    }
}
  • so, once I have two legit different domains going to that ip address, my current setup will work (given a change in server_name ) ?

You can't have two vhosts on the same IP with the same server_name, otherwise how would the web server know which vhost you're trying to reach?

You want to combine those in the same vhost since it's the same site. If they aren't, then you need two vhosts with two different server_names.

No. Once you have two domains you will need two server blocks like you had originally set up but including server_name definitions for the two domains. Unfortunately the way a sub-folder is handled is quite different from the way a domain or subdomain is handled.

Have another answer? Share your knowledge.