Question

Nginx server block displaying wrong site on droplet

I have 3 sites on one droplet. The first two work fine, but as I add a third following this guide it directs itself to the first site I uploaded to the droplet. I’ve triple checked my symlinks between sites-enabled and sites-available, and the files themselves within (including having a www. and non www. listed in the server_name line), and I can’t understand why this would be happening when the second site never had this problem following the same method. I even restored a backup of my entire droplet and redid it, but the problem persists. Below are the two files under sites-available (the only difference being an SSL cert by certbot in the first site):

Original Site

server {
  listen 80;
  listen [::]:80;
  
  root /var/www/anchoridea.com/html;
  index index.html index.htm index.nginx-debian.html;

  server_name anchoridea.com www.anchoridea.com;

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

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/anchoridea.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/anchoridea.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
    if ($host = www.anchoridea.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = anchoridea.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen 80;
  listen [::]:80;

  server_name anchoridea.com www.anchoridea.com;
    return 404; # managed by Certbot




}

New Site

server {
	listen 80;
	listen [::]:80;
	root /var/www/prairiestormpaintball.com/html;
	index index.html index.htm index.nginx-debian.html;

	server_name prairiestormpaintball.com www.prairiestormpaintball.com;

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

Any help would be greatly appreciated, thanks!


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.

KFSys
Site Moderator
Site Moderator badge
March 28, 2024

The Nginx configuration files for both your original site and the new site appear to be correctly set up, with distinct server_name directives and different root directories. The issue you’re experiencing, where the third site redirects to the first site, is commonly due to Nginx not properly recognizing the server block for the new site. Here’s a checklist to troubleshoot and resolve this issue:

1. Ensure Nginx Configuration is Loaded

  • Make sure the new site’s configuration is symlinked in the sites-enabled directory. You can check this with ls -l /etc/nginx/sites-enabled.

  • After making changes to Nginx configuration files or symlinks, always reload Nginx to apply the changes:

sudo nginx -t
sudo systemctl reload nginx

2. Check for Conflicting Server Blocks

  • Ensure there are no conflicting server blocks or duplicate server_name entries across your Nginx configuration files. Nginx will serve the first matching server block it finds, so any misconfiguration could cause it to serve the wrong site.

3. DNS Settings

  • Confirm that the DNS records for prairiestormpaintball.com and www.prairiestormpaintball.com are correctly pointing to the IP address of your droplet.

4. Default Server Block

  • Consider adding a default server block to catch requests that do not match any server_name. This can help identify if the issue is with Nginx’s selection of server blocks:
server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name _;
  return 444;
}

5. Server Block for HTTPS (SSL)

  • If you plan to use SSL for prairiestormpaintball.com, you will need to set up an SSL certificate and add a similar server block for listening on port 443, like you did for your original site.

6. Browser Cache

  • Clear your browser cache or try accessing the site in incognito mode. Browsers can cache redirects, leading to the appearance of a problem even after it’s been resolved server-side.

7. Error and Access Logs

  • Check the Nginx error and access logs for clues:
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log
  • his can provide insights into what’s happening when you try to access the new site.

8. File Permissions

  • Verify that the files in /var/www/prairiestormpaintball.com/html have the correct permissions and ownership for Nginx to access them.

Hi @CanuckLuck

I have tried to access your domains prairiestormpaintball.com, www.prairiestormpaintball.com, anchoridea.com, and www.anchoridea.com. They all point to their own website. There is no redirection. Did you fixed your problem?

Hi there @CanuckLuck,

I’ve seen the same behavior when the document root directory is not readable or writable by the Nginx user.

What I could suggest is making sure that your /var/www/prairiestormpaintball.com/html folder has the same permissions and ownership as the /var/www/anchoridea.com/html folder.

Also what I could suggest is checking your Nginx error log to see if there’s some more information about the problem:

  1. tail -100 /var/log/nginx/error.log

Let me know how it goes! Regards, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel