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

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 ( I tried to set a 301 redirect via app.yaml file. Here is what I tried:

- 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/1.1 301 Moved Permanently
Date: Thu, 15 Apr 2021 07:03:34 GMT
Content-Type: text/html
Content-Length: 178
Server: nginx
Via: 1.1 google