Question

I want to force HTTPS for certain subdomains in nginx. I also do not want to edit the .conf file when i create a new subdomain that needs HTTPS. Is it possible to force HTTPS if an SSL folder is present in the root or something equivalent to it? So all i have to do to enable HTTPS on a new subdomain is add a new folder or something like that?

Here is my nginx.conf file:

    #HTTP DOMAIN.COM
    server {
       listen               80;
       server_name          *.domain.com;
       root                 /var/www/$http_host;


            location /ssl {
                    rewrite ^ https://$http_host/$1 redirect;
            }

edit: I've been messing around a bit with symlinks but no luck yet. This is the new configuration I made:

server {
    listen              80;
    server_name         *.domain.com;
    root                /var/www/$http_host;

        location /var/www/ssl/$http_host {
                 rewrite  https://$http_host/$1 redirect;
        }

    include             error_page;
    include             location_php;
}

Am I coming close to my answer?

edit: What I basically want is to be able to use ssl without having to change my .conf file. All I need to do to enable HTTPS is change something somewhere around the docroot.

Was it helpful?

Solution 2

I got it working! this is the config file:

server {
    listen          80;
    server_name     *.domain.com;
    root            /var/www/$host;

    if (-e /var/www/config/ssl/$host) {
            rewrite ^ https://$host$request_uri? permanent;
    }

    include         error_pages;
    include         php_config;
}

I still had to use an if statement but that's alright because it works now.

OTHER TIPS

I don't think you can enable SSL based merely on the existence of a directory. But you can extend the configuration using file globbing:

include vhosts/*.conf
include vhosts/ssl/*.conf

Just drop a minimal file in the directory.

(note that you still need to set up certificates, most likely on a per vhost basis - and these should not be incide the document root).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top