NGINX HTTPS not redirecting properly

I followed Cerbot’s instructions
to get a HTTPS certificate for NGINX in my Debian server for a
domain, but the HTTPS is not redirecting properly.

I got the following in etc/nginx/conf.d/app.conf from Certbot’s automatic generation:

server {
    server_name mnpd.khkm.dev www.mnpd.khkm.dev;
    # listen 8080;
    server_tokens off;
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }
    location / {
        # return 301 https://mnpd.khkm.dev$request_uri;
        proxy_pass http://mnpd.khkm.dev;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mnpd.khkm.dev/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mnpd.khkm.dev/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 = mnpd.khkm.dev) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;
    server_name mnpd.khkm.dev www.mnpd.khkm.dev;
    return 404; # managed by Certbot
}

In Chrome, when I go to https://mnpd.khkm.dev/,
I get:

mnpd.khkm.dev redirected you too many times.
Try deleting your cookies.
ERR_TOO_MANY_REDIRECTS

I found this Stack Overflow answer
where I looked at the "Network" tab in the web console and
saw that the page is constantly being redirected to https://mnpd.khkm.dev/.
The NGINX configuration should be listening to port 443 for
the HTTPS, so why isn’t it loading and constantly being
redirected? (I expect the default NGINX page to be loaded.)

Asked By: Kevin

||

The port 80 is being redirected to 443 which is trying to proxy 80 which is redirecting ….

Remove the

    location / {
        # return 301 https://mnpd.khkm.dev$request_uri;
        proxy_pass http://mnpd.khkm.dev;
    }

I wouldn’t let certbot rewrite my config files. It really only works well for the simplest use case. I recommend using certbot in standalone mode. That makes the config your problem and lets certbot manage the certificates.

You run cert bot, it tells you where the cert files are and you add those bits to your HTTPS server config. When it renews; it puts the new files in the same location so you don’t have to change the config again after the site is setup. certbot update and restart nginx.

Answered By: txyoji
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.