How to redirect non-https to https?

5 posts / 0 new
Last post
#1 Mon, 06/11/2018 - 07:19
peterwebdev

How to redirect non-https to https?

I have turned on the option under Virtualmin settings but it doesn't seem to be working.

I am using Nginx instead of Apache.

i see some post about 301 redirection but the way webmin sets up everything in one block its hard to read.

server { server_name peterwebsolutions.com www.peterwebsolutions.com; listen 128.199.xx.xx; root /home/webmanager/public_html; index index.html index.htm index.php; access_log /var/log/virtualmin/peterwebsolutions.com_access_log; error_log /var/log/virtualmin/peterwebsolutions.com_error_log; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME /home/webmanager/public_html$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT /home/webmanager/public_html; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; fastcgi_param HTTPS $https; location ~ .php$ { try_files $uri =404; fastcgi_pass unix:/var/php-nginx/152871755116483.sock/socket; } listen 128.199.90.xx:443 ssl http2; ssl_certificate /home/webmanager/ssl.combined; ssl_certificate_key /home/webmanager/ssl.key; }

XX in the ip is just me censoring it out.

i tired adding a redirect above the listen :443 but it still to till redirect when the site is on https

Mon, 06/11/2018 - 20:43
jimdunn

I have tested the following and it seems to work for Apache2.

# .htaccess
<IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTPS} off
        RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
</IfModule>
Tue, 06/12/2018 - 00:33
peterwebdev

THanks i am using Nginx, so no htaccess =(

Tue, 06/12/2018 - 12:58
jimdunn

Oh, so sorry... for NGINX, do the following.

vi /etc/nginx/conf.d/default.conf

and then be sure to have the following "return 301" line in the default.conf file.

server {
listen       80;
server_name  www.example.com 1.2.3.4;
return 301 https://$server_name$request_uri;

You should change the domain and IP to your domain and IP address.

(and this will only work if you have certbot and the ssl_certificate lines in there as well)

Tue, 06/12/2018 - 21:29 (Reply to #4)
peterwebdev

thanks

i used something similar, im sure your one would work the same

server { server_name domain.com www.domain.com; listen IPADDRESS:80; rewrite ^/(.*) https://domain.com/$1 permanent; }

i added it the VHOST.

Topic locked