Question

Nginx server blocks not working.

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?

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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/
    }
}

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.