[英]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
我正在尝试的一切都是将所有网址重定向到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
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
的 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.