title: NGINX redirect HTTPS to HTTPS
body: <p><strong><em>How can I redirect https from one domain to https of another?</em></strong></p>
  
  <h2>Case description:</h2>
  
  <p>I own three domains that is directed to the same ip/website in the NS server:</p>
  
  <ol>
  <li>example1.com </li>
  <li>example2.com </li>
  <li>example3.com</li>
  </ol>
  
  <p>I paid only for one SSL licence that initially was issued for the first domain, so when I bought the second domain, I redirected all traffic to <a href="https://example1.com" rel="nofollow noreferrer">https://example1.com</a></p>
  
  <p>Lately, I wanted to redirect all calls to a new domain: <a href="https://example3.com" rel="nofollow noreferrer">https://example3.com</a>. I reissued the old SSL licence for the new domain and installed it successfully. The last part is redirecting all traffic to the new domain.</p>
  
  <p>Now, all traffic from http is redirect well, but http<strong>s</strong> (<a href="https://example1.com" rel="nofollow noreferrer">https://example1.com</a> and <a href="https://example2.com" rel="nofollow noreferrer">https://example2.com</a>) is not directed at all, and results in working "Not secure" page. </p>
  
  <p>When I try to listen to <code>443 SSL</code> and redirect if from 1&amp;2 name servers to the third one and run <code>service nginx restart</code>, I get:</p>
  
  <pre><code>Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
  </code></pre>
  
  <p>When I run <code>systemctl status nginx.service</code>, I get:</p>
  
  <pre><code>nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Sun 2020-05-24 17:53:24 UTC; 4min 5s ago
    Process: 14068 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
    Process: 13952 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 14073 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
   Main PID: 13957 (code=exited, status=0/SUCCESS)
  </code></pre>
  
  <p>My guess I'm doing something wrong and redirecting <code>443 SSL</code> twice is not allowed.</p>
  
  <h2>Code</h2>
  
  <p>This is my conf that is working without redirecting https example1.com and example2.com:</p>
  
  <p>On <code>/etc/nginx/nginx.conf</code> I have <code>include /etc/nginx/sites-enabled/*;</code>.</p>
  
  <p>On <code>/etc/nginx/sites-enabled/example3.com.conf</code> I wrote this:</p>
  
  <pre><code>server {
      listen 80;
      server_name example3.com wwww.example3.com example1.com www.example1.com example2.com www.example2.com;
      rewrite ^/(.*) https://example3.com/$1 permanent;
  }
  
  
  server {
      listen 443 ssl;
      server_name example3.com;
      ssl_certificate /etc/ssl/certs/ssl-bundle.crt;
      ssl_certificate_key /etc/ssl/websitessl/example3.com.key;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_prefer_server_ciphers on;
      ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
  
      root /var/www/html;
      index index.php index.html index.htm index.nginx-debian.html;
  }
  </code></pre>
  
  <p>This is maybe duplication of this:
  <a href="https://stackoverflow.com/questions/32433846/https-to-https-redirect-nginx">HTTPS to HTTPS redirect Nginx</a>
  But I failed to make it work.</p>