Nginx – Subdomain loading main root directory instead of subdomain root directory

nginxsubdomain

I have a website and am trying to create a subdomain. However, the subdomain is loading the root directory of example.com, instead of loading the root directory of sub.example.com. Here are my Nginx configuration files:

Subdomain configuration file:

server {
    listen 80;
    listen [::]:80;
    server_name sub.example.com;
}

server {

    # SSL configuration

    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332   #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/sub/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri.html $uri.php $uri/ =404;
    }

    location ~ /.well-known {
            allow all;
    }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
    #       fastcgi_pass 127.0.0.1:9000;
    }

    location @extensionless-php {
            rewrite ^(.*)$ $1.php last;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
            deny all;
    }
}

Main domain configuration file:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

server {

    # SSL configuration

    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

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

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

    location ~ /\.ht {
        deny all;
    }
    location ~ /.well-known {
        allow all;
    }
    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
    location ~*  \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2)$ {
        expires 365d;
    }
    location ~*  \.(pdf)$ {
        expires 30d;
    }
    #  Ensure requests for pagespeed optimized resources go to the pagespeed
    #  handler and no extraneous headers get set.
    location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
    location ~ "^/ngx_pagespeed_static/" { }
    location ~ "^/ngx_pagespeed_beacon" { }
}

I know that it is not getting the correct root domain because if I visit, for example, example.com/testpage.html, I can type sub.example.com/testpage.html and it loads the exact same thing, for any page I try.

Also, visiting sub.example.com redirects to example.com.

Any help would be appreciated! Thanks!

Best Answer

So, as @Richard Smith and @EEAA said, it is simply a case of not having separate server blocks.

To fix the error, I:

  • Combined the two server blocks in each file
  • Removed the line 'return 301 https://$server_name$request_uri;' because it was causing a redirect loop.

Thanks, @Richard Smith and @EEAA!