【发布时间】: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>%
【问题讨论】: