mnml
By:
mnml

Ghost blog shows on domain instead of subdomain only

March 31, 2016 2.7k views
Ghost Nginx DNS Getting Started Networking

Let's start with the zone file:

mysi.te. 1800 IN NS ns1.digitalocean.com.
mysi.te. 1800 IN NS ns2.digitalocean.com.
mysi.te. 1800 IN NS ns3.digitalocean.com.
mysi.te. 1800 IN A <droplet's IP address here>
txt.mysi.te. 1800 IN A <droplet's IP address here> // this is where the blog should be
www.mysi.te. 1800 IN CNAME mysi.te.

I changed the DNS settings so now any.mysi.te doesn't take you to mysi.te, but gives you an error message and this is fine. When you type txt.mysi.te, you go to the blog, and this is fine too. However, when you go to mysi.te, you will see the blog again, and I basically want to see Nginx's Welcome message there.

But this is not happening. What am I doing wrong?

1 Answer

You need a virtual host to handle that domain name, otherwise Nginx will fall back to the default one, which is either the one you specifically defined as default, or the first one it will have loaded from your configuration.

  • @gparent Thank you. I recreated my A records -- actually, I destroyed two, and created one as follows:

    txt.mysi.te. 1800 IN A <droplet IP>

    But it raises several other questions.

    txt.mysi.te should point to the ghost service on the droplet, while the droplet IP should be translated as mysi.te. But if I create an other A record for the domain, the only thing I can give as a value is the droplet's IP which means that the blog will be displayed when the mysi.te is entered -- something that I don't want.

    How will I resolve this?

    Thank you in advance.

    • If you don't want your blog to be displayed when you use your domain, then don't configure the blog's virtual host with that domain.

      If there is no virtual host configured for a domain, Nginx will fall back to the default virtual host, which is either the one defined as such on the listen directive or the first virtualhost loaded by Nginx.

  • Ok, took it a bit further.

    I realized that I will need to add an other A record to @ which I did:
    mysi.te. 1800 IN A <droplet IP>

    I created /etc/nginx/sites-available/ghost.conf, and added:

    server {

    listen 80;
    server_name blog.mysi.te;

    location / {
        proxy_set_header   HOST      $http_host;
        proxy_pass         http://127.0.0.1:2368;
        proxy_redirect     off;
    }
    

    }

    Then created the symlink to sites-enabled dir as follows:

    sudo ln -s /etc/nginx/sites-available/ghost.conf /etc/nginx/sites-enabled/ghost.conf

    restarted nginx.

    Still doesn't work.

    • What doesn't work?

      • The blog is now running under its subdomain as it should be (blog.mysi.te). However, the "Welcome to nginx!" message is still not visible when you go to mysi.te or www.mysi.te.

        • Okay, what is visible instead? Remember that I do not know what you know, only what you tell me.

          • Ok, here we go.
            www.myis.te and simply mysi.te will give you an error message: "This site can’t be reached - mysi.te/www.mysi.te’s server DNS address could not be found."

            The blog service runs fine on the subdomain.

            Currently the relevant zone file entries are as follows:

            txt.mysi.te. 1800 IN A <droplet IP> -- this is the blog service that works fine
            www.mysi.te. 1800 IN CNAME mysi.te. -- this would take the users to mysi.te when they type www.mysi.te which is also fine

            However, there is no A record for @ which would also point to the droplet, hence the error message: "DNS address could not be found". But when I do add an other A record for @ to point to the droplet IP, the blog will come up when you visit mysi.te or www.mysi.te -- and this is what I don't want.

            I think that I would have to add an A record so that mysi.te would be translated to droplet's IP. But then the blog would show on the main domain instead of Nginx's welcome message, so I will have to reconfigure Nginx, but I don't know how.

            What I have done to set up Nginx is detailed here: https://www.digitalocean.com/community/questions/ghost-blog-shows-on-domain-instead-of-subdomain-only?comment=45356

            Thanks for helping a noob out.

            PS. I added an A record as follows:
            mysi.te. 1800 IN A <droplet IP>

            mysi.te and www.mysi.te are taking me to the blog, which means that Nginx is not configured correctly.

          • I am also adding the contents of various config files that are related to Nginx.

            File: /etc/nginx/conf.d/default.conf

            #Following section for txt.mysi.te
             server {
                 listen 80;
                 server_name  txt.mysi.te;
            
                 location / {
                         proxy_pass http://127.0.0.1:2368/;
                         proxy_set_header Host $host;
                         proxy_buffering off;
            
                 }
             }
            

            File: /etc/nginx/sites-available/ghost

            server {
                listen 80 default_server;
                listen [::]:80 default_server ipv6only=on;
            
                server_name txt.mysi.te; # Replace with your domain
            
                root /usr/share/nginx/html;
                index index.html index.htm;
            
                client_max_body_size 10G;
            
                location / {
                    proxy_pass http://localhost:2368;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header Host $http_host;
                    proxy_set_header X-Forwarded-Proto $scheme;
                    proxy_buffering off;
                }
            }
            
            

            File: /etc/nginx/sites-available/ghost.conf

            server {
                listen 80;
                server_name txt.mysi.te;
            
                location / {
                    proxy_set_header   HOST      $http_host;
                    proxy_pass         http://127.0.0.1:2368;
                    proxy_redirect     off;
                }
            }
            

            The latter two are symlinked into the /etc/nginx/site-enabled directory as well.

            The situation now:

            txt.mysi.te -- Ghost blog service works fine here
            mysi.te -- the same ghost blog runs here as well, and i dont want it here.
            www.mysi.te -- the same ghost blog runs here as well, and i dont want it here.

            Thanks,

          • I can't work with redacted configurations. You'll need to use the real domains from now on, or I can't help.

          • Ok, tell me what you need, and I will show it all.

          • I need the unredacted configurations and domain names.

          • Unredacted zone file:

            $ORIGIN mnml.ws.
            $TTL 1800
            mnml.ws. IN SOA ns1.digitalocean.com. hostmaster.mnml.ws. 1459497466 10800 3600 604800 1800
            mnml.ws. 1800 IN NS ns1.digitalocean.com.
            mnml.ws. 1800 IN NS ns2.digitalocean.com.
            mnml.ws. 1800 IN NS ns3.digitalocean.com.
            mnml.ws. 1800 IN MX 10 in1-smtp.messagingengine.com.
            mnml.ws. 1800 IN MX 20 in2-smtp.messagingengine.com.
            mesmtp._domainkey.mnml.ws.mnml.ws. 1800 IN TXT v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDeLhRtOtvmGpoLeHk0TZ93z2P7V/1I9Cx6skM8QNHOeSHK1qe41k9TyalDYNiC7wKAhvd4W8VmQR3/hP2qnyHgyfzqQBLCrst8NnoN7PEJHYR50Wif4Px7AsvjpOkECjV9AZ+8m4rp8Ks23nFd9u88ETIIHM4rLtRxM+FKs7tQTQIDAQAB
            mnml.ws.mnml.ws. 1800 IN TXT v=spf1 include:spf.messagingengine.com ?all
            txt.mnml.ws. 1800 IN A 188.166.170.232
            www.mnml.ws. 1800 IN CNAME mnml.ws.
            mnml.ws. 1800 IN A 188.166.170.232
            
          • File: /etc/nginx/conf.d/default.conf

             #Following section for txt.mnml.ws
             server {
                 listen 80;
                 server_name  txt.mnml.ws;
            
                 location / {
                         proxy_pass http://127.0.0.1:2368/;
                         proxy_set_header Host $host;
                         proxy_buffering off;
            
                 }
             }
            

            File: /etc/nginx/sites-available/default

            # You may add here your
            # server {
            #       ...
            # }
            # statements for each of your virtual hosts to this file
            
            ##
            # You should look at the following URL's in order to grasp a solid understanding
            # of Nginx configuration files in order to fully unleash the power of Nginx.
            # http://wiki.nginx.org/Pitfalls
            # http://wiki.nginx.org/QuickStart
            # http://wiki.nginx.org/Configuration
            #
            # Generally, you will want to move this file somewhere, and start with a clean
            # file but keep this around for reference. Or just disable in sites-enabled.
            #
            # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
            ##
            
            server {
                    listen 80 default_server;
                    listen [::]:80 default_server ipv6only=on;
            
                    root /usr/share/nginx/html;
                    index index.html index.htm;
            
                    # Make site accessible from http://localhost/
                    server_name localhost;
            
                    location / {
                            # First attempt to serve request as file, then
                            # as directory, then fall back to displaying a 404.
                            try_files $uri $uri/ =404;
                            # Uncomment to enable naxsi on this location
                            # include /etc/nginx/naxsi.rules
                    }
            
                    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
                    #location /RequestDenied {
                    #       proxy_pass http://127.0.0.1:8080;
                    #}
            
                    #error_page 404 /404.html;
            
                    # redirect server error pages to the static page /50x.html
                    #
                    #error_page 500 502 503 504 /50x.html;
                    #location = /50x.html {
                    #       root /usr/share/nginx/html;
                    #}
            
                    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
                    #
                    #location ~ \.php$ {
                    #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                    #
                    #       # With php5-cgi alone:
                    #       fastcgi_pass 127.0.0.1:9000;
                    #       # With php5-fpm:
                    #       fastcgi_pass unix:/var/run/php5-fpm.sock;
                    #       fastcgi_index index.php;
                    #       include fastcgi_params;
                    #}
            
                    # deny access to .htaccess files, if Apache's document root
                    # concurs with nginx's one
                    #
                    #location ~ /\.ht {
                    #       deny all;
                    #}
            }
            
            
            # another virtual host using mix of IP-, name-, and port-based configuration
            #
            #server {
            #       listen 8000;
            #       listen somename:8080;
            #       server_name somename alias another.alias;
            #       root html;
            #       index index.html index.htm;
            #
            #       location / {
            #               try_files $uri $uri/ =404;
            #       }
            #}
            
            
            # HTTPS server
            #
            #server {
            #       listen 443;
            #       server_name localhost;
            #
            #       root html;
            #       index index.html index.htm;
            #
            #       ssl on;
            #       ssl_certificate cert.pem;
            #       ssl_certificate_key cert.key;
            #
            #       ssl_session_timeout 5m;
            #       ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
            #       ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
            #       ssl_prefer_server_ciphers on;
            #
            #       location / {
            #               try_files $uri $uri/ =404;
            #       }
            #}
            
            

            File: /etc/nginx/sites-available/ghost

            server {
                listen 80 default_server;
                listen [::]:80 default_server ipv6only=on;
            
                server_name txt.mnml.ws; # Replace with your domain
            
                root /usr/share/nginx/html;
                index index.html index.htm;
            
                client_max_body_size 10G;
            
                location / {
                    proxy_pass http://localhost:2368;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header Host $http_host;
                    proxy_set_header X-Forwarded-Proto $scheme;
                    proxy_buffering off;
                }
            }
            
            

            File: /etc/nginx/sites-available/ghost.conf

            server {
                listen 80;
                server_name txt.mnml.ws;
            
                location / {
                    proxy_set_header   HOST      $http_host;
                    proxy_pass         http://127.0.0.1:2368;
                    proxy_redirect     off;
                }
            }
            
          • Those virtual hosts are a mess. I would suggest deleting it all and starting from scratch. For each domain you try, you'll reach your blog because not only is it the default site, it's there more than once and it's the only domain configured.

            Read the documentation I gave you very carefully, so you understand what the server_name directive does.

            Then, create one virtual host for each site that you want. Then put the correct domains in server_name so that those domains reach the sites you want.

            So presumably that would be two or three virtual hosts: one for the blog, one for your sites that you want to display "Welcome to Nginx!" for, and one for sites that you want to return an error instead. If you always want to display "Welcome to Nginx!" for the non-blog sites rather than return an error, then you'll only need two.

          • OK, started the DNS settings from scratch:

            $ORIGIN mnml.ws.
            $TTL 1800
            mnml.ws. IN SOA ns1.digitalocean.com. hostmaster.mnml.ws. 1459525448 10800 3600 604800 1800
            mnml.ws. 1800 IN MX 10 in1-smtp.messagingengine.com.
            mnml.ws. 1800 IN MX 20 in2-smtp.messagingengine.com.
            mnml.ws. 1800 IN A 188.166.170.232
            

            I will wait for the DNS service to refresh, then test what I see when visiting mnml.ws. I think it would be the blog, but I'm not too sure.

            Let's see how it fares, will post soon.

          • Hi,

            Zone file as follows:

            $ORIGIN mnml.ws.
            $TTL 1800
            mnml.ws. IN SOA ns1.digitalocean.com. hostmaster.mnml.ws. 1459525448 10800 3600 604800 1800
            mnml.ws. 1800 IN MX 10 in1-smtp.messagingengine.com.
            mnml.ws. 1800 IN MX 20 in2-smtp.messagingengine.com.
            mnml.ws. 1800 IN A 188.166.170.232
            
            

            mnml.ws -- displays the blog.
            188.166.170.232 -- also displays the blog
            www.mnml.ws -- cannot be found for obvious reasons
            txt.mnml.ws -- cannot be found for obvious reasons

            Now, could we focus on the issue, namely, that the blog is displayed instead of Nginx's Welcome message? I assume I should be setting something in the Nginx conf files, but I wouldn't know what.

          • "Now, could we focus on the issue, namely, that the blog is displayed instead of Nginx's Welcome message?"

            I'm sorry? I've already solved your issue with my first answer. Since then I've only been rewriting my answer differently until you understand more of it.

            I'm willing to do this for as long as it takes, but I don't appreciate the assumption that I'm not focused on the "issue".

            At the moment, you claim to have one virtual host configured, the one for mnml.ws. As explained previously, this means that all connections to your website will reach that virtual host, because it is the only one and thus it is the default.

            If you want connections to other domain names than mnml.ws to reach their own virtual host, for instance to display the "Welcome to Nginx" page, then you need to create a virtual host for those domains, and make it do so. You would make it similar to the file in /etc/nginx/sites-available/default. For example, once you re-enable your ghost virtualhost for txt.mnml.ws, then that's the website that will be displayed when you connect to that address. Do that for every domain you want things to happen, and use the default virtual host for the rest.

            You should read this, as it explains what I've said in my replies above regarding the default_server directive and how to use it:

            http://nginx.org/en/docs/http/ngxhttpcore_module.html#listen

            You can also refer once more to the server_name documentation to complete this.

            Also, I'm assuming that you understand the difference between /etc/nginx/sites-enabled, /etc/nginx/sites-available, and /etc/nginx/conf.d. You want to make sure that your virtual hosts are enabled.

            EDIT: Also, don't forget to re-add the domains.

Have another answer? Share your knowledge.