title: NGINX 2 domains on the same IP, want to redirect both to HTTPS
body: <p>I have 2 domains running on my server, NGINX just proxies them to node apps. I have a certificate for one, but for the other I'm just using cloudflare to provide HTTPS. I want to ensure that when users visit either domain, they always get redirected to the HTTPS version of the domain, without a www. This is my current configuration, uncommenting the block for the domain2 configuration file seems to break both sites :(</p>
  
  <p><strong>domain1 config file:</strong></p>
  
  <pre><code>upstream domain1.com {
      server 127.0.0.1:8000;
      keepalive 8;
  }
  
  server {
  
      listen 0.0.0.0:80;
      server_name domain1.com www.domain1.com;
      return 301 https://domain1.com$request_uri;
  
  }
  
  server {
      #listen 80;
      listen 443 ssl http2;
      server_name domain1.com;
      access_log /var/log/nginx/domain1.com.log;
  
      root /var/www/domain1.com/client/public;
  
      include /etc/nginx/global/cloudflare-allow.conf;
  
      ssl_certificate /etc/nginx/ssl/domain1.crt;
      ssl_certificate_key /etc/nginx/ssl/domain1.key;
  
      if ($bad_referer) {
          return 444;
      }
  
      location / {
          proxy_http_version 1.1;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;
          proxy_set_header Connection "";
  
          proxy_pass http://domain1.com;
          proxy_redirect off;
      }
  
      location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|webp)$ {
          expires 1M;
          access_log off;
          add_header Cache-Control "public";
      }
  
      # CSS and Javascript
      location ~* \.(?:css|js)$ {
          expires 1y;
          access_log off;
          add_header Cache-Control "public";
      }
  
      location ~* \.(?:rss|atom)$ {
          expires 1h;
          add_header Cache-Control "public";
      }
  
      location ~* \.(?:manifest|appcache|html?|xml|json)$ {
          expires -1;
      }
  
   }
  
  server {
  
      listen 443 ssl http2;
      server_name www.domain1.com;
      return 301 https://domain1.com$request_uri;
  
  }
  </code></pre>
  
  <p><strong>domain2 config file:</strong></p>
  
  <pre><code>upstream domain2.com {
      server 127.0.0.1:9000;
      keepalive 8;
  }
  
  #server {
  #    listen 80;
  #    server_name domain2.com www.domain2.com;
  #    return 301 https://$server_name$request_uri;
  #}
  
  server {
      listen 80;
      #listen 443 ssl http2;
      server_name domain2.com;
      access_log /var/log/nginx/domain2.com.log;
  
      root /var/www/domain2.com;
  
      include /etc/nginx/global/cloudflare-allow.conf;
  
      if ($bad_referer) {
          return 444;
      }
  
      location / {
          proxy_http_version 1.1;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;
          proxy_set_header Connection "";
  
          proxy_pass http://domain2.com;
          proxy_redirect off;
      }
  
      location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|webp)$ {
          expires 1M;
          access_log off;
          add_header Cache-Control "public";
      }
  
      # CSS and Javascript
      location ~* \.(?:css|js)$ {
          expires 1y;
          access_log off;
          add_header Cache-Control "public";
      }
  
      location ~* \.(?:rss|atom)$ {
          expires 1h;
          add_header Cache-Control "public";
      }
  
      location ~* \.(?:manifest|appcache|html?|xml|json)$ {
          expires -1;
      }
  
   }
  </code></pre>