-2

I am using Ubuntu 14.04 with nginx installed and SSL installed on main domain, which works fine. I have even parked 5-6 other domains with main domain. If I go to any of those domains, I get an SSL warning because I have forced HTTPS in nginx config using following config:

I have added following in /etc/nginx-sp/vhosts.d/APPNAME.d/redirect_to_https.nonssl_conf

return 302 https://$host$request_uri;

And this correctly forces the HTTPS version to be served.

How could I implement some condition such as: if domain is main.example.com then redirect to HTTPS version, otherwise show HTTP version?

0

Any attempt at creating an if statement to return the https version of the site will result in a redirect loop (as your condition, server_name is not changing after the redirect, so will always match the condition and redirect again...).

With that said, what I would recommend would be to set up individual configuration files per virtual host/subdomain (I infer that from your current setup, you are using a single catch-all configuration - if this is not the case, please update your question accordingly)

You may also find the below a more elegant way of redirecting to https on the domain(s)/subdomain(s) you wish to be served always via https:

    if ( $scheme = http ){
            rewrite ^ https://$server_name$request_uri? permanent;
    }

For example you could create the following:

server {
  server_name nonssldomain.com anothernonssldomain.com;
  listen 80;
           }

in a file called non_ssl_sites.conf (or whatever you like), then another:

server {
  server_name ssldomain.com;
  listen 80;
  listen 443 ssl;

  if ( $scheme = http ){
            rewrite ^ https://$server_name$request_uri? permanent;
  }
           }

in a file called ssl_sites.conf (or again, whatever you prefer)

  • What i want to do is to check if domain is 'main.com' then force SSL. – seoppc Dec 15 '14 at 10:09
  • Yes, so as above - you will need to create individual configuration files for each domain you want to serve from the server. – BE77Y Dec 15 '14 at 10:10
  • I have updated my answer above to reflect an example – BE77Y Dec 15 '14 at 10:14
  • Is there any way to use one configuration file like i already have and check for domain if($domain == 'main.com') return 302 https://$host$request_uri; – seoppc Dec 15 '14 at 10:15
  • No, because as I have said above, you will find that this causes a redirect loop. You may use one configuration file but will need separate server segments as in my examples above. – BE77Y Dec 15 '14 at 10:16
3

You should use a different server block for each "type" of domain you manage, so that you don't have to use if statements to test the $host variable.

# Domains to serve only via HTTPS
server {
  server_name domaintoredirect1.tld domaintoredirect2;
  listen 80;

  return 302 https://$host$request_uri;
}

server {
  server_name domaintoredirect1.tld domaintoredirect2;
  listen 443 ssl;

  ...
}

# Domains to serve via both HTTP/HTTPS
server {
  listen 80;
  listen 443 ssl;

  server_name otherdomain1.tld otherdomain2.tld;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.