Настройка редиректов www → non-www и HTTP → HTTPS в то же время я столкнулся с проблемой дублирования, которую мне не удалось решить.
На моем домене — пусть это будет example.com
— у меня есть веб-сайт с основным именем another.example.com
. Я хочу, чтобы запросы на example.com
, www.example.com
и www.another.example.com
перенаправлялись на another.example.com
, а все HTTP-запросы одновременно перенаправлялись на HTTPS; Я также хочу поддерживать HTTP/2 и IPv6.
У меня нет проблем с тем, чтобы заставить это работать, но я не могу избавиться от дублирования существенной части файла конфигурации (, а именно настроек сертификата HTTPS). Все попытки уменьшить дублирование приводят к тому, что одна или несколько или все переадресации перестают работать (, иногда вместе с HTTP/2).
Пожалуйста, взгляните на конфиг и предложите, как его почистить.:
server {
listen 80;
listen [::]:80;
server_name www.another.example.com www.example.com another.example.com example.com;
return 301 https://another.example.com$request_uri;
}
server {
listen 443;
listen [::]:443;
server_name www.another.example.com www.example.com example.com;
return 301 https://another.example.com$request_uri;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
server_name another.example.com;
root /usr/share/nginx/another.example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
listen [::]:443 ssl http2;
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
server_name another.example.com;
root /usr/share/nginx/another.example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
listen [::]:443 ssl http2;
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.another.example.com www.example.com example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
rewrite ^/(.*)$ https://another.example.com/$1 permanent;
}
server {
listen 80;
listen [::]:80;
server_name www.another.example.com www.example.com another.example.com example.com;
location / {
if ($host !~* ^(www)) {
rewrite ^/(.*)$ https://another.example.com/$1 permanent;
}
}
}