Nginx: ejecutar múltiples aplicaciones web en el mismo servidor usando subdominios

Nginx: ejecutar múltiples aplicaciones web en el mismo servidor usando subdominios

Estoy teniendo un Ubuntu 20.04.1 LTSy estoy corriendo nginx/1.18.0 (Ubuntu).

Básicamente tengo tres archivos de configuración en mi carpeta /etc/nginx/sites-availableya que me gustaría enrutar solicitudes a:

  1. miservidor.com
  2. immos.myserver.com
  3. artículos.myserver.com

Mi myserver.comarchivo de configuración se parece al siguiente:

server {
    server_name myserver.com www.myserver.com;
    root /var/www/main-application/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

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

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }


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


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


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




}

El nginx-config de mi immos.myserver.comse parece al siguiente:

server {
    listen 80;
    server_name immos.myserver.com;
    root /var/www/immos-application/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

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

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

}

Mi configuración de nginx items.myserver.comse parece a la siguiente:

server {
    listen 80;
    server_name items.myserver.com;
    root /var/www/items_application/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

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

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

}

Todos los subdominios y el dominio se enrutan en el DNS a la IP de mi servidor.

Puedo abrir myserver.comy ser dirigido a la página correcta.

PERO al abrir immos.myserver.com, items.myserver.comme dirigen a la aplicación que se está ejecutando myserver.com.

Las tres aplicaciones son aplicaciones de Laravel.

¿Alguna sugerencia de qué estoy haciendo mal?

información relacionada