MyBB Community Forums

Full Version: Too many redirects error when using CloudFlare SSL with nginx URL rewriting
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am getting error "Too many redirects" in browser whenever I try to access any thread using HTTPS. HTTP works fine.
All .php pages such as index.php, member.php also work fine on HTTPS and problem is with only those pages which are coming through URL rewriter such as threads and posts etc.. Here's my setup:

1. CloudFlare SSL is set to flexible i.e. site can be accessed via both HTTP and HTTPS
2. Site and Board URL in admin panel is set to "https://"
3. Here's my nginx file


server {
        listen 80;
        listen [::]:80;
        server_name mybb.com;
        return 301 https://www.mybb.com$request_uri;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/mybb/html;
        index portal.php index.php  index.html index.htm;

        server_name www.mybb.com;

        location / {
                try_files $uri $uri/ = 404;
                rewrite ^/([^&]*)&(.*)$ https://www.mybb.com/$1?$2 permanent;
                rewrite ^/((?i)sitemap-([^./]+)\.xml)$ /misc.php?google_seo_sitemap=$2;
                rewrite ^/((?i)Forum-([^./]+))$ /forumdisplay.php?google_seo_forum=$2;
                rewrite ^/((?i)Thread-([^./]+))$ /showthread.php?google_seo_thread=$2;
                rewrite ^/((?i)Announcement-([^./]+))$ /announcements.php?google_seo_announcement=$2;
                rewrite ^/((?i)User-([^./]+))$ /member.php?action=profile&google_seo_user=$2;
                rewrite ^/((?i)Calendar-([^./]+))$ /calendar.php?google_seo_calendar=$2;
                rewrite ^/((?i)Event-([^./]+))$ /calendar.php?action=event&google_seo_event=$2;
        }

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

        location ~ /\.ht {
                deny all;
        }

}
EDIT

I commented following two lines in my file above and also set ALWAYS USE HTTPS to ON in CloudFlare admin panel but still redirect error.
#return 301 https://www.mybb.com$request_uri;
#rewrite ^/([^&]*)&(.*)$ https://www.mybb.com/$1?$2 permanent;
(2018-02-01, 06:36 AM)forumtester123 Wrote: [ -> ]
server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/mybb/html;
        index portal.php index.php  index.html index.htm;

This server listens on port 80 only (IPv4 and IPv6), that makes it a HTTP server. Not HTTPS.

As a result, PHP will also see port 80 and Google SEO will believe a redirect to HTTPS is in order. (if you set the bburl to https://)

If HTTPS is enforced / provided by external service (such as cloudflare) this is unfortunately not detected. What you have to do in this case, is

# in case of Apache, .htaccess
SetEnv HTTPS 1

# in case of Nginx fastcgi-php
fastcgi_param HTTPS 1;
fastcgi_pass...

That forcibly sets $_SERVER[HTTPS] to 1 in PHP, so Google SEO will believe you're on HTTPS and stop redirecting you (but then it also won't redirect HTTP).

Alternatively you can also try this codechange: https://github.com/frostschutz/MyBB-Google-SEO/pull/57

I plan to disable http <-> https redirects in Google SEO altogether (by default, and make the old functionality optional). You need a different way to redirect HTTPS anyway (for all resources not covered by Google SEO), so it's best to leave this headache to the site admin. Toungue

But I didn't get around to it yet... Sleepy



Note that HTTPS provided by an external service, is no true HTTPS at all.

It will result in Client <- HTTPS -> Cloudflare <- HTTP -> YourServer

So while traffic from and to client is encrypted, traffic from and to your server is still unencrypted.

That's unless the HTTPS proxy is a local service running on your own server.