繁体   English   中英

nginx 使用子域和裸域重定向

[英]nginx redirect with a subdomain and naked domain

我试图在我的 nginx 配置中找出我的域的重定向。 我有这三个域名:

domain.com //points to server 1
www.domain.com //points to hubspot server 2, no a record to change naked domain
app.domain.com //points to server 1
  • 我需要 domain.com 重定向到www.domain.com
  • 我需要 app.domain.com 保持未重定向

我正在尝试的一切都是将所有网址重定向到www.domain.com

我究竟做错了什么?

一次尝试,重定向所有内容:

server {
    listen 80;
    server_name domain.com;
    return 301 www.domain.com;
}

server {
    root var/www/domain/public;
    index index.php index.html;
    server_name app.domain.com;
}

另一次尝试,重定向一切:

server {
    listen 80;
    root var/www/domain/public;
    index index.php index.html;
    server_name domain.com app.domain.com;
    return 301 www.domain.com
}

这完全破坏了我的配置:

server {
    listen 80;
    root var/www/domain/public;
    index index.php index.html;
    server_name domain.com app.domain.com;
  
    location domain.com {
      return 301 www.domain.com;
    }
}

更新:

我已经更新了我的配置,这样说:

server {
        server_name     domain.com;
        return          301 $scheme://www.domain.com$request_uri;

}

server {
    root /var/www/domain/public;
    index index.php index.html;
    server_name app.domain.com;


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

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

这是目前正在发生的事情:

  • domain.com 和 app.domain.com 都指向我们的谷歌云服务器。 我希望 app.domain.com 继续指向这里,并且对于 domain.com 到 301 重定向到 hubspot
  • www.domain.com指向我们的 hubspot 服务器

目前发生的情况是:

  • www.domain.com去正确的地方(hubspot)
  • app.domain.com 去正确的地方(谷歌云)
  • domain.com 仍然去错地方(谷歌云),但使用curl -I -L domain.com说它是 301 重定向到正确的位置。 我很混乱。

我的一些朋友为我测试地址说他们被正确的 301 重定向,但有些不是(包括我自己)

这是 curl 命令的打印输出:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3 (Ubuntu)
Date: Sun, 09 Aug 2020 22:33:49 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: ht tp://www.domain.com/

HTTP/1.1 200 OK
Date: Sun, 09 Aug 2020 22:33:49 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 54566
Connection: keep-alive

www.example.comapp.example.com使用同一个根目录? 如果是,请参见下面的示例。

例如example.com如果您需要重定向到 www 子域:

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

对于www.example.comapp.example.com没有重定向和来自/var/www/example/public的 output 数据:

server {
    listen 80;
    server_name www.example.com app.example.com;

    root /var/www/example/public;
    index index.php index.html;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM