Home > Mobile >  Why does the subdomain redirect requests back to parent domain?
Why does the subdomain redirect requests back to parent domain?

Time:11-21

I have a subdomain sub.example.com that is 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;
        }
    }
}

CodePudding user response:

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.

CodePudding user response:

  • Create two type "A" DNS records in Route53 (xxx.yyy.zzz.aaa is the public IP address of your EC2 instance, e.g. 18.185.121.30):
sub.example.com -> xxx.yyy.zzz.aaa
www.sub.example.com -> xxx.yyy.zzz.aaa
  • Use the standard nginx configuration (do not specify any DNS name in server_name - use the default value instead i.e. server_name _;):
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 _;

        # 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;
        }
    }
}
  • Both the client (browser) and server (nginx) might be caching responses. Use your browser's Incognito mode or curl to test:
curl -I -H "Cache-Control: no-cache" http://sub.example.com
  • Be patient. DNS records require some time (time to live or TTL) to propagate across the globe. You can reduce TTL in Route53 and wait less.

  • To debug DNS issues, use this Linux command:

dig -t a sub.example.com

I also like this web-service which can help you track DNS propagation globally.


UPDATE: here's the example node.js web server I'm running on port 5000:

var http = require('http');

var server = http.createServer(function (req, res) {
  if (req.url == '/') {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write('<html><body><p>This is home Page.</p></body></html>');
    res.end();
  }
});

server.listen(5000);
console.log('Node.js web server at port 5000 is running..')
  • Related