I have the same configuration on all my blocks yet only this one redirects to www while all my other redirects to non www. I think it may have to do something with the hsts that I've recently added but am not sure, because like I said the other blocks have the same config and this is the config file:
server {
root /var/www/mydomain;
index index.php index.html index.htm index.nginx-debian.html;
server_name mydomain.es www.mydomain.es;
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png|svg|woff)$ {
expires 2d;
add_header Cache-Control "public, no-transform";
}
location ~* \.(jpg|jpeg|gif|png|svg|woff)$ {
expires 365d;
}
location ~* \.(pdf|css|html|js|swf)$ {
expires 2d;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mydomain.es/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mydomain.es/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}
server {
if ($host = www.mydomain.es) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = mydomain.es) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name mydomain.es www.mydomain.es;
listen 80;
return 404; # managed by Certbot
}
What am I doing wrong?