【发布时间】:2017-10-19 09:49:44
【问题描述】:
我是 Nginx 的新手,并试图弄清楚如何正确处理子域。我想要实现的是主域example.com总是重定向到https://www.example.com,但子域sub.example.com应该总是重定向到https://sub.example.com。在我当前的设置中,第一个要求得到满足,但sub.example.com 总是被重定向到https://www.sub.example.com。我的配置有什么问题,我该如何解决?
在此先感谢您,费边。
我的两个服务器配置文件:
默认
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.$host$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /path/on/my/server/to/certificate.pem;
ssl_certificate_key /path/on/my/server/to/privatekey.pem;
return 301 https://www.$host$request_uri;
}
server {
listen 443 default_server ssl http2;
listen [::]:443 default_server ssl http2;
server_name www.example.com;
ssl_certificate /path/on/my/server/to/certificate.pem;
ssl_certificate_key /path/on/my/server/to/privatekey.pem;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
index index.php index.html index.htm;
}
}
子
server {
listen 80;
listen [::]:80;
server_name sub.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name sub.example.com;
ssl_certificate /path/on/my/server/to/subcertificate.pem;
ssl_certificate_key /path/on/my/server/to/subprivatekey.pem;
root /var/www/sub;
location / {
index index.php index.html index.htm;
try_files $uri = 404;
}
location ~ \.php$ {
try_files $uri = 404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php7-fpm-web1.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
【问题讨论】:
-
您当前的配置还将
www.example.com重定向到www.www.example.com。您确定正在加载sub配置文件吗?试试:nginx -t和nginx -T -
是的,配置文件已经加载,奇怪的是,在我清除浏览器缓存后它现在可以工作了......
标签: http redirect nginx https subdomain