Question

setup 2 subdomains, lt.cce.acedemy to launch Leantime, nc.cce.academy to launch NextCloud application

I have the /etc/nginx/sites-available config for leantime setup as below, basically I merely copy the master copy of the config from the Leantime support site and change the server name to be the subdomain lt.cce.academy and document root to be /var/www/cce/leantime/public:

root@ /etc/nginx/sites-available 15:02$ cat lt.cce.academy
server {

  listen 80;
  listen [::]:80;
  server_name lt.cce.academy;
  return 301 https://lt.cce.academy$request_uri;
}

server {

  listen 443 ssl http2;
  listen [::]:443 ssl http2;

  server_name lt.cce.academy;
  set $base /var/www/cce/leantime;
  root $base/public;

  ssl_certificate /etc/ssl/cce.academy/cce.academy.ca-bundle;
  ssl_certificate_key /etc/ssl/cce.academy/cce.academy.key;

  # logging
  access_log /var/log/nginx/lt_access;
  error_log /var/log/nginx/lt_error warn;

  # index.php
  index index.php;

  location ~.php$ {

    # 404
    try_files $fastcgi_script_name =404;

    # default fastcgi_params
    include fastcgi_params;

    # fastcgi settings
    fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    fastcgi_index index.php;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;

    # fastcgi params
    fastcgi_param DOCUMENT_ROOT $realpath_root;
    fastcgi_param SCRIPT_FILENAME       $realpath_root$fastcgi_script_name;
    fastcgi_param PHP_ADMIN_VALUE       "open_basedir=$base/:/usr/lib/php/:/tmp/";

  }

  location / {

    rewrite ^/?$ /index.php?act=dashboard.show;
    rewrite ^/([^/\.]+)/?$ /index.php?act=$1;
    rewrite ^/([^/\.]+)/([^/\.]+)/?$ /index.php?act=$1.$2;
    rewrite ^/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?act=$1.$2&id=$3;
  }


  # additional config
  # favicon.ico
  location = /favicon.ico {

    log_not_found off;
    access_log off;
  }

  # robots.txt
  location = /robots.txt {

    log_not_found off;
    access_log off;
  }

  # assets, media
  location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {

    expires 7d;
    access_log off;
  }

  # svg, fonts
  location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {

    add_header Access-Control-Allow-Origin "*";
    expires 7d;
    access_log off;
  }

  # gzip
  gzip on;
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

}

And in another subdomain nc.cce.academy, I will be launching NextCloud application.

The /etc/nginx/site-available config for nextcloud is as follows :

root@ /etc/nginx/sites-available 15:07$ cat nc.cce.academy
server {
    listen 80;

    server_name nc.cce.academy;
    root /var/www/cce/nextcloud;
    index index.php;

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

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
server {
    listen 80;

    server_name nc.cce.academy;
    root /var/www/cce/nextcloud;
    index index.php;

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

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

cat /etc/hosts
127.0.0.1 localhost
my-IP-address Debian-bullseye-latest-amd64-base
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

root@ /etc/nginx/sites-enabled 15:15$ ls -la
total 8
drwxr-xr-x 2 root root 4096 May 22 15:15 .
drwxr-xr-x 8 root root 4096 May 19 16:08 ..
lrwxrwxrwx 1 root root   41 May 18 01:13 lt.cce.academy -> /etc/nginx/sites-available/lt.cce.academy
lrwxrwxrwx 1 root root   41 May 18 12:55 nc.cce.academy -> /etc/nginx/sites-available/nc.cce.academy

So I have 2 virtual hosts enabled.


syntax check on the configuration shows 1 warning, which I am not sure how to resolve it.

 nginx -t
nginx: [warn] conflicting server name "nc.cce.academy" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

when at my browser, I type in “lt.cce.academy” subdomain to launch leantime application, it works normally.

However, when I type in “cce.academy” domain name in browser search bar, it also launch leantime application.

Lastly, I type in “nc.cce.academy”, it again launch leantime application.

As I merely copy the master copy of leantime config to lt.cce.academy without understanding the directives, not sure how to edit the lt.cce.academy config?

such that when I type in “nc.cce.academy” should launch “nextcloud” application instead

and when I type in “cce.academy”, it should open up “index.html” at /var/www/cce/ directory

Thank you.


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.

Bobby Iliev
Site Moderator
Site Moderator badge
May 22, 2023

Hi there,

It sounds like there might be a problem with the default Nginx server block. This is because when you enter “cce.academy” in the browser, Nginx needs to know which server block to use. If you don’t have a specific server block defined for “cce.academy”, it will use the default server block. If you haven’t explicitly defined a default server, Nginx will use the first server block that it finds, which in your case appears to be the one for “lt.cce.academy”.

Let’s solve your problems in a step by step manner:

  1. To solve the issue with “nc.cce.academy”, the first thing to check is that there are no duplicates of the server_name directive in any other configuration files. As far as I can see from the output that you’ve shared and the error suggests, the server name “nc.cce.academy” seems to be used in two places. Please verify this and remove the duplicate server block.

  2. To handle requests to “cce.academy”, you need to create a new server block for it. If you want it to serve a static HTML file, your new configuration would look like this:

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

    server_name cce.academy www.cce.academy;

    root /var/www/cce/;
    index index.html;

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

Note: Update this to reflect your application-specific details.

The default_server option will tell Nginx to use this server block when the Host header of the HTTP request doesn’t match any other server block.

  1. Restart or reload Nginx after making the above changes.

Please make sure to replace “cce.academy” and “/var/www/cce/” with your actual domain and directory.

If you have SSL certificates for the domain “cce.academy”, you should add another server block for handling HTTPS (port 443) requests, similar to how you’ve done for “lt.cce.academy”. You will also need to configure a permanent redirection from HTTP to HTTPS for better security.

To learn more about the Nginx server blocks and how they work, I would recommend the following tutorial:

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

Let me know if you have any questions!

Best,

Bobby

Try DigitalOcean for free

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

Sign up

Featured on Community

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