Google Cloud Platform: Redirect 301 HTTPS on App Engine with nginx

Ercan (@flightlesstux)
Teknasyon Engineering

--

I started to play with App Engine on Google Cloud Platform these days. I’m using a flexible environment with a custom runtime to run PHP in Docker. App Engine serving the content via HTTP and HTTPS as well. I checked the disable HTTP or redirect the HTTP traffic easily to HTTPS via Web UI but unfortunately, it’s not possible right now. Yes, you heard right, the basic feature is not supporting!

I check the document for (https://cloud.google.com/appengine/docs/standard/php7/application-security) I tried to set a 301 redirect via app.yaml file. Here is what I tried:

handlers:
- url: /.*
script: auto
secure: always
redirect_http_response_code: 301

I deployed a new version of my app but App Engine still serving via HTTP and it doesn’t redirect to HTTPS when I requested insecure connection.

This time, I decided to give a shot to nginx. I opened my nginx-app.conf file and I wrote this:

server_name _;if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}

And Voila! It worked so perfectly. How I tried and understand this process you can see below.

➜  ~ curl -I http://ercan-ermis.ew.r.appspot.com/
HTTP/1.1 301 Moved Permanently
Date: Thu, 15 Apr 2021 07:03:34 GMT
Content-Type: text/html
Content-Length: 178
Server: nginx
Location: https://ercan-ermis.ew.r.appspot.com/
Via: 1.1 google

--

--

⌨️ Solution Architect, formerly DevOps Engineer Lead 👨‍👩‍👧‍👦 3x @AWSCommunity Builder 😶‍🌫️ Certified #AWS Solution Architect (SAA-C03) 🐍 #Python Dev♥️