2

My website's IP address shows up when searching on google. Google knows the site exists, if I search for my website. Bing doesn't though, if I search for my website on bing.

my nginx config file:

server {
  listen 80;
  server_name  _;
  return 301 https://$host$request_uri;
}


server {
  listen       443 ssl;
  server_name  localhost;

  ssl_certificate  /etc/letsencrypt/live/mosachi.ga/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/mosachi.ga/privkey.pem;

  location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}
8
  • I'm not an nginx expert, but why is the server_name "localhost"? Surely it should be mosachi.ga?
    – davidgo
    Commented Dec 9, 2020 at 18:15
  • 1
    @davidgo if there are no other matching server_name fields, then it'll default to the first-read config - if this is the only site config listening on https:// on port 443, then it'll default to that. Commented Dec 9, 2020 at 19:39
  • .... which is probably the root of the problem, the server/website is accepting all requests that reach it (including the IP address) and there is no attempt to canonicalise the requested hostname. However, a further issue is that Google has now indexed HTTPS + IP-Address (for which the SSL cert is not valid), so browser warnings are unfortunately unavoidable.
    – MrWhite
    Commented Dec 9, 2020 at 20:35
  • what should i do? I don't really know what my config file does, I just copy pasted it from somewhere
    – i'm a girl
    Commented Dec 9, 2020 at 20:44
  • So the solution is to do a 301 redirect for any site that is not www.mosachi.ga - Google will pick up on this and update its links.
    – davidgo
    Commented Dec 9, 2020 at 21:04

1 Answer 1

4

So, you have a case of an "NGINX Has Only One Site Configured" problem. That is, NGINX is serving the same data not just for your domain but for the bare IP address and any other request to your server/site (even with invalid domains - domains/subdomains which are NOT hosted on your site).

Thankfully, this is something you can fix.

FIRST we need to have a 'catch-all' to just drop non-valid domains and direct-to-IP requests. We'll redirect them all to your proper domain.

server {
    listen 80 default_server;

    server_name _;

    return 301 https://mosachi.ga$request_uri;
}

SECOND we need your config for your domain, but with a few tweaks (you shouldn't have server_name localhost; if you intend to serve an actual domain):

server {
  listen 80;
  server_name  mosachi.ga;
  return 301 https://mosachi.ga$request_uri;
}


server {
  listen       443 ssl;
  server_name  mosachi.ga;

  ssl_certificate  /etc/letsencrypt/live/mosachi.ga/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/mosachi.ga/privkey.pem;

  location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

Combined, your configuration should look like this:

# Catchall configuration - redir to the domain for bare and invalid domain requests
server {
    listen 80 default_server;

    server_name _;

    return 301 https://mosachi.ga$request_uri;
}

# HTTP handler to redirect to HTTPS for mosachi.ga
server {
  listen 80;
  server_name  mosachi.ga;
  return 301 https://mosachi.ga$request_uri;
}

# HTTPS for mosachi.ga
server {
  listen       443 ssl;
  server_name  mosachi.ga;

  ssl_certificate  /etc/letsencrypt/live/mosachi.ga/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/mosachi.ga/privkey.pem;

  location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

It may take some time for Google to play catchup here, but eventually the IP will just disappear from the search data, and the domain itself will show up in the search results instead.

5
  • I would recommend returning a 404 error rather than dropping the connection for unknown host names. The other alternative is redirecting to your actual domain name. Commented Dec 9, 2020 at 21:32
  • @StephenOstermiller that's fixed, however as an IT Security professional, when I see 404s when (under contract) scanning target IPs, when I see 404s I assume that it's something I can hit - you get literally no response when returning a 444 to a request, which security-wise is just me doing an extra chaos mode - but I don't disagree (from SEO perspective both would do the same result I believe) Commented Dec 9, 2020 at 21:40
  • 1
    For SEO it would make sense to redirect the IP address that's currently indexed in Google to your domain name. At least that way visitor is will get where they're supposed to go from Google until Google indexes the content on the domain name. Commented Dec 9, 2020 at 21:45
  • Let us continue this discussion in chat. Commented Dec 10, 2020 at 19:15
  • Thanks to the mods for cleaning up the comments which moved to chat already. @imagirl please refer to the linked chat room to continue the discussion Commented Dec 10, 2020 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.