title: nginx to expose internal http service as https subdomain
body: <p>I have found several partial answers on this site and similar ones, but I couldn't figure out how to put them together, or maybe the answer is there and I cannot recognise it :-/</p>
  
  <p>What I'm trying to achieve:</p>
  
  <p>I have an http service running in the LAN (I have setup dnsmasq appropriately) as:</p>
  
  <p><a href="http://subdomain1.domain.com:1234/" rel="nofollow">http://subdomain1.domain.com:1234/</a></p>
  
  <p>and I would like to expose it as internet (also the external DNS is working fine):</p>
  
  <p><a href="https://subdomain2.domain.com:443/" rel="nofollow">https://subdomain2.domain.com:443/</a></p>
  
  <p>with user authentication handled by nginx.</p>
  
  <p>I also want to (must?) keep the URL visible in the browser unmodified.</p>
  
  <p>I have tried several combinations of what I found from similar questions, but something seems to elude me.</p>
  
  <p>This is my last attempt:</p>
  
  <pre><code>ssl_certificate /var/www/domain.com/domain_com.crt;
  ssl_certificate_key /var/www/domain.com/domain_com.key;
  
  server {
          listen   443 default_server;
          server_name subdomain1.domain.com;
  
          location / {
                  auth_basic "Restricted";
                  auth_basic_user_file /var/www/domain.com/domain.com.passwords;
  
                  proxy_pass              http://subdomain1.domain.com:1234/;
                  proxy_set_header        Host    $http_host;
                  # proxy_set_header        X-Real-IP       $remote_addr;
                  # proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_redirect          https://subdomain2.domain.com/       http://subdomain1.domain.me:1234/;
          }
  }
  </code></pre>
  
  <p>What I get is: "Error 107 (net::ERR_SSL_PROTOCOL_ERROR): SSL protocol error."</p>
  
  <hr>
  
  <p>Update:</p>
  
  <p>I think I found a solution, but review would still be appreciated.
  This one also rewrites http access to go through https.</p>
  
  <pre><code>ssl_certificate /var/www/domain.com/domain_com.crt;
  ssl_certificate_key /var/www/domain.com/domain_com.key;
  
  server {
          listen   80;
          server_name subdomain1.domain.com;
          rewrite ^ https://$server_name$request_uri? permanent;
  }
  
  server {
          listen   443;
          ssl on;
          server_name subdomain1.domain.com;
  
          location / {
                  auth_basic "Restricted";
                  auth_basic_user_file /var/www/domain.com/domain.com.passwords;
  
                  proxy_pass              http://subdomain2.domain.com:1234/;
                  proxy_set_header        Host    $host;
                  proxy_set_header        X-Real-IP       $remote_addr;
                  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header        X-Forwarded-Proto       $scheme;
                  add_header              Front-End-Https         on;
                  proxy_redirect          off;
          }
  }
  </code></pre>