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

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;
}