title: (NGINX) NodeJS Reverse Proxy Subdomain
body: <p>This question has probably been asked a lot, but I can't seem to find a really simple solution that actually works. And I need help to set this up.</p>
  <p><strong>I'll list my current enviroment so you can get a quick overview:</strong></p>
  <ul>
  <li>Server Host: HETZNER Cloud Server</li>
  <li>Domain Host: Webhuset.no (DNS Records are here)</li>
  <li>OS: CentOS 7 (Hetzner)</li>
  <li>Website: Running NGINX with NodeJS reverse proxy</li>
  <li>DNS: I have not added *.domain.example in my DNS Record, because I don't want every sub-host to to to my server.</li>
  <li>SSL: Enabled, using Let's Encrypt Certbot.</li>
  </ul>
  <p>I am only running one site, with of course, different pages for different stuff (Not multiple sites)</p>
  <p>My goal is to have something similar to CPanel-subdomains. Where I can add <strong>admin.domain.example</strong> and set it to for example <strong>domain.example/admin</strong> without redirect.</p>
  <p>But I have found this to be harder than expected, because I have been reading so many forum-posts and so many docs now and I just can't get it to work.</p>
  <p>I have come far enough to understand that the subdomain has to be configured in Nginx. Before this I kept trying to do this in the nodejs config.</p>
  <p>My current <strong>nginx.conf</strong> which makes my domain <strong>example.domain</strong> go straight to root folder of my website, with SSL. This works fine.</p>
  <pre><code>user nginx;
  worker_processes auto;
  error_log /var/log/nginx/error.log;
  pid /run/nginx.pid;
  
  include /usr/share/nginx/modules/*.conf;
  
  events {
      worker_connections 1024;
  }
  
  http {
      server {
          server_name example.domain www.example.domain;
          include /etc/nginx/default.d/*.conf;
  
          location / {
              proxy_pass http://127.0.0.1:3000;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection 'upgrade';
              proxy_set_header Host $host;
              proxy_cache_bypass $http_upgrade;
          }
  
          listen [::]:443 ssl ipv6only=on;
          listen 443 ssl;
          ssl_certificate /etc/letsencrypt/live/example.domain/fullchain.pem;
          ssl_certificate_key /etc/letsencrypt/live/example.domain/privkey.pem;
          include /etc/letsencrypt/options-ssl-nginx.conf;
          ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
      }
  }
  </code></pre>
  <p>Here I assume I have to add a server block in order to make <strong>admin.example.domain</strong> route to <strong>example.domain/admin</strong></p>
  <p>How can I do this and still maintain SSL? I need SSL in order to make the images render.
  I have tried adding a server block like this:</p>
  <pre><code> server {
      server_name admin.example.domain
  
      location / {
          proxy_pass http://127.0.0.1:3000/admin;
      }
  
      listen [::]:443 ssl ipv6only=on;
      listen 443 ssl;
      ssl_certificate /etc/letsencrypt/live/example.domain/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/example.domain/privkey.pem;
      include /etc/letsencrypt/options-ssl-nginx.conf;
      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
  }
  </code></pre>
  <p>If this were to work <em>(It doesn't unless I listen to port 80 and use http://)</em>, it would just redirect me to <strong>(UNSECURE)<a href="https://admin.example.domain/admin" rel="nofollow noreferrer">https://admin.example.domain/admin</a></strong></p>
  <p>And everybody who do the same as I just did above, gets it to work. But I don't. What is different, and how can I solve this?</p>