5

I have deployed my Node JS app onto Google Cloud App Engine, and I can successfully add my custom domain for my app.

Let say my custom domain is example.com.

Now I can browse my app via example.com & www.example.com, it works as expected. But I find that I can still browse my app through the default domain. https://[project-id].appspot.com

I want to disable the default domain, is it possible to do that?

kitko112
  • 706
  • 1
  • 7
  • 14
  • 2
    I know you want to disable it, but why? It can actually be pretty helpful to get an appspot subdomain to compare with problems in your custom domain such as SSL and DNS misconfigurations. – Luís Brito Jul 09 '17 at 22:34

3 Answers3

4

You cannot disable that default domain. You would have to write a redirect script. You can test for "appspot" in the request's HTTP_HOST, and test for "AppEngine-Google" in the request's HTTP_USER_AGENT, to determine if it is an internal request.

Also, internal functions, like cron jobs and task queues, run on that domain, so be careful what you do. Task queues will fail given a 301.

GAEfan
  • 11,244
  • 2
  • 17
  • 33
3

After considering GAEfan suggestion, I have made some change on the router logic. If the host name is ended with "appspot.com" and the user agent is included "AppEngine-Google", it will redirect to my custom domain.

router.get('/', function(req, res, next) {
    if(req.get("host").endsWith(GOOGLE_APP_SPOT) && 
        req.get("User-Agent").includes(GOOGLE_USER_AGENT))
    {
        var redirectURL =  url.format({
            protocol: req.protocol,
            host: CUSTOM_DOMAIN,
            pathname: req.originalUrl
        });
        res.redirect(redirectURL);
    }

    res.render('templateName', viewModel);
});
kitko112
  • 706
  • 1
  • 7
  • 14
2

You can do it just using the dispatch.yaml file from App Engine, list all your domains there, but not the *.appspot.com one. Google will show a 404 route when you try to access that.

EDIT: Not possible anymore, check comments.

Checkout the official reference.

Luís Brito
  • 1,652
  • 1
  • 17
  • 34
  • 2
    Unfortunately this is incorrect; creating a `dispatch.yaml` containing only custom domains and not the appspot domain does *not* disable the appspot domain, it continues to dispatch to the default service regardless. I haven't been able to test yet whether using `dispatch.yaml` to route to a redirect or 404 service would have any side-effects. – Haravikk Nov 26 '18 at 12:14