Snipo
By:
Snipo

Subdomain server block doesn't work

August 4, 2016 589 views
Nginx DNS Ubuntu 16.04

Hello, I'm trying to create a subdomain on my server but it's not working. I have main domain where i host a wordpress also with Let's Encrypt certificate, everything through tutorials on this site. My DNS is handled by CloudFlare.

So first i turned off CloudFlare services, now it handles only DNS. I also created a new A DNS record which points to the server IP.

Next i created a link of default server block in sites-enabled folder just to make sure everything is working and it is, when i enter IP of my server in browser, nginx welcome page appears. ( but I had to do it in another browser because chrome always translate ip to main domain name )

In the default server block, I changed server name to my subdomain ( something.something.com ) and when I restarted the nginx service a go to my subdomain it didn't work, it redirected me to the main domain. So I was trying to solve this problem, I also removed my main domain from enabled sites and it still didn't work.

But when I change the server name in default server block from subdomain to my main domain (main domain server block is disabled), it works (shows nginx welcome page).

What can be a problem?

3 comments
  • Next weird thing for me... If I restore everything, and let only DNS record for subdomain, it shows me the content of the main domain. I don't have any default server, it's OK?

  • Can you share the two server blocks you have configured?

    You can also rule out a DNS issue by setting a temporary hosts file entry for your new subdomain. This will allow you to make sure that nginx is handling the name correctly. If things work properly with the hosts file entry I would double-check your DNS settings.

  • Main domain

    server {
            listen 80;
            listen [::]:80;
            server_name maindomain.com www.maindomain.com;
            return 301 https://$server_name$request_uri;
    }
    
    server {
            client_max_body_size 5M;
    
            listen 443 ssl;
            listen [::]:443;
            include snippets/ssl-maindomain.com.conf;
            include snippets/ssl-params.conf;
    
            root /var/www/maindomain.com/public;
    
            index index.html index.htm index.nginx-debian.html index.php;
    
            server_name maindomain.com www.maindomain.com;
    
            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;
            }
    }
    
    

    Subdomain

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        root /var/www/html;
    
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
    
        server_name sub.maindomain.com;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    
        #   # With php7.0-cgi alone:
        #   fastcgi_pass 127.0.0.1:9000;
    
            # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny all;
        }
    
        location ~ /.well-known {
                    allow all;
            }
    }
    
1 Answer

Ok, I decided to get rid of CloudFlare and everything works now.

Have another answer? Share your knowledge.