
Configurando redirecionamentoswww→não wwweHTTP→HTTPSao mesmo tempo, encontrei problemas de duplicação que não consegui superar.
No meu domínio – deixe estar example.com
– tenho um site com nome principal another.example.com
. Quero que as solicitações para example.com
, www.example.com
e www.another.example.com
sejam redirecionadas para another.example.com
, e todas as solicitações HTTP sejam redirecionadas para HTTPS ao mesmo tempo; Também quero oferecer suporte a HTTP/2 e IPv6.
Não tenho problemas em fazer isso funcionar, mas não consigo me livrar da duplicação de uma parte substancial do arquivo de configuração (ou seja, configurações do certificado HTTPS). Todas as tentativas de reduzir a duplicação fazem com que um ou mais redirecionamentos parem de funcionar (às vezes junto com HTTP/2).
Por favor, dê uma olhada na configuração e sugira como limpá-la:
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;
}
Responder1
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;
}
}
}