Nginx certbot and redirect http

My domain is:

My web server is (include version): nginx 17

The operating system my web server runs on is (include version): Debian 9


I want ngnix to redirect http to https. To accomplish this I have added the following to my nginx config:

server {
    # Redirect http to https.
    listen 80 default_server;

    server_name _;

    return 301 https://$host$request_uri;

which works well. But when using “certbot --nginx”, the config is updated (added to my main server block: listen 80; # managed by Certbot). Thus the redirect no longer works. Is there an easy fix to this?

EDIT: I could do something like this: sed -i '/listen 80; # managed by Certbot/d' /etc/nginx/conf.d/default.conf. But I’m not sure if certbot will update the config again in its cron-job.

Thanks in advance!

Hi @oscarson

perhaps it's easier if you create an explicit vHost with


Or you remove the redirect and use

--redirect            Automatically redirect all HTTP traffic to HTTPS for
                        the newly authenticated vhost. (default: Ask)

So Certbot creates the own solution.


Thanks! The --redirect options works great. I removed the server block which I had in my config, was added by certbot.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.