HTTP to HTTPS Nginx too many redirects

I have a website running on the LEMP stack. I have included cloudflare from the website. I am using cloudflare flexible SSL certificate for https. When I open a website in chrome, it shows that the website redirected you too many times, and in firefox found that the server redirects the request to this address in a way that will never be completed. I tried to see the answers to other questions, but none of them seemed to solve the problem. NGINX configuration file: -

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name mydomain.com www.mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
root /var/www/html;

index index.php index.html index.htm index.nginx-debian.html;

location / {
    try_files $uri $uri/ =404;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

location ~ /\.ht {
    deny all;
}
}

I would really appreciate it if someone could point out what I'm doing wrong.

+4
source share
1 answer

SSL- cloudflare, nginx : -

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name mydomain.com www.mydomain.com;

  if ($http_x_forwarded_proto = "http") {
      return 301 https://$server_name$request_uri;
  }

  root /var/www/html;

  index index.php index.html index.htm index.nginx-debian.html;

  location / {
     try_files $uri $uri/ =404;
  }

  location ~ \.php$ {
     include snippets/fastcgi-php.conf;
     fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  }

  location ~ /\.ht {
     deny all;
  }
}
+13

Source: https://habr.com/ru/post/1666456/


All Articles