为什么此Nginx服务器重定向到错误的域?

时间:2018-10-01 07:29:02

标签: redirect nginx dns

为什么此Nginx服务器重定向到错误的域?我的Nginx Web服务器有两个服务域,server1.eu和server2.eu,为什么它们相互干扰?当我设置新的“干净”服务器安装时,不会出现此现象,因此此服务器的设置有什么问题。

侦听IPv6的Nginx Web服务器优先于IPv4并干扰SNI。通过删除服务器进行测试可以揭示nginx的行为。

删除除启用了IPv4和IPv6的服务器1之外的所有服务器,重新加载nginx,然后仅使用IPv4侦听器激活服务器2,然后再次重新加载nginx。浏览到服务器2将使您最终进入服务器1。看来nginx会自动侦听第一个添加的IPv6。因此,交换激活顺序将切换路由。

找到/etc/nginx/{conf.d,sites-enabled}给出的

/etc/nginx/sites-enabled/server1.eu 
/etc/nginx/sites-enabled/server2.eu 

区域文件记录:

AAAA    server1.eu    directs to IPv6 address
A   server1.eu    directs to IPv4 address
AAAA    server2.eu  directs to IPv6 address
A server2.eu directs to IPv4 address

nginx服务器配置:

server {
    listen 80;
    listen [::]:80;
server_name server1.eu;
    return 301 https://www.server1.eu;
}
server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        ssl_certificate /etc/letsencrypt/live/server1.eu/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/server1.eu/privkey.pem;
        include snippets/ssl-params.conf;

        server_name www.server1.eu;
        root /var/www/server1.eu/webroot;
        index index.php index.html index.htm ;

        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
}

server {
    listen 80;
    listen [::]:80;
    server_name www.server2.eu;
    return 301 https://$server_name$request_uri;
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/server2.eu/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/server2.eu/privkey.pem;
    include snippets/ssl-params.conf;
    server_name www.server2.eu;
    root /var/www/server2.eu/webroot;
    index index.php index.html index.htm ;

        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
}

2 个答案:

答案 0 :(得分:1)

我已经修改了您的Nginx配置。

这应该有效:

服务器1:

server {
        listen 443 default_server ssl http2;
        listen [::]:443 default_server ssl http2;

        root /var/www/server1.eu/webroot;
        index index.php index.html index.htm;

        server_name www.server1.eu;

        ssl_certificate /etc/letsencrypt/live/server1.eu/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/server1.eu/privkey.pem;

        location / {
                try_files $uri $uri.html $uri/ @extensionless-php;
                autoindex on;
        }

        location @extensionless-php {
            rewrite ^(.*)$ $1.php last;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
}

server {
    listen 80;
    server_name www.server1.eu;
    return 301 https://$host$request_uri;
}

服务器2:

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;

        root /var/www/server2.eu/webroot;
        index index.php index.html index.htm;

        server_name www.server2.eu;

        ssl_certificate /etc/letsencrypt/live/server2.eu/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/server2.eu/privkey.pem;

        location / {
                try_files $uri $uri.html $uri/ @extensionless-php;
                autoindex on;
        }

        location @extensionless-php {
            rewrite ^(.*)$ $1.php last;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
}

server {
    listen 80;
    server_name www.server2.eu;
    return 301 https://$host$request_uri;
}

注意:

更改 default_server 导致server1是您的默认服务器。

答案 1 :(得分:0)

我确实发生了同样的问题。我无法遵循所选解决方案建议的“更改”来解决您的问题,但就我而言,我碰巧通过在domain2 / server2.eu前面(在conf文件中)添加“ www”来解决它对于domain2,在您的情况下为server2.eu)。