Nginx服务器删除www和301重定向到错误的主机

时间:2016-01-12 12:19:32

标签: redirect nginx

我有一个奇怪的301重定向问题。我的服务器设置为处理指向服务器的多个域,即

但是访问自定义域并包含www.时出现了一个奇怪的301问题,例如:http://www.somecustomdomain.com。在maindomain上这很好用:

  • 访问https://www.maindomain.com/some-uri时,它会重定向到:https://maindomain.com/some-uri

但是,在访问自定义域时,它会从:http://www.somecustomdomain.com/some-uri重定向到https://maindomain.com/some-uri(!!)。您可能希望将其重定向到:http://somecustomdomain.com/some-uri

我已尝试调试此问题(确保我的浏览器不会缓存301重定向)并且我无法解决此问题。我的sites-available目录中有三个nginxs配置。它们列在这里:

  • maindomain.com
  • catch-all(我尝试删除此文件,因此仅存在maindomain.com,但问题仍然存在)
  • www.maindomain.com(我尝试删除此文件,因此仅存在maindomain.com,但问题仍然存在)

maindomain.com内容

server {
    listen 80;
    server_name maindomain.com;
    return 301 https://maindomain.com$request_uri;
}

server {
    listen 443 ssl;
    server_name maindomain.com;
    root /home/forge/maindomain.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/maindomain.com/30126/server.crt;
    ssl_certificate_key /etc/nginx/ssl/maindomain.com/30126/server.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/maindomain.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

catch-all内容

server {
    listen 80;
    server_name ~^(.+)$;

    root /home/forge/maindomain.com/public;

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/maindomain.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

www.maindomain.com内容

server {
    listen 80;
    server_name www.maindomain.com;
    return 301 $scheme://maindomain.com$request_uri;
}

如果我加入http://www.somecustomdomain.com/some-uri这是我收到的内容:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="1;url=https://maindomain.com/some-uri" />

        <title>Redirecting to https://maindomain.com/some-uri</title>
    </head>
    <body>
        Redirecting to <a href="https://maindomain.com/some-uri">https://maindomain.com/some-uri</a>.
    </body>
</html>%

1 个答案:

答案 0 :(得分:2)

您需要为www域额外配置服务器:

server {
  server_name   ~^(www\.)?(?<domain>.+)$;

  location / {
    return 301 $scheme://$domain/$uri;
  }
}