我是Nginx的新手并试图弄清楚如何正确处理子域名。我想要实现的是主域example.com
始终重定向到https://www.example.com
,但子域sub.example.com
应始终重定向到https://sub.example.com
。在我当前的设置中,第一个要求已完成,但sub.example.com
始终会重定向到https://www.sub.example.com
。我的配置有什么问题,我该如何解决?
先谢谢Fabian。
<小时/> 我的两个服务器配置文件:
默认
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;
}
}
答案 0 :(得分:0)
如果其他人遇到此问题:请尝试清理浏览器缓存。