【问题标题】:Nginx server removes www and 301 redirects to wrong hostNginx 服务器删除 www 和 301 重定向到错误的主机
【发布时间】:2016-04-17 01:25:50
【问题描述】:

我遇到了一个奇怪的 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 目录中有三个 nginx 配置。它们在此处列出:

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

如果我 CURL 到 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>%

【问题讨论】:

    标签: redirect nginx


    【解决方案1】:

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

    server {
      server_name   ~^(www\.)?(?<domain>.+)$;
    
      location / {
        return 301 $scheme://$domain/$uri;
      }
    }
    

    【讨论】:

    • 谢谢。我已尝试附加服务器块,但问题仍然存在。我也尝试用www.maindomain.com 中的块替换服务器块,但这也不起作用。奇怪的问题是,即使我删除了www.maindomain.com,我仍然会从 www 重定向 301。没有的。这让我觉得其他地方的一些配置会导致这种重定向。
    • 更新:我会尝试添加它,然后再将配置放入 catch-all 中,但现在我收到了 301 太多重定向。好像它 301 重定向有和没有 www。在服务器名称中。 @elDante
    • 我终于让它工作了。谢谢!您的答案已编辑
    猜你喜欢
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2018-04-11
    • 2011-04-01
    • 2014-08-28
    • 2017-01-11
    • 1970-01-01
    • 2012-06-17
    相关资源
    最近更新 更多