title: How to redirect HTTP requests to HTTPS with GCP Load Balancer
body: <p>I'm using L7 HTTP Load Balancer. I want to redirect from HTTP requests to HTTPS. This is the part of my nginx config. It works when I access to my web server without Load Balancer. But it does not work with Load Balancer. 404 error is returned.</p>
  
  <p>How can I fix it?</p>
  
  <p>I'm using nginx</p>
  
  <pre><code>server {
      listen 80;
      server_name example.com;
      return 301 https://$host$request_uri;
  }
  
  server {
      listen 443 ssl;
      ...
  }
  </code></pre>
  
  <p>HTTP Response</p>
  
  <pre><code>HTTP/1.1 404 Not Found
  Server: Google Frontend
  </code></pre>
  
  <p>I found this issue below and I added $http_x_forwarded_proto check. But the result did not change.</p>
  
  <p>Issue 255:  Redirect all HTTP traffic to HTTPS when using the HTTP(S) Load Balancer
  <a href="https://code.google.com/p/google-compute-engine/issues/detail?id=255&amp;thanks=255&amp;ts=1446612833" rel="nofollow noreferrer">https://code.google.com/p/google-compute-engine/issues/detail?id=255&amp;thanks=255&amp;ts=1446612833</a></p>
  
  <pre><code>server {
      listen 80;
      server_name example.com;
      if ($http_x_forwarded_proto = "http") { 
         return 301 https://$host$request_uri; 
      }
  }
  
  server {
      listen 443 ssl;
      ...
  }
  </code></pre>
  
  <hr>
  
  <p>Update 1</p>
  
  <p>I found that my load balancer has only HTTPS protocol end-point config. I think I need HTTP port end-point setting.</p>
  
  <p>This post below would be helpful.</p>
  
  <p><a href="https://stackoverflow.com/questions/36295056/gce-load-balancer-https-and-http-ip?rq=1">GCE Load Balancer HTTPS and HTTP IP</a></p>