nginx - 仅将主域重定向到 www,而不是子域
nginx - redirect only main domain to www, not subdomains
我是 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;
}
}
sub
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;
}
}
如果其他人遇到此问题:请尝试清理浏览器缓存。
我是 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;
}
}
sub
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;
}
}
如果其他人遇到此问题:请尝试清理浏览器缓存。