我正在尝试在我的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
我正在尝试的所有操作都是将所有URL重定向到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;
}
}
这是当前正在发生的事情:
以上情况当前发生的是:
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
答案 0 :(得分:0)
www.example.com
和app.example.com
使用相同的根目录?如果是,请参见下面的示例。
对于example.com
,如果您需要重定向到www子域:
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
对于www.example.com
和app.example.com
,而无需重定向并从/var/www/example/public
输出数据:
server {
listen 80;
server_name www.example.com app.example.com;
root /var/www/example/public;
index index.php index.html;
}