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

Question

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;
}
0
, , , dev-jim 5 years 2016-12-31T03:12:41-05:00 0 Answers 75 views 0

Leave an answer

Browse
Browse