nginxhttp-redirectvhosts

How do I eliminate a nginx too many redirects error?


I am getting a redirect error im not sure how to fix. The error is; ERR_TOO_MANY_REDIRECTS

Heres my host file on nginx. Any advice appreciated. Thanks.

# HTTP
server {
    server_name _;
    # I added the line below to solver: Could not automatically find a matching server block for mail.mydomain.net. Set the `server_name` directive to use the Nginx installer.
    server_name mydomain.net www.mydomain.net;

    # Redirect all insecure http:// requests to https://
    return 301 https://$host$request_uri;

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.net/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


}

server {
    if ($host = www.mydomain.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = mydomain.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name _;
    server_name mydomain.net www.mydomain.net;
    return 404; # managed by Certbot

}

I have tried without the host_name as it previously was but that doesnt work. Im not sure what to try now as I am more used to apache and nginx seems quite different.


Solution

  • Your lines of

        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
    

    are not HTTP, therefore you will need to remove the

        # Redirect all insecure http:// requests to https://
        return 301 https://$host$request_uri;
    

    lines just before that and test again (don't forget to deploy your changes to sites-enabled or where you need it to be enabled and to restart nginx). That might already fix your issue, but you will also need to test this so, if you still have these redirects, some of them may come from the app code.