I have a droplet for hosting three sites, all of them using Kirby CMS, a PHP based, flat-file CMS.

I followed this tutorial to setup Nginx and PHP: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04

I’ve setup a separate nginx config for each site, like so:

var/www/site1.com
var/www/site2.com
var/www/site3.com

Whenever I go to site1.com, it works as expected. When I go to site2.com, it looks like site 1, but the URL in the browser shows site1.com. If I go to site2.com/index.php I see the proper site that I expected.

Here’s what the site1 nginx config looks like:

server {
    server_name site1.com www.site1.com;
    root /var/www/site1.com;

    index index.php index.html index.htm;

    client_max_body_size 100M;

    # Don't hint these as folders
    rewrite ^/(content|site|kirby)$ /error last;

    # block content
    rewrite ^/content/(.*).(txt|md|mdown)$ /error last;

    # block all files in the site and kirby folder from being accessed directly
    rewrite ^/(site|kirby)/(.*)$ /error last;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ /panel {
        autoindex off;
        try_files $uri $uri/ /panel/index.php$uri&$args;
    }

    location ~ (?:^|/)\. {
        deny all;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
     }

    location ~ /\.ht {
        deny all;
    }

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


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


    listen 80;
    server_name site1.com www.site1.com;
    return 404; # managed by Certbot

}

Site 2’s config looks the same except the URLs are changed and it doesn’t have the Let’s Encrypt stuff yet since I can’t seem to get it to confirm ownership of the domains.

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.

×
1 answer

Hi @jasonland,

The config file looks fine. I can think of two things happening here. The first one: are you accessing site2.com via https? If you have no SSL on that website and try to load it, it will show the first website it finds with SSL. What I’m trying to say is, make sure you don’t have an https redirection.

The second thing I can think of is if you have somewhere in your site2.com’s database configured the url for site1. Can you confirm if those are not actually correct as well?

Regards,
KFSys

  • I just thought of another option. Have you created a separate nginx config file for each website? Additionally, have you by any chance added/ not disabled the default nginx configuration file?

    • I have created a separate nginx config for each site, however I haven’t touched the default config. I didn’t add one, but if one exists I haven’t modified it.

      For your parent comment- both sites lack a database, and I haven’t secured site 2 with Let’s Encrypt yet because I think this redirection weirdness is preventing the security challenge from passing when I run certbot.

      • Hi @jasonland,

        In that case, if you don’t have a database, it should be configured somewhere in the files, the website name I mean. Other than that, I can see anything else coming up as a problem.

        This is a long shot but please check just in case your site2.com’s config that it has the following block

        location / {
            try_files $uri $uri/ /index.php?$args;
        }
        

        Regards,
        KFSys

        • I do have a very similar line. Here’s my whole config for site2

          server {
              listen 80;
              server_name site2.com www.site2.com;
              root /var/www/site2.com;
          
              index index.php index.html index.htm;
          
              client_max_body_size 50M;
          
              # Don't hint these as folders
              rewrite ^/(content|site|kirby)$ /error last;
          
              # block content
              rewrite ^/content/(.*).(txt|md|mdown)$ /error last;
          
              # block all files in the site and kirby folder from being accessed directly
              rewrite ^/(site|kirby)/(.*)$ /error last;
          
              location / {
                  try_files $uri $uri/ /index.php$is_args$args;
              }
          
              location ~ (?:^|/)\. {
                  deny all;
              }
          
              location ~ \.php$ {
                  include snippets/fastcgi-php.conf;
                  fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                  include fastcgi_params;
               }
          
              location ~ /\.ht {
                  deny all;
              }
          }
          
Submit an Answer