Question

nginx multiple domains on one droplet configuration

Hi, I am having some issues with setting multiple domains on one droplet as the 2nd and 3rd subdomains point to the same folder of the 1st domain. I have domain1 set up and working. The 2nd and 3rd server blocks I add are showing me the website I set up for the 1st domain. I symlinked the conf files from sites-available with sites-enabled, added the dns records on digital ocean admin and send them to someone else to set them in the domain name registrar dns records. These are the server blocks I have in sites-available along with the default config file: domain1.com

server {

	root /var/www/domain1.com/html;

	index index.html index.php index.htm index.nginx-debian.html;

	server_name domain1.com www.domain1.com;

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


	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
	
		fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
		
	}


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain1.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.domain1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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


	listen 80;
	listen [::]:80;

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




}

lp.domain2.com

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


	root /var/www/lp.domain2.com/html;

	index index.html index.php index.htm index.nginx-debian.html;

	server_name lp.domain2.com;

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


	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
	
		
		fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
		
	}
}

lp.domain3.com

server {


	root /var/www/lp.domain3.company/html;

	index index.html index.php index.htm index.nginx-debian.html;

	server_name lp.domain3.company www.lp.domain3.company;

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

	
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
	
		fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
	
	}


}


What am I doing wrong here? I can’t figure it out.


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.

Accepted Answer

I managed to find the issue: The company holding the domains were redirecting from http to https before pointing the DNS to my droplet. the only server block with SSL was my domain1 config file and all https request were pointing to this domain root folder. I added listen 443 ssl; listen [::]:443 ssl;

to the other server blocks

Bobby Iliev
Site Moderator
Site Moderator badge
February 1, 2021

Hi there,

The Nginx configuration looks correct. A possible problem here could be that the user that Nginx is running as, does not have permissions to the directories in question. You could try setting the owner of the files and folders to the Nginx user.

Also, I could suggest checking your error log to verify if this is the case:

  1. sudo 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