Search code examples
node.jsamazon-web-serviceshttpnginxamazon-route53

Why does the subdomain redirect requests back to parent domain?


I have a subdomain sub.example.com that is pointing to a web server hosted on an EC2 instance.

  • In the AWS Route53 console I've created an A-record that points to the public EIP of that instance.
  • I've checked the DNS records with nslookup and they look ok.
  • I can access the subdomain web server from the browser using its public IP address.

But if I try to access using the domain name, the browser redirects the request to the parent domain: http://sub.example.com -> http://example.com. I'm using Nginx as a reverse proxy & NodeJs as a backend server.

What do I need to do to make it work?

Edit
I'm able to access it if I use the www. prefix (www.sub.example.com). But without the "www" the browser just redirects me to the parent domain..

nginx.conf

user nginx;

worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;
        server_name sub.example.com www.sub.example.com;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        # Redirect all HTTP request to the node.js
        location / {
            proxy_redirect off;
            proxy_pass "http://127.0.0.1:5000";
            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;
        }
    }
}

Solution

  • The problem was in the dns resolver cache. It has cached obsolete A-records that point to old IPs. After the dns cache has been updated the problem was gone.

    Thank you @maslick for your replies.