<p>I'm looking to redirect all traffic from </p>

<p><a href="http://example.com" rel="noreferrer">http://example.com</a> -> <a href="https://example.com" rel="noreferrer">https://example.com</a> like how nearly all websites do.</p>

<p>I've looked at this link with no success:
<a href="https://stackoverflow.com/questions/40763718/kubernetes-https-ingress-in-google-container-engine">Kubernetes HTTPS Ingress in Google Container Engine</a></p>

<p>And have tried the following annotations in my ingress.yaml file.</p>

<pre><code>nginx.ingress.kubernetes.io/configuration-snippet: |
  if ($http_x_forwarded_proto != 'https') {
    return 301 https://$host$request_uri;
  }
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
kubernetes.io/ingress.allow-http: "false"
</code></pre>

<p>All without any success. To be clear, I can access <a href="https://example.com" rel="noreferrer">https://example.com</a> and  <a href="http://example.com" rel="noreferrer">http://example.com</a> without any errors, I need the http call to redirect to https. </p>

<p>Thanks</p>

<p>Currently, the documentation for how to do this properly (annotations, SSL/HTTPS, health checks etc) is severely lacking, and has been for far too long. I suspect it's because they prefer you to use the App Engine, which is magical but stupidly expensive. For GKE, here's two options:</p>

<ul>
<li>ingress with a google-managed SSL cert and additional NGINX server configuration in front of your app/site</li>
<li>the NGINX ingress controller with self-managed/third-party SSL certs</li>
</ul>

<p>The following is steps to a working setup using the former.</p>

<h1>1 The door to your app</h1>

<p><strong>nginx.conf:</strong> (ellipses represent other non-relevant, non-compulsory settings)</p>

<pre><code>user  nginx;
worker_processes  auto;

events {
    worker_connections  1024;
}

http {
    ...

    keepalive_timeout  620s;

    ## Logging ##
    ...
    ## MIME Types ##
    ...
    ## Caching ##
    ...
    ## Security Headers ##
    ...
    ## Compression ##
    ....

    server {
        listen 80;

        ## HTTP Redirect ##
        if ($http_x_forwarded_proto = "http") {
            return 301 https://[YOUR DOMAIN]$request_uri;
        }

        location /health/liveness {
            access_log off;
            default_type text/plain;
            return 200 'Server is LIVE!';
        }

        location /health/readiness {
            access_log off;
            default_type text/plain;
            return 200 'Server is READY!';
        }

        root /usr/src/app/www;
        index index.html index.htm;
        server_name [YOUR DOMAIN] www.[YOUR DOMAIN];

        location / {
            try_files $uri $uri/ /index.html;
        }
    }
}
</code></pre>

<p>NOTE: <strong>One serving port only</strong>. The global forwarding rule adds the <a href="https://cloud.google.com/load-balancing/docs/https/#components" rel="noreferrer">http_x_forwarded_proto</a> header to all traffic that passes through it. Because ALL traffic to your domain is now passing through this rule (remember, one port on the container, service and ingress), this header will (crucially!) always be set. Note the check and redirect above: it only continues with serving if the header value is 'https'. The root and index and location values may differ depending for your project (this is an angular project). keepalive_timeout is set to the value <a href="https://cloud.google.com/load-balancing/docs/https/#timeouts_and_retries" rel="noreferrer">recommended by google</a>. I prefer using the main nginx.conf file, but most people add a custom.conf file to /etc/nginx/conf.d; if you do this just make sure the file is imported into the main nginx.conf http block using an <em>includes</em> statement. The comments highlight where other settings you may want to add when everything is working will go, like gzip/brotli, security headers, where logs are saved, and so on.</p>

<p><strong>Dockerfile:</strong></p>

<pre><code>...
COPY nginx.conf /etc/nginx/nginx.conf
CMD ["nginx", "-g", "daemon off;"]
</code></pre>

<p>NOTE: only the final two lines. Specifying an EXPOSE port is unnecessary. COPY replaces the default nginx.conf with the modified one. CMD starts a light server.</p>

