admin管理员组

文章数量:1122846

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;


    root   /var/www/html/mydomian/;
    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.crtbined;
    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;
}

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;
}
Share Improve this question edited Dec 31, 2016 at 17:47 dev-jim asked Dec 31, 2016 at 9:12 dev-jimdev-jim 1,98811 gold badges35 silver badges54 bronze badges 3
  • This may help stackoverflow.com/questions/35143193/… – prosti Commented Dec 31, 2016 at 17:27
  • @prosti, nope, still getting in infinite redirection loop. – dev-jim Commented Dec 31, 2016 at 18:19
  • The settings on cloud flare is what I would try to alter as the first thing. BTW, I used let's encrypt (free) https. You may consider that @dev-jim – prosti Commented Jan 3, 2017 at 21:38
Add a comment  | 

1 Answer 1

Reset to default 0

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

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

本文标签: sslI have cloudflare flexible enabledHow to redirect to https for Nginx server