Question

Nginx, SSL, multiple domains

Hi folks!

Have been straggling around nginx.conf for multiple domains.

This is what I have

  1. 3 domains
  2. SSL cert
  3. Droplet
  4. nginx

I want to redirect all https, https://www, http, http://www to one of the domain from these 3. Simply saying, all should go to just https://www.example.com

ssl_certificate /var/www/domain1.com/shared/ssl/bundle.crt; 
ssl_certificate_key /var/www/domain1.com/shared/ssl/www_domain1_com.key;
ssl_verify_depth 3;

server {
	listen         *:80;
	server_name    www.domain1.com domain1.com www.domain2.com domain2.com www.domain3.com domain3.com;
	return         301 https://www.domain1.com$request_uri;
}

server {
	listen         *:443 ssl;
	server_name   domain1.com www.domain2.com domain2.com www.domain3.com domain3.com;
	return         301 https://www.domain1.com$request_uri;
}


server {
    	server_name www.domain1.com;
    	listen 443 ssl;
    	root /var/www/domain1.com/current/public;
    	access_log /var/www/domain1.com/current/log/nginx.access.log;
    	error_log /var/www/domain1.com/current/log/nginx.error.log info;
}

these above config does not have any configuration issues. nginx -t give “ok”.

But the problem is that all https requests (which are not from domain1.com) trying to shake ssl hands which is not true. they should 301-ing to www.domain1.com

I have tried doing some playing around in default config, seemed do not help!

any suggestions - would be great!

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
June 15, 2016
Accepted Answer

Hi!

Because of the way TLS/SSL works, the whole handshake and encryption process must be done before receiving any HTTP headers or the response body (as all HTTP traffic is passed through the TLS/SSL ‘tunnel,’ so it has to be established first).

So you will have to create valid HTTPS server blocks for every one of the domains and configure that to redirect to the domain that you want.

server {
    listen         *:443 ssl;
    server_name   domain1.com;
    ssl_certificate /path/to/domain1.crt; 
    ssl_certificate_key /path/to/domain1.key;
    return         301 https://www.domain1.com$request_uri;
}

server {
    listen         *:443 ssl;
    server_name   domain2.com www.domain2.com;
    ssl_certificate /path/to/domain2.crt; 
    ssl_certificate_key /path/to/domain2.key;
    return         301 https://www.domain1.com$request_uri;
}

server {
    listen         *:443 ssl;
    server_name   domain3.com www.domain3.com;
    ssl_certificate /path/to/domain3.crt; 
    ssl_certificate_key /path/to/domain3.key;
    return         301 https://www.domain1.com$request_uri;
}

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more