title: Cloudfront Nginx Rewrite Causing Problems
body: <p>I am setting up a cdn using cloudfront. My origin for my cloudfront distribution is my aws load balancer (ELB). When I make a request to cloudfront instead of getting the cloudfront url (cdn.mysite.com/images/image.jpg) it is being redirected to <a href="https://www.alio.com/images/image.jpg" rel="nofollow">https://www.alio.com/images/image.jpg</a>. I figured out why it is doing this due to my nginx.conf:</p>
  
  <pre><code>server {
      root /var/www/html/alio/public;
      index index.php;
  
      server_tokens off;
  
      server_name www.alio.com;
  
      location / {
          if ($http_x_forwarded_proto != 'https') {
              rewrite ^ https://$host$request_uri? permanent;
          }
          try_files $uri $uri/ /index.php?$query_string;
      }
      location ~ \.php$ {
          try_files $uri /index.php =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
      }
  }
  </code></pre>
  
  <p>The rewrite ^ https://$host$request_uri? permanent; changes the url (when i delete the rewrite i get the cdn url). I have this rewrite to ensure that all requests to my site are https. If there a way to do instead of a rewrite a 301 redirect or not do this rewrite if I detect it's cloudfront making the call to the ELB?</p>