【发布时间】:2020-11-28 01:44:44
【问题描述】:
我试图在我的 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 去正确的地方(中心点)
- 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
【问题讨论】:
标签: nginx redirect ubuntu-16.04