大网数据广告活动
服务器运维问答

Invalid subdomains getting redirected to another domain

· 52CE 我爱测

I have two domains hosted on the same server. In the DNS records I've got wildcard(*) A records for both domains pointed to the server. So I am expecting xyz.domain1.com to resolve to domain1.com and xyz.domain2.com to domain2.co…

问题原文

原帖链接 · charsi · CC BY-SA 3.0

I have two domains hosted on the same server. In the DNS records I've got wildcard(*) A records for both domains pointed to the server.

So I am expecting xyz.domain1.com to resolve to domain1.com and xyz.domain2.com to domain2.com.

However currently everything except the www subdomain on domain2.com is redirecting to domain1.com.

I have got identical nginx config for both domains so I don't understand what could be causing this. This is what my nginx config looks like --

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

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

    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  /dev/stdout  main;
    sendfile        on;
    keepalive_timeout  65;

    # Listen for non-HTTPS requests and redirect them to HTTPS
    server {
        server_name www.domain1.com domain1.com;
        return 301 https://domain1.com$request_uri;
    }

    # Listen for www requests with HTTPS and redirect them to non www site 
    server {
        listen 443 ssl;
        server_name www.domain1.com;
        ssl_certificate     /etc/letsencrypt/live/www.domain1.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.domain1.com/privkey.pem;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        return 301 https://domain1.com$request_uri;
    }

    # Listen for non-www HTTPS requests and serve the app
    server {
        listen              443 ssl;
        #add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        server_name         domain1.com api.domain1.com;
        ssl_certificate     /etc/letsencrypt/live/www.domain1.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.domain1.com/privkey.pem;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;

        location ^~ /.well-known/ {
            root   /usr/share/nginx/html;
            allow all;
        }

        location / {
            root /var/www/domain1;
        }
    }




    # Listen for non-HTTPS requests and redirect them to HTTPS
    server {
        server_name www.domain2.com domain2.com;
        return 301 https://domain2.com$request_uri;
    }

    # Listen for www requests with HTTPS and redirect them to non www site
    server {
        listen 443 ssl;
        server_name www.domain2.com;
        ssl_certificate     /etc/letsencrypt/live/www.domain2.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.domain2.com/privkey.pem;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        return 301 https://domain2.com$request_uri;
    }


    # Listen for non-www HTTPS requests and serve the app
    server {
        listen              443 ssl;
        #add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        server_name         domain2.com;
        ssl_certificate     /etc/letsencrypt/live/www.domain2.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.domain2.com/privkey.pem;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;

        location ^~ /.well-known/ {
            root   /usr/share/nginx/html;
            allow all;
        }

        location / {
            root /var/www/domain2;
        }
    }
}

How can I keep the domains separate so that the subdomains for each get redirected to the correct domain?

已采纳解答

原帖链接 · Andrew Domaszek · CC BY-SA 3.0

You must include a wildcard server_name entry for each domain to select which domain to redirect to. Otherwise unknown server_names will be dispatched to the first entry (in this case, a redirect to domain1.com).

Add *.domain2.com to the line with server_name www.domain2.com and it should split off the domain2.com subdomains properly.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-09-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。