Enabling Redirect non-https traffic to https loop nginx

I just used certbot to install let’s encrypt on a ubuntu 16.04 server and Nginx. The install went fine and I was able to vertify the install using ssl labs website. When I edit the nginx conf file etc/nginx/sites-available/mysite and uncomment the redirect added by certboot in the botton of the file `

# Redirect non-https traffic to https
# if ($scheme != "https") {
#   return 301 https://$host$request_uri;
# } # managed by Certbot

` the site starts redirection to itself in a loop.

My server block looks like this (server name changed):

`server {
listen 80;
#listen [::]:80; # Uncomment this line if you also want to enable IPv6 support
server_name example.com www.example.com *.example.com;

root /var/www/example;
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
server_tokens off;

 index index.php; # Letting nginx know which files to try when requesting a folder
#index /example/index.php; # Letting nginx know which files to try when requesting a folder


location = /favicon.ico {
    log_not_found off;      # 
    access_log off;         # Disable logging to prevent excessive log sizes

#################

} `

The redirect made by certbot is in the botton of the conf file, not insied the server block at thoe top of the file.

SOLVED. certbot placed the new redirect directived in the botton of my conf file. When I moved the new redirect directive and other certbot stuff into the server block at the top of the file, everything works smooth.

1 Like

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