0
我是Nginx新手,想弄清楚如何正確處理子域名。我想實現的是主域example.com
總是被重定向到https://www.example.com
,但子域名爲sub.example.com
應該總是被重定向到https://sub.example.com
。在我目前的設置中,滿足了第一個要求,但總是將sub.example.com
重定向到https://www.sub.example.com
。我的配置有什麼問題,我該如何解決它?nginx - 只將主域名重定向到www,而不是子域名
在此先感謝,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;
}
}
您目前的配置也會將'www.example.com'重定向到'www.www.example.com'。你確定'sub'配置文件正在被加載嗎?嘗試:'nginx -t'和'nginx -T' –
是的,配置文件被加載,奇怪的是,它現在工作後,我清除瀏覽器緩存... – FTFT1234