title: Nginx: Redirect Conditional on Server Name and Sub Domain for Short URLs
body: <p>I want to redirect conditionally based on the server name, but where I redirect to also depends on the subdomain.  So for example, here is my basic config</p>
  
  <pre><code>server {
      listen 80;
      return 301 https://$host$request_uri;
  }
  
  server {
      listen 443;
      server_name company.com compa.ny;
      ssl on;
      ssl_client_certificate /etc/ssl/certs/godaddy_CA.crt;
      ssl_certificate /etc/ssl/certs/wildcard.company.com.crt;
      ssl_certificate_key /etc/ssl/private/wildcard.company.com.key;
      ssl_prefer_server_ciphers on;
      root /var/www/company;
      access_log /var/log/nginx/nginx.access.log;
      error_log /var/log/nginx/nginx.error.log;
      client_max_body_size 8M;
      location ^~ /application {
          proxy_set_header HOST $http_host;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_pass http://127.0.0.1:8080;
      }
  }
  </code></pre>
  
  <p>I want to have something that looks for the short url host "compa.ny" and redirects to "company.com/shortUrldRedirector" and I also want to include the subdomain, so in dev or qa this will work correctly:</p>
  
  <pre><code>https://compa.ny/abc123 -&gt; https://company.com/shortUrldRedirector/abc123
  
  and
  
  https://dev.compa.ny/abc123 -&gt; https://dev.company.com/shortUrldRedirector/abc123
  </code></pre>
  
  <p>I see there is a <code>$server_name</code> config variable, but how do I accomplish the above redirects respecting the subdomain?</p>