0

I'm running into a big problem when trying to configure a domain name through Google Domains to point to my EC2 (Ubuntu Server) instance.

I set up nginx as a reverse proxy to serve a Django app. I also configured and associated an elastic IP with the EC2 instance. When I visit the elastic IP (34.XXX.XXX.28), I get to the app just fine. This tells me that everything is configured properly within the EC2 instance (nginx, Django app, etc...).

The problem is when I visit my domain name at www.mydomain.com, I get this page:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com.

Thank you for using nginx.

I'm confused why directly visiting the elastic IP works, but visiting through the domain name (www.mydomain.com) would lead to the default nginx page. Could someone please point me in the right direction?

Here is some context:

upstream djangoapp {
    server unix:/var/www/run/djangoapp.sock;  
}

server {

    listen 80;
    server_name 34.XXX.XXX.28;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://djangoapp ;   
    }

    location /static/ {
        alias /var/www/djangoapp/static/;       
        autoindex on;
    }   
}
  • You've configured Nginx incorrectly. Please edit your question to include your nginx.conf and any/all site configurations. You can edit anything you need to, but leaving them 100% as configured is best. Of course you should check access/error logs to ensure nothing's in there before you post, and restart Nginx. – Tim May 28 '17 at 0:22
  • I added the nginx config :) – ttmtran May 28 '17 at 0:32
  • Change server_name from 34.XXX.XXX.28 to www.mydomain.com then reload the config (service nginx reload) – Diogenes deLight May 28 '17 at 1:38
1

Nginx can listen for multiple websites on the same IP address, both http and https. Right now you have it listening for requests to a specific IP, not the domain name. The solution is to specify the domain(s) you want it to listen to - for example

server_name example.com www.example.com;

You should typically only listen on one domain. Subdomains should be forwarded to the main domain, though whether www or the root domain is what you consider "main" is entirely up to you.

# Main server
server {
  listen 80;
  server_name example.com;
  // Locations etc
}

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

You can also any given server block the 'default server'. This could be the main server block for your site, or it could be another to prevent your server responding by IP. You might not want to respond to IPs because the internet is continually scanned

server {
  listen      80 default_server;
  server_name 1.2.3.4 example.com;
}

This is a default server that rejects traffic to your IP that isn't handled by a specific server block

server {
  listen      80 default_server;
  server_name _; # Wildcard, any domain is served
  return      444; # This means "go away", effectively
}
| improve this answer | |
  • Wow, thank you for your help! This was such a simple problem. I can't believe I didn't think of this before. I'll read the nginx docs again and maybe this won't happen again in the future. Thanks again! – ttmtran May 28 '17 at 11:45
1

When you are using public IP of your site, it is escaping the DNS. You are viewing 100% correct things, no caching mechanism of DNS is in between. It is mot possible to say without precise details of error log, full config. But Nginx config is wrong like others pointed and obviously Nginx has documentation. Example config given on official docs. You'll see they said about similar format like the other answers, comments here.

Only we can say to :

  1. set the TTL of DNS to very low like 30 seconds during debug
  2. and set server_name www.your-domain-name.com; from nginx.
  3. Then check DNS propagation
  4. access via Tor browser to check. May be you are seeing cached page.

After correcting, run nginx -t config test and then restart nginx to take effect.

| improve this answer | |
0

Nginx is listening on IP address. You need to add your domain name in "server_name" directive. Then reload the nginx and it should work fine.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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