<h1>2 Create a deployment manifest and apply/create</h1>

<p><strong>deployment.yaml:</strong></p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: uber-dp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: uber
  template:
    metadata:
      labels:
        app: uber
    spec:
      containers:
        - name: uber-ctr
          image: gcr.io/uber/beta:v1 // or some other registry
          livenessProbe:
            failureThreshold: 3
            initialDelaySeconds: 60
            httpGet:
              path: /health/liveness
              port: 80
              scheme: HTTP
          readinessProbe:
            failureThreshold: 3
            initialDelaySeconds: 30
            httpGet:
              path: /health/readiness
              port: 80
              scheme: HTTP
          ports:
            - containerPort: 80
          imagePullPolicy: Always
</code></pre>

<p>NOTE: only one specified port is necessary, as we're going to point all (HTTP and HTTPS) traffic to it. For simplicity we're using the same path for liveness and readiness probes; these checks will be dealt with on the NGINX server, but you can and should add checks that probe the health of your app itself (eg a dedicated page that returns a 200 if healthy).The readiness probe will also be picked up by GCE, which by default has its own irremovable health check.</p>

<h1>3 Create a service manifest and apply/create</h1>

<p><strong>service.yaml:</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: uber-svc
  labels:
    app: uber
spec:
  ports:
    - name: default-port
      port: 80
  selector:
    app: uber
  sessionAffinity: None
  type: NodePort
</code></pre>

<p>NOTE: default-port specifies port 80 on the container.</p>

<h1>4 Get a static IP address</h1>

<p>On GCP in the hamburger menu: VPC Network -> External IP Addresses. Convert your auto-generated ephemeral IP or create a new one. Take note of the name and address.</p>

<h1>5 <a href="https://cloud.google.com/load-balancing/docs/ssl-certificates" rel="noreferrer">Create an SSL cert</a> and default zone</h1>

<p>In the hamburger menu: Network Service -> Load Balancing -> click 'advanced menu' -> Certificates -> Create SSL Certificate. Follow the instructions, create or upload a certificate, and take note of the name. Then, from the menu: Cloud DNS -> Create Zone. Following the instructions, <a href="https://cloud.google.com/dns/docs/quickstart" rel="noreferrer">create a default zone</a> for your domain. Add a CNAME record with www as the DNS name and your domain as the canonical name. Add an A record with an empty DNS name value and your static IP as the IPV4. Save.</p>

<h1>6 Create an ingress manifest and apply/create</h1>

<p><strong>ingress.yaml:</strong></p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: mypt-ingress
  annotations:
    kubernetes.io/ingress.global-static-ip-name: [NAME OF YOUR STATIC IP ADDRESS]
    kubernetes.io/ingress.allow-http: "true"
    ingress.gcp.kubernetes.io/pre-shared-cert: [NAME OF YOUR GOOGLE-MANAGED SSL]
spec:
  backend:
    serviceName: mypt-svc
    servicePort: 80
</code></pre>

<p>NOTE: backend property points to the service, which points to the container, which contains your app 'protected' by a server. Annotations connect your app with SSL, and force-allow http for the health checks. Combined, the service and ingress configure the <a href="https://cloud.google.com/load-balancing/docs/https/" rel="noreferrer">G7 load balancer</a> (combined global forwarding rule, backend and frontend 'services', SSL certs and target proxies etc).</p>

<h1>7 Make a cup of tea or something</h1>

<p>Everything needs ~10 minutes to configure. Clear cache and test your domain with various browsers (Tor, Opera, Safari, IE etc). Everything will serve over https.</p>

<p><strong>What about the NGINX Ingress Controller?</strong>
I've seen discussion it being better because it's cheaper/uses less resources and is more flexible. It isn't cheaper: it requires an additional deployment/workload and service (GCE L4). And you need to do more configuration. Is it more flexible? Yes. But in taking care of most of the work, the first option gives you a more important kind of flexibility — namely allowing you to get on with more pressing matters.</p>
