title: nginx : how redirect server side application access to correct domain ( multiple domains on same vps )
body: <p>I'm facing an issue on my nginx configuration :</p>
  
  <hr>
  
  <p><strong>Context:</strong></p>
  
  <ul>
  <li>I have a VPS, and multiple domains on it.</li>
  <li>I host <strong>2</strong> web applications on it.</li>
  <li>I use let's encrypt through certbot to handle https.</li>
  </ul>
  
  <p>Each web app is composed of :</p>
  
  <ul>
  <li>a static build for front-end served on one domain ( http redirects to https )</li>
  <li>a https served node.js backend ( port :5000/graphql for app1 and :5001/graphql for app2 )</li>
  </ul>
  
  <p>I can access to each backend if I specify the port, using any of the domain I have :</p>
  
  <ul>
  <li>https//domain1:5000/graphql is ok ( :5000 is for the backend of app1 served on domain1 )</li>
  <li>https//domain2:5000/graphql is also ok ( complaining not secure, since :5000 is for app1 )</li>
  </ul>
  
  <hr>
  
  <p><strong>My question:</strong></p>
  
  <p>How can I configure properly nginx to redirect all request to :5000 to <a href="https://domain1:5000" rel="nofollow noreferrer">https://domain1:5000</a> and all request on :5001 to <a href="https://domain2:5001" rel="nofollow noreferrer">https://domain2:5001</a> ? ( and doing the same for the other port) </p>
  
  <hr>
  
  <p><strong>My nginx conf</strong></p>
  
  <p>( both apps have same config expect for the domain and the port )</p>
  
  <pre><code>server {
          server_name domain1.com www.domain1.com;
          root /path/to/client/build
          index index.html;
  
          access_log /var/log/nginx/domain1.com.access.log;
          error_log /var/log/nginx/domain1.com.error.log;
  
          location / {
                  try_files $uri /index.html;
          }
          location /graphql {
                  proxy_pass https://localhost:5000;
                  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; # managed by Certbot
      ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
      ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem; # managed by Certbot
      include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  }
  
  server {
      if ($host = www.domain1.com) {
          return 301 https://$host$request_uri;
      } # managed by Certbot
  
  
      if ($host = domain1.com) {
          return 301 https://$host$request_uri;
      } # managed by Certbot
  
  
          server_name domain1.com www.domain1.com;
      listen 80;
      return 404; # managed by Certbot
  
  }
  </code></pre>
  
  <p>Thanks !</p>