I have cloudflare flexible enabled, How to redirect to https for Nginx server

If I access directly to https it works, but the site is still accessible by HTTP. How do I force all http to https?

I have used WordPress HTTPS plugin. But it is not redirect to https.

I have configured the nginx with following:

server {

    listen       xxx.x.xxx.xxx:80;
    listen       127.0.0.1:80;

    server_name mydomain.com;


    root   /var/www/html/mydomian.com/;
    index  index.html index.htm index.php;
    charset utf-8;



    location / {
            try_files $uri $uri/ /index.php?$args;
    rewrite ^(.*)$ https://$http_host$request_uri redirect;
  }
   ...

But this redirection will leads to infinite loop. I installed the
CloudFlare Flexible SSL, which said to solve the infinite loop, but still not helping.

Any other way to force the http redirect to https?

UPDATE

My ssl block:

   server {

       listen       xxx.x.xxx.xxx:443 ssl;
       listen       xxx.x.x.x:443 ssl;

    server_name  $hostname xxx.x.xxx.xxx;


    ssl                    on;
    ssl_certificate         /etc/nginx/ssl.crt/server.crt.combined;
    ssl_certificate_key     /etc/nginx/ssl.key/server.key;

    root   /var/www/html;
    index  index.html index.htm index.php;


    location ~^/~(?userdir_user.+?)(?userdir_uri/.*)?$ {
            alias /home/$userdir_user/private_html$userdir_uri;
            index index.html index.htm index.php;
            autoindex on;

            location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include /etc/nginx/fastcgi_params;
                fastcgi_index index.php;

                #try_files does not work after alias directive
                if (!-f $request_filename) {
                    return 404;
                }

                fastcgi_param DOCUMENT_ROOT /home/$userdir_user/private_html;
                fastcgi_param SCRIPT_FILENAME $request_filename;

                fastcgi_pass 127.0.0.1:9000;

            }
    } 



    location ~ \.php$ {
            try_files $uri =404;

            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include /etc/nginx/fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fastcgi_pass unix:/usr/local/php55/sockets/webapps.sock;
          fastcgi_pass 127.0.0.1:9000;

    }


    include /etc/nginx/nginx-info.conf;
    include /etc/nginx/webapps.ssl.conf;
}

Topic cloudflare nginx redirect ssl Wordpress

Category Web


There are several ways to enable https on your wordpress site.

You need to update your site urls including https in it if you have access to your dashboard

Wordpress settings page screenshot

You can also define both the WP_SITEURL and WP_HOME in your test wp-config.php

define( 'WP_SITEURL', 'http://example.com.mytestdomain.com' );
define( 'WP_HOME', 'http://example.com.mytestdomain.com' );

In relation to your nginx config:

You can use an if block instead of nesting the rewrite in your base location

if ($host ~* ^example\.com$) {
    rewrite ^(.*)$ https://example.com$1 permanent;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.