Redirect from HTTP to HTTPS in Nginx without creating another server block

1 post / 0 new
#1 Sun, 10/27/2013 - 16:34
virtualminfan

Redirect from HTTP to HTTPS in Nginx without creating another server block

Hi, I know this can be done to redirect from www to non-www

server {                                                               
        server_name site.com www.site.com;             
        if ($host = 'www.site.com') {                          
            rewrite ^/(.*)$ http://site.com/$1 permanent;   
        }                                                              
        ...
}

In this case, how can I redirect HTTP to HTTPS? Currently I can redirecting it with 2 server blocks like:

server {                                                                   
        server_name site.com www.site.com;                 
        listen 162.243.47.235:80;                                          
        rewrite        ^ https://site.com$request_uri? permanent;  
}                                                                          
server {                                                                   
        server_name site.com www.site.com;                 
                                                                           
        if ($host = 'www.site.com') {                              
            rewrite ^/(.*)$ $scheme://site.com/$1 permanent;       
        }                                                                  
        ...
        listen 162.243.47.235:443 ssl;                                          
        ...
}

but I assume it will give problem to Virtualmin when trying to delete a Virtual Server, correct?

Thank you.