【问题标题】:Why does the subdomain redirect requests back to parent domain?为什么子域将请求重定向回父域?
【发布时间】:2021-12-30 22:47:04
【问题描述】:

我有一个子域 sub.example.com,它指向托管在 EC2 实例上的 Web 服务器。

  • 在 AWS Route53 控制台中,我创建了一个指向该实例的公共 EIP 的 A 记录。
  • 我检查了nslookup 的 DNS 记录,它们看起来没问题。
  • 我可以使用其公共 IP 地址从浏览器访问子域 Web 服务器。

但是如果我尝试使用域名访问,浏览器会将请求重定向到父域:http://sub.example.com -> http://example.com。我使用 Nginx 作为反向代理和 NodeJs 作为后端服务器。

我需要做什么才能让它工作?

编辑
如果我使用 www,我可以访问它。前缀 (www.sub.example.com)。但如果没有“www”,浏览器只会将我重定向到父域..

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;
        }
    }
}

【问题讨论】:

  • 您的预期输出是什么?您只想路由 sub.x.com 而不是 x.com?还是两者都路由到 ECC2 实例?
  • @Ermiya Eskandary 我希望可以通过“www.sub.x.com”或“sub.x.com”访问该子域。但如果我不使用“www”部分,它会将我重定向到父域。
  • 听起来您只有一个指向 EC2 实例的 www.sub.example.com 的 DNS 记录。您还需要为 sub.exammple.com 添加一条指向 EC2 实例的 DNS A 记录。
  • @MarkB 我有“sub.x.com”的 A 记录 > “www.sub.x.com” > “sub.x.com”的 ip 和别名记录。跨度>
  • 你能提供你的nginx配置吗?

标签: node.js amazon-web-services http nginx amazon-route53


【解决方案1】:
  • 在 Route53 中创建两条“A”类 DNS 记录(xxx.yyy.zzz.aaa 是您的 EC2 实例的公共 IP 地址,例如 18.185.121.30):
sub.example.com -> xxx.yyy.zzz.aaa
www.sub.example.com -> xxx.yyy.zzz.aaa
  • 使用标准 nginx 配置(不要在 server_name 中指定任何 DNS 名称 - 使用默认值,即 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;
        }
    }
}
  • 客户端(浏览器)和服务器(nginx)都可能缓存来自旧配置的响应。使用浏览器的隐身模式或带有no-cache 标头的curl 进行测试:
curl -I -H "Cache-Control: no-cache" http://sub.example.com
  • 要有耐心。 DNS 记录需要一些时间(生存时间或 TTL)才能在全球传播。你可以减少Route53中的TTL,减少等待。

  • 要调试 DNS 问题,请使用以下 Linux 命令:

dig -t a sub.example.com

我也喜欢 this 网络服务,它可以帮助您在全球范围内跟踪 DNS 传播。


更新:这是我在端口 5000 上运行的示例 node.js Web 服务器:

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..')

【讨论】:

  • 感谢您的回复。我已经有了那些 Route53 A 记录。我已经停止了 nginx 服务。即使 nginx 服务停止,curl 也会显示 301 重定向到 example.com.. 我只是不知道此时该怎么想.. 可能是一些 AWS 错误..
  • 用上面的配置改变/etc/nginx/ngninx.conf。重启nginx服务sudo systemctl restart nginx.service再试一次。我已经在干净的系统上尝试了上述解决方案。并且两个 DNS 名称都解​​析到同一个页面,没有任何重定向。您还可以尝试减少 DNS 记录上的 TTL。请记住,您的 DNS 更改需要一些时间来传播,具体取决于您发出请求的位置(地理上)。清理浏览器缓存也可能会有所帮助。
  • Nginx 可能正在缓存您的请求。所以试试这个命令告诉 nginx 不要缓存:curl -I -H "Cache-Control: no-cache" http://sub.example.com
  • 'curl --trace-ascii - sub.example.com' 显示不正确的 IP。也许 dns 解析器缓存了旧的 dns 记录。
  • 是的,等待和/或减少 TTL
【解决方案2】:

问题出在 dns 解析器缓存中。它缓存了指向旧 IP 的过时 A 记录。更新 dns 缓存后,问题就消失了。

感谢@maslick 的回复。

【讨论】:

  • 请接受我的回答作为对努力的奖励;) thnx
  • 我对您的问题做了一些小的修改。请检查它们是否正常。
  • 也欢迎对答案进行投票 ? thnx
猜你喜欢
  • 2010-10-31
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
相关资源
最近更新 更多