nginx – 无效的子域被重定向到另一个域

前端之家收集整理的这篇文章主要介绍了nginx – 无效的子域被重定向到另一个域前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在同一台服务器上托管了两个域.在DNS记录中,我有通配符(*)两个域的记录都指向服务器.

所以我期待xyz.domain1.com解析为domain1.com
和xyz.domain2.com到domain2.com.

但是目前除了domain2.com上的www子域之外的所有内容都将重定向到domain1.com.

我为这两个域提供了相同的Nginx配置,所以我不明白是什么原因引起的.这是我的Nginx配置看起来像 –

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

如何将域保持分离,以便每个域的子域都重定向到正确的域?

最佳答案
您必须为每个域包含通配符server_name条目,以选择要重定向到的域.否则,将将未知的server_names分派到第一个条目(在这种情况下,重定向到domain1.com).

将* .domain2.com添加到server_name www.domain2.com的行,它应该正确地拆分domain2.com子域.

原文链接:https://www.f2er.com/nginx/435188.html

猜你在找的Nginx相关文章