title: Nginx - is it possible to redirect to different https servers based on the servername?
body: <p>Currently , I am redirecting http request this way :</p>
  
  <pre><code>server {
      listen 80;
      listen [::]:80 ipv6only=on;
      server_name emberhub.me www.emberhub.me test.emberhub.me;
      return 301 https://$host$request_uri;
  }
  
  
  # HTTPS server static html
  #
  server {
      listen 443 ssl;
      server_name emberhub.me www.emberhub.me;
  
      root html;
      index index.html index.htm;
  
      ssl on;
      ....
      location / {
              try_files $uri $uri/ =404;
  
  
  # HTTPS server proxy Node.js app
  #
  server {
      listen 444 ssl;
      server_name test.emberhub.me;
  
      ssl on;
      ...
      location / {
          proxy_pass http://127.0.0.1:8080;
  </code></pre>
  
  <p>When user request :
  <a href="http://emberhub.me" rel="nofollow">http://emberhub.me</a>  or <a href="http://www.emberhub.me" rel="nofollow">http://www.emberhub.me</a> , he is correctly redirected to  <a href="https://emberhub.me" rel="nofollow">https://emberhub.me</a> or <a href="https://www.emberhub.me" rel="nofollow">https://www.emberhub.me</a>  ( on default port 443 )</p>
  
  <p>But when user request:
  <a href="http://test.emberhub.me" rel="nofollow">http://test.emberhub.me</a> , he is redirected ALSO to port 443 and get the static html page, not to the node.js app</p>
  
  <p>If the user request it with the port 444 
  <a href="http://test.emberhub.me:444" rel="nofollow">http://test.emberhub.me:444</a>, then he is correctly proxied to the node.js app...</p>
  
  <p>Is there a way to filter the request based on the server_name and redirect it with the correct SSL port ? or is there another better way to perform these redirections ?</p>
  
  <p>thanks for feedback</p>
  
  <p>UPDATE 1 :</p>
  
  <p>I tried to change the server declaratives , as following, but I still get the same issue ...  <a href="http://test.emberhub.me" rel="nofollow">http://test.emberhub.me</a> , is always redirected to port 443</p>
  
  <pre><code>server {
      listen 80;
      listen [::]:80 ipv6only=on;
      server_name emberhub.me;
      return 301 https://$host$request_uri;
  }
  
  server {
      listen 80;
  #        listen [::]:80 ipv6only=on;
      server_name www.emberhub.me;
      return 301 https://$host:443$request_uri;
  }
  
  server {
      listen 80;
  #        listen [::]:80 ipv6only=on;
      server_name test.emberhub.me;
      return 301 https://$host:444$request_uri;
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I updated the default config to </p>
  
  <pre><code>server {
      listen 80;
      listen [::]:80 ipv6only=on;
      server_name test.emberhub.me;
      return 301 https://$host:444$request_uri;
  }
  
  server {
      listen 80;
      listen [::]:80 ipv6only=on;
      server_name  emberhub.me  www.emberhub.me;
      return 301 https://$host$request_uri;
  }
  </code></pre>
  
  <p>using <code>curl -v</code> everything runs fine ...</p>
  
  <pre><code> $ curl -v http://emberhub.me
   * Rebuilt URL to: http://emberhub.me/
   ...
   &lt; Connection: keep-alive
   &lt; Location: https://emberhub.me/
   ...
   Connection #0 to host emberhub.me left intact
  
  $ curl -v http://www.emberhub.me
  ...
  &lt; Connection: keep-alive
  &lt; Location: https://www.emberhub.me/
  Connection #0 to host www.emberhub.me left intact
  
  curl -v http://test.emberhub.me
  Rebuilt URL to: http://test.emberhub.me/
  ...
  &lt; Location: https://test.emberhub.me:444/
  Connection #0 to host test.emberhub.me left intact
  </code></pre>
  
  <p>BUT requesting <a href="http://test.emberhub.me" rel="nofollow">http://test.emberhub.me</a> in the browser display <a href="https://test.emberhub.me" rel="nofollow">https://test.emberhub.me</a>  on port 443 ... </p>