Question

nginx multiple domains on one droplet configuration

Posted February 1, 2021 959 views
NginxUbuntu 18.04

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.

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.

×
Submit an Answer
2 answers

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

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:

  • sudo tail -100 /var/log/nginx/error.log

Let me know how it goes!
Regards,
Bobby

  • Thanks for the response!

    The sudo tail return no lines.

    These are the permissions on the var/www/ folders

    drwxr-xr-x 3 root root 4096 Jan 29 13:16 domain1.com
    drwxr-sr-x 11 noip www-data 4096 Jan 29 15:39 html
    drwxr-xr-x 3 root root 4096 Jan 19 16:07 lp.domain3.company
    drwxr-xr-x 3 root root 4096 Jan 19 16:07 lp.domain2.com

    • Hi there,

      I would recommend setting the owner and the group of all folders to match the ones that you have for the /var/www/html folder. You can use the chown command to change the ownership.

      Let me know how it goes.

      Regards,
      Bobby

      • I tried but still no success.
        I did a
        sudo tail -10 /var/www/nginx/access.log
        and I get a 304 redirect when accessing one of the 2 domains that are not pointing to the right folder.

        [01/Feb/2021:14:11:11 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like....
        
        • I did more investigation and I see that the subdomains (lp.domain2.com, lp.domain3.company) are resolving to the IPs of the domain2.com and domain3.company not to the IP of my droplet.
          The 2 domains are hosted on a different server and I requested them to change the DNS records only for the subdomains (as in lp.).
          Should this be the issue